最近、ハッシュマップについて学びました。これまでのところ、私のコードでは、パフォーマンス スケールの追加、削除、出力、および従業員の姓/名の並べ替えを行うことができます。ただし、更新/変更は何もしていません。私の試みは、最初に更新したい従業員の ID を検索することです。ID を入力した後、ユーザーにパフォーマンス スケールを変更するように求めますが、後でユーザーがすべてのパフォーマンス スケールを出力しても、何も変更されません。
したがって、更新方法に関するアドバイス/ヒント/リンクは役に立ちます。すべてのケースをカバーするコードの一部を次に示します。(ケース 3 は更新/変更であり、私が試みた作業を示しています)。
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());
}
}
}