0

Ok, I asked before my Employee hashmap. I believe I fixed both my Equals & CompareTo method, where now, the Strings uses "Equals" rather than "==", where my integer ID still uses "==". The compareTo now utilizes all my employee information to sort out. I think my problem lies with the main compiler. I'm trying to sort employees who have the same last name, and first name, by ID number. Here is both my files. I'll still play around with my main, but this is driving me bananass.

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(this.getLastName().equalsIgnoreCase(((Employee) o).getLastName()) &&
        (this.getFirstName().equalsIgnoreCase(((Employee)o) .getFirstName()) &&
        (this.getId() == ((Employee)o) .getId())));
else
    return false;
}



public int compareTo(Object o) {
Employee e = (Employee) o;
if (this.lastName.equals(((Employee) o).lastName)){
if(this.firstName.equals(((Employee) o) .firstName)){
    return (this.id - e.getId());}
else
    return(this.firstName.compareTo(((Employee) o).firstName));
}
 else
return(this.lastName.compareTo(((Employee) o).lastName));
}

public int hashCode(Object o){
return (int)this.id *
            firstName.hashCode() *
            lastName.hashCode();
}

public String toString()
{
return getLastName() + ", " + getFirstName() + " ID: " + getId() + " Rating: " + getPerfScale();

}
}

My 2nd main file that compiles. Every other case besides ascending function well. I'm playing around with treemaps with it. Before I was using getKey, then getValue(Where it gets my toString method in the other file). It used to omit the person with the same name that had the largest ID, now this omits the employee that has the smallest ID.

import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

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;
        case 3:
            modifyPerformanceScale(idNumber, performanceScale);
            break;
        case 4:
            printAllperfScale(performanceScale);
            break;
        case 5:
            printLastNameAscending(firstAndLast, idNumber);
            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);
    //changed - Added(addEmployee,perfScale) from put(perfScale)
    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();
    //System.out.println();


    firstAndLastMap.remove(lastName + ", " + firstName);
    idNumberMap.remove(id); 
    //should be remove(perfScale)

}
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);
    performanceScale.put(employee, modScale);
}

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> firstAndLast,
        TreeMap<Integer, Employee>idNumber) {
    Set Employee2 = firstAndLast.entrySet();
    Iterator itr2 = Employee2.iterator();
    while (itr2.hasNext()) {
        Map.Entry fe = (Map.Entry) itr2.next();
        System.out.println(fe.getValue());
    }

}
Christian
  • 35
  • 7

0 Answers0