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]);
}
}
}
答案 0 :(得分:2)
您通常不会实现排序。您使用Arrays.sort
(在您的情况下)或Collections.sort
。您所要做的就是提供一个定义排序的Comparator
。
http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html
答案 1 :(得分:0)
使用Java Comparator,或将Class设置为Comparable。 然后使用Arrays.sort()
请参阅此处的示例:http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/
答案 2 :(得分:0)
class MyComparator implements Comparator<Employee>{
public int compare(Employee e1, Employee e2) {
return Integer.valueOf(e1.id).compareTo(e2.id);
}
}
MyComparator myComparator = new MyComparator();
Collections.sort(array, myComparator);
答案 3 :(得分:0)
最好利用像TreeSet这样的Java Sorted Collections:
Collection<Employee> employList = new TreeSet<Employee>(
new Comparator<Employee>() {
@Override
public int compare(Employee a, Employee b) {
int getBirthDate = a.getBirthDate()
.compareTo(b.getBirthDate());
if (getBirthDate != 0)
return getBirthDate;
int getA = a.getA().compareTo(b.getA());
if (getA != 0)
return getA;
return Long.valueOf(a.getB()).compareTo(Long.valueOf(b.getB()));
}
});
employList.add(new Employee(...);
首先启动收集。然后开始添加Employee对象。它们将首先在BirthDate上自动排序,然后在Employee对象的A和B字段中自动排序。你可以添加任意多个或删除A并成为part,只返回a.getBirthdate()。compareTo(b.getBirthdate());如果你只需要生日。
答案 4 :(得分:0)
我想知道为什么没有人提到过Comparator.natural()。reverse()。