Ad

תרגול טיפוסי נתונים מורכבים
במסעדה של טוויטי רוצים לעקוב אחרי העובדים במסעדה ואחרי הגילאים שלהם על מנת לנהל את השכר שלהם בצורה מדויקת וטובה. כאן בדיוק אתם נכנסים לתמונה: עליכם לכתוב אפליקציה בת שתי מחלקות שתנהל את רשימות העובדים במסעדה.
מחלקת ה-Main
מחלקה זו תכיל את את השגרה הראשית main והיא תציג תפריט למשתמש עד אשר הוא יבחר לצאת. כל פעם שהמשתמש בוחר פעולה מהתפריט יש להפעיל את המתודות המתאימות.
התפריט יכיל את האופציות הבאות:

  1. הוספת עובד
  2. הסרת עובד
  3. בדיקה האם עובד קיים ברשימות העובדים
  4. הדפסת כל העובדים במסעדה
  5. שינוי גיל של עובד
    יש להגדיר קבועים לייצוג האפשרויות האלו

מחלקת המסעדה – Restaurant
למחלקה זו data member אחד:
private HashMap<String, Integer> emploees;
ה Data Member שלנו הוא מטיפוס HashMap.
מתודות:
הוספת עובד
public boolean addEmployee(String name, int age)
ערך החזר:
האם הצלחנו להוסיף את העובד.
הפרמטרים המתקבלים:
שם העובד
גל העובד
שני נתונים אלו ישמרו בתוך הרשימה שלנו.
כיצד ניתן לשמור את שניהם יחדיו? כנראה שאחד מהערכים יהיה המפתח (הממוין), והשני יהיה הערך הנשמר. חישבו היטב מה יהיה ממוין ומה יהיה הערך.
כאשר אנו קולטים את שני הנתונים הללו יש לבדוק את הדברים הבאים:
השדות הללו מכילים ערכים – הרי לא הגיוני שנקלוט עובד שאין לו שם.
לא קיים עובד עם שם כזה במערכת – כי אם הוא כבר קיים המערכת תספוג תעופה

פיטור עובד
public boolean removeEmployee(String name)
הסרה מהמסעדה של העובד
יש לבדוק שהעובד באמת קיים במערכת

בדיקה האם קיים עובד במערכת
private boolean isEmployeeExist(String name)
מקבלת שם עובד ובודקת האם הוא קיים ברשימה
זוהי מתודה פרטית למחלקת המסעדה, חישבו היכן היא תהיה שימושית.

הדפסת רשימת העובדים
public void printEmployees()

יש לטפל במקרה ולא קיימים עובדים במסעדה באמצעות הדפסה מיוחדת.

import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
    private static Scanner input = new Scanner(System.in);
    private static final int EXIT = -999;
    public static Restaurant restaurant;
    final static int ADD_EMPLOYEE = 1;
    final static int REMOVE_EMPLOYEE = 2;
    final static int IS_EMPLOYEE_EXIST = 3;
    final static int PRINT_EMPLOYEES = 4;
    final static int CHANGE_EMPLOYEE_AGE = 5;

    public static void main(String[] args) {
        System.out.println("Choose your option from the list below or if you want to stop enter -999");
        System.out.println("Press "+ADD_EMPLOYEE+" to add employee");
        System.out.println("Press "+REMOVE_EMPLOYEE+" to remove employee");
        System.out.println("Press "+IS_EMPLOYEE_EXIST+" to check if employee exist");
        System.out.println("Press "+PRINT_EMPLOYEES+" to print employees");
        System.out.println("Press "+CHANGE_EMPLOYEE_AGE+" to change age");

        int option = input.nextInt();
        while (option > 5 || (option < 1  && option!= EXIT )) {
            System.out.println("Wrong option number choose your option again");
            option = input.nextInt();
        }

        restaurant = new Restaurant();

        while (option != EXIT) {
            turn(option);

            System.out.println("Choose your option if you want to stop enter -999");
            option = input.nextInt();
            while (option > 5 || (option < 1  && option!= EXIT )) {
                System.out.println("Wrong option number choose your option again");
                option = input.nextInt();
            }
        }

    }

    private static void turn(int option) {
        switch (option) {
            case ADD_EMPLOYEE:
                addEmployee();

                break;
            case REMOVE_EMPLOYEE:
                removeEmployee();

                break;
            case IS_EMPLOYEE_EXIST:
                isEmployeeExist();

                break;
            case PRINT_EMPLOYEES:
                restaurant.printEmployees();

                break;
            case CHANGE_EMPLOYEE_AGE:
                changeEmployeeAge();

                break;
            default:
                break;
        }
    }

    private static void addEmployee() {
        String name;
        int age;

        System.out.println("Enter employee's name");
        name = input.next();
        System.out.println("Enter employee's age");
        age = input.nextInt();
        if (restaurant.addEmployee(name, age)) {
            System.out.println("You added the employee name to " + name + " and age to " + age);
        } else {
            if (name.isEmpty()) {
                System.out.println("You did not entered a name");
            } else {
                System.out.println("The employee " + name + " already exist");
            }
        }
    }

    private static void removeEmployee() {
        String name;

        System.out.println("Enter employee's name");
        name = input.next();
        if (restaurant.removeEmployee(name)) {
            System.out.println("You remvoed the employee " + name);
        } else {
            System.out.println("The employee " + name + " does not exist");
        }
    }

    private static void isEmployeeExist() {
        String name;

        System.out.println("Enter employee's name");
        name = input.next();
        if (restaurant.isEmployeeExist(name)) {
            System.out.println("Employee " + name + " exist");
        } else {
            System.out.println("Employee " + name + "  does not exist");
        }

    }

    private static void changeEmployeeAge() {
        String name;
        int age;

        System.out.println("Enter employee's name");
        name = input.next();
        System.out.println("Enter employee's age");
        age = input.nextInt();
        if (restaurant.setAge(name, age)) {
            System.out.println("You updated the employee " + name + " age to " + age);
        } else {
            System.out.println("The employee " + name + " does not exist");
        }
    }
}

import java.util.HashMap;

public class Restaurant {
    private HashMap<String, Integer> emploees;
    public Restaurant(){
        emploees = new HashMap<String, Integer>();
    }

    public boolean addEmployee(String name, int age) {
        if (this.isEmployeeExist(name) || name.isEmpty()) {
            return false;
        } else {
            this.emploees.put(name, age);
            return true;
        }
    }

    public boolean removeEmployee(String name) {
        if(this.isEmployeeExist(name)){
            this.emploees.remove(name);
            return true;
        }
        return false;
    }

    public boolean isEmployeeExist(String name){
        return (this.emploees.containsKey(name));
    }

    public void printEmployees() {
        if(this.emploees.isEmpty()){
            System.out.println("This restaurant has no workers");
        }
        else{
            for(String key : this.emploees.keySet()){
                System.out.println(key+" "+ this.emploees.get(key));

            }
        }

    }

    public boolean setAge(String name,int age){
        if(this.emploees.containsKey(name)){
            this.emploees.replace(name,age);
            return true;
        }
        return false;


    }



}