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());
        }
    }

} 

Recommended Answers

All 2 Replies

Hello,
Haven't run your code, only scanned through it, so appologies if I didn't get this one right (what are the chances !? :P).
For me the problem is on line 104:
firstAndLastMap.remove(lastName = " , " + firstName);
Surely you wanted to say
firstAndLastMap.remove(lastName + " , " + firstName);
so that you delete the right thing from the map.

ok, thanks for the reply will check this out. Well, to run it, I only showed you one file, it consists of two files. This is my main tester. Here is the file, if anyone wants to try running it for reference.

public class Employee implements Comparable {
private String firstName; 
private String lastName;
private int id;
private int perfScale;

Employee() {
    firstName = "";
    lastName = "";
    id = 0;
    perfScale = 0;
    }

Employee(String lastName, String firstName, int id, int perfScale){
    this.firstName = firstName;
    this.lastName = lastName;
    this.id = id;
    this.perfScale = perfScale;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName){
    this.lastName = lastName;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName){
    this.firstName = firstName;
}
public int getId() {
    return id;
}
public void setId(int id){
    this.id = id;
}
public int getPerfScale() {
    return perfScale;
}
public void setPerfScale(int perfScale){
    this.perfScale = perfScale;
}
public boolean equals(Object o) {
    if(o instanceof Employee)
        return(getLastName() == ((Employee) o) .getLastName()) &&
            (getFirstName() == ((Employee)o) .getFirstName());
    else
        return false;
}

public int compareTo(Object o) {
    Employee e = (Employee) o;
    int performance1 = e.getPerfScale();
    int performance2 = this.getPerfScale();

    if(performance1 < performance2) {
        return 1;

    } else if(performance1 > performance2) {
        return -1;
    } else {
        return this.getLastName().compareTo(e.getLastName());
    }
}

public int hashCode() {
    int h1 = firstName.hashCode();
    int h2 = lastName.hashCode();
    int h3 = new Integer(id).hashCode();
    final int HASH_MULTIPLIER1 = 29;
    final int HASH_MULTIPLIER2 = 19;
    final int HASH_MULTIPLIER3 = 17;
    int h = HASH_MULTIPLIER1 * h1 + HASH_MULTIPLIER2 * h2 + HASH_MULTIPLIER3 * h3;
    return h;
}

public String toString()
{
    return getLastName() + ", " + getFirstName() + " ," + getId() + " rating: " + getPerfScale()+ " Performance Scale";

}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.