0

Just learned about hashmap recently. So far, my code can Add, Remove, print performance scales, and sort last/first Names of an Employee. However, update/modify is not doing anything. My attempt is, to first search for an Employee's ID that I want to update. After entering their ID, I would then prompt the user to modify a performance scale, but afterwards if the user prints out all Performance Scale, it still does not change anything.

Therefore, any advice/tips/links on how to update is helpful. Here's some part of my code that covers all cases. (Case 3 is the update/modify, shows my attempted work on it).

switch (choice) {
        case 1:
            addEmployee(firstAndLast, idNumber, performanceScale);
            break;
        case 2:
            removeEmployee(firstAndLast, idNumber, performanceScale);
            break;

        case 3:
            modifyPerformanceScale(idNumber);
            break;
        case 4:
            printAllperfScale(performanceScale);
            break;
        case 5:
            printLastNameAscending(firstAndLast);
            break;
        case 6:
            exit = true;
            System.out.println("Exiting program...");
            break;
        default:
            System.out
                    .println("Please choose a number from 1 - 5 from the menu.");
        }
    }

}

public static void addEmployee(TreeMap<String, Employee> firstAndLastMap,
        TreeMap<Integer, Employee> idNumberMap,
        TreeMap<Employee, Integer> performanceScale) {
    Scanner keyboardInput = new Scanner(System.in);
    String firstName;
    String lastName;
    int id;
    int perfScale;

    System.out.print("Enter first name for the Employee: ");
    firstName = keyboardInput.nextLine();
    System.out.print("Enter last name for the Employeer: ");
    lastName = keyboardInput.nextLine();
    System.out.print("Enter ID number of the Employee: ");
    id = keyboardInput.nextInt();
    System.out.print("Enter Performance Scale rating between 1 to 5: ");
    perfScale = keyboardInput.nextInt();

    Employee addEmployee = new Employee(lastName, firstName, id, perfScale);

    firstAndLastMap.put(lastName + ", " + firstName, addEmployee);
    idNumberMap.put(id, addEmployee);

    performanceScale.put(addEmployee, perfScale);

}

public static void removeEmployee(TreeMap<String, Employee> firstAndLastMap,
        TreeMap<Integer, Employee> idNumberMap,
        TreeMap<Employee, Integer> performanceScale) {

    Scanner keyboardInput = new Scanner(System.in);
    String firstName;
    String lastName;
    int id;
    System.out.print("Enter First name of Employee you want to remove: ");
    firstName = keyboardInput.nextLine();
    System.out.print("Enter last name of Employee you want to remove: ");
    lastName = keyboardInput.nextLine();

    System.out.print("Enter ID number of Employee you want to remove: ");
    id = keyboardInput.nextInt();



    firstAndLastMap.remove(lastName + ", " + firstName);
    idNumberMap.remove(id); 


}

public static void modifyPerformanceScale(TreeMap<Integer, Employee> idNumber) {
    System.out.print("Enter ID: ");
    Scanner keyboardInput = new Scanner(System.in);

    int idNumber1;
    int modScale;
    idNumber1 = keyboardInput.nextInt();
    idNumber.remove(idNumber1);

    System.out.print("Enter the number you want to change to: ");
    modScale = keyboardInput.nextInt();
}

public static void printAllperfScale(TreeMap<Employee,Integer> performanceScale) {
    Set Employee1 = performanceScale.entrySet();    
    Iterator itr1 = Employee1.iterator();

    while (itr1.hasNext()) {
        Map.Entry me = (Map.Entry) itr1.next();
        System.out.println(me.getValue());
    }
}


public static void printLastNameAscending(TreeMap<String, Employee> LastName) {
    Set Employee1 = LastName.entrySet();
    Iterator itr1 = Employee1.iterator();

    while (itr1.hasNext()) {
        Map.Entry me = (Map.Entry) itr1.next();
        System.out.println(me.getKey());
    }
}

    } 
4

1 回答 1

0

我相信你想改变你的方法

public static void modifyPerformanceScale(TreeMap<Integer, Employee> idNumber, TreeMap<Employee, Integer> performanceScale) {
    System.out.print("Enter ID: ");
    Scanner keyboardInput = new Scanner(System.in);

    int idNumber1;
    int modScale;
    idNumber1 = keyboardInput.nextInt();

    System.out.print("Enter the number you want to change to: ");
    modScale = keyboardInput.nextInt();

    Employee employee = idNumber.get(idNumber1); //note: This will return null if the employee does not exist. Check for this, if necessary.
    performanceScale.put(employee, modScale);
}

这将获取具有该 ID 的员工。

这将用新条目替换该员工的先前条目。(Map.put如果键不存在,则添加条目,如果该键已存在于映射中,则替换该键的条目。)

然后确保将正确TreeMap的 s 传递给您的新modifyPerformanceScale方法。

于 2013-10-28T02:48:06.287 回答