好的,我在 Employee hashmap 之前问过。我相信我修复了我的 Equals 和 CompareTo 方法,现在,字符串使用“Equals”而不是“==”,我的整数 ID 仍然使用“==”。compareTo 现在利用我所有的员工信息进行整理。我认为我的问题在于主编译器。我正在尝试按 ID 号对姓氏和名字相同的员工进行排序。这是我的两个文件。我仍然会玩我的主要游戏,但这让我发疯。
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();
}
}
我编译的第二个主文件。除了上升之外的所有其他情况都很好。我正在玩它的树形图。在我使用 getKey 之前,然后是 getValue(它在另一个文件中获取我的 toString 方法的位置)。它以前省略了具有最大 ID 的同名人员,现在省略了具有最小 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());
}
}