好吧,我的 java 项目快完成了,但还有一个部分要处理,排序。我必须根据员工的 ID 号使用升序排序。我不确定要使用哪种类型,以及在哪里实现它。为它创建一个新类?让我向您展示我的测试代码。我有教职员工/兼职//员工/教育课程都在工作。这些类的代码都显示在 TestEmployee 中。因此,对于如何对我的 ID 号进行排序的任何帮助/提示,我们将不胜感激。
import java.util.Calendar;
import java.util.Scanner;
public class TestEmployee implements EmployeeInfo{
public static void main(String[] args) throws CloneNotSupportedException {
/**
* Part A: output all employee from Staff, Faculty, and partime.
*/
double sum=0,total=0;
Employee[]emp = new Employee[9];
Calendar staffBirthDate = Calendar.getInstance();
Calendar facultyBirthDate = Calendar.getInstance();
Calendar partimeBirthDate = Calendar.getInstance();
staffBirthDate.set(1959, 2, 23);// Scott Chan
emp[0] = new Staff("Chan, Scott", 123, 'M', staffBirthDate, 35.00);
staffBirthDate = Calendar.getInstance();
staffBirthDate.set(1964, 7, 12);// Brian Salinas
emp[1] = new Staff("Salinas, Brian", 456, 'F', staffBirthDate, 30.00);
staffBirthDate = Calendar.getInstance();
staffBirthDate.set(1970, 6, 2);// Allen Weir
emp[2] = new Staff("Weir, Allen", 789, 'M', staffBirthDate, 22.00);
facultyBirthDate = Calendar.getInstance();
facultyBirthDate.set(1962, 4, 27);
emp[3] = new Faculty("Im, Lee", 243, 'F', facultyBirthDate, "Full", "PH.D", "Engineering", "3");
facultyBirthDate = Calendar.getInstance();
facultyBirthDate.set(1975, 3, 14);//
emp[4] = new Faculty("Bui, Thung", 791, 'F', facultyBirthDate, "Associate", "PH.D", "English", "1");
facultyBirthDate = Calendar.getInstance();
facultyBirthDate.set(1980, 5, 22);//
emp[5] = new Faculty("Monreno, Maria", 623, 'F', facultyBirthDate, "Assistant", "MS", "Physical Education", "0");
partimeBirthDate = Calendar.getInstance();
partimeBirthDate.set(1977, 8, 10);
emp[6] = new Partime("Lee, Chesong", 455, 'F', partimeBirthDate, 20, 35.00);
partimeBirthDate = Calendar.getInstance();
partimeBirthDate.set(1987, 9, 15);
emp[7] = new Partime("Garcia, Frank", 678, 'M', partimeBirthDate, 25, 30.00);
partimeBirthDate = Calendar.getInstance();
partimeBirthDate.set(1980, 8, 22);//
emp[8] = new Partime("Alquilo, Roscoe", 945, 'M', partimeBirthDate, 30, 20.00);
for(int i = 0; i<emp.length;i++)
{
if(emp[i] instanceof Staff)
{
System.out.println("\n"+emp[i]);
}//end of if statement
if(emp[i] instanceof Faculty)
{
System.out.println("\n"+emp[i]);
}//end of if statement
}// end of for loop
for(int i = 0; i<emp.length; i++)
{
sum = ((Employee) emp[i]).monthlyEarning()+sum;
}
System.out.println("\nTotal monthly salary for all Employees");
System.out.println("$"+sum);
//c
System.out.println("\nTotal monthly salary for all faculuty");
for(int i = 0; i<emp.length;i++)
{
if(emp[i] instanceof Faculty)
{
total = ((Employee) emp[i]).monthlyEarning()+total;
}
}
System.out.println("$"+total);
// Duplicate a faculty object. test the duplication
Faculty f1 = (Faculty)emp[4];
Faculty f2 = (Faculty)f1.clone();
Education dupl = new Education("PH.D",
"Doctor", "4");
f2.setEducation(dupl);
System.out.println("\nD Duplicate a Faculty Object"
+"\n"+f2.toString());
// Verify two staff objects are the same
System.out.println("\nE.Verify two staff objects ");
Staff s1 = (Staff)emp[6];
Staff s2 = (Staff)s1.clone();
staffBirthDate = Calendar.getInstance();
Staff s3 = new Staff("Danger, Norman", 456, 'M', staffBirthDate, 25.00);
if(s1.getBirthdate()==s2.getBirthdate())
{
System.out.print("\nThe two staff objects " +
" birthdays"+ " are the same "
+"therefore "+true+"\n");
}
if(s3.getBirthdate()==s1.getBirthdate())
{
System.out.print(true);
}
// Sort employees by ascending employee ID
System.out.println("\nSort employees by ID");
{
System.out.println("\n"+emp[i]);
}
}
}