Hello. Just learned about hashmap a week ago. My code can add an Employee's name/id/performance scale based on user input. I allow the user to also remove an employee by allowing user to input name/id to remove. However, when the user prints out sorted last name, the name the user input to remove, still shows up on the sorted list. All I seek is an answer why my code won't remove employee information.
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
//changed TreeMap<Employee, Integer> id number, to Integer, Employee.
public class EmployeeTest {
public static void main(String[] args) {
TreeMap<String, Employee> firstAndLast = new TreeMap<String, Employee>();
TreeMap<Integer, Employee> idNumber = new TreeMap<Integer, Employee>();
TreeMap<Employee, Integer> performanceScale = new TreeMap<Employee, Integer>();
TreeSet<Integer> sort = new TreeSet<Integer>();
Scanner keyboardInput = new Scanner(System.in);
boolean exit = false;
int choice;
while (exit != true) {
System.out.println("//-----MENU-----//");
System.out.println("1. Add an Employee ");
System.out.println("2. Remove an Employee ");
System.out.println("3. Modify performance scale ");
System.out.println("4. Print all the performance scale ");
System.out.println("5. Sort first and last name based on ID ");
System.out.println("6. Exit the program.");
System.out.print("Enter choice: ");
choice = keyboardInput.nextInt();
switch (choice) {
case 1:
addEmployee(firstAndLast, idNumber, performanceScale);
break;
case 2:
removeEmployee(firstAndLast, idNumber, performanceScale);
break;
//changed firstAndLast, to idNumber
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.");
}
}// end while
} // end main
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());
}
}
}