項目三(2)——職工表+比較器
阿新 • • 發佈:2017-10-11
sys import int time() util println ide ray gen
修改項目三(1)中的第3題,實現比較方法,將對象數組的數據按照生日的大小給職工排序。
package pro4; import java.util.Arrays; public class Employee02 { public static void main(String args[]){ Employee e[]=new Employee[10]; e[0]=new Employee("10001","張三","男",19960920,"人事部",20170902); e[1]=new Employee("10002","李四","女",19950920,"人事部",20160902); e[2]=new Employee("10003","王五","男",19940920,"研發部",20150902); e[3]=new Employee("10004","趙六","女",19930920,"研發部",20140902); e[4]=new Employee("10005","錢七","男",19920920,"辦公室",20150902); e[5]=new Employee("10006","王八","男",19910920,"辦公室",20160902); e[6]=new Employee("10007","張九","女",19960520,"保安處",20170902); e[7]=new Employee("10008","李十","男",19960420,"保安處",20150902); e[8]=new Employee("10009","白一","女",19960320,"維修部",20130902); e[9]=new Employee("10010","陳二","男",19960220,"維修部",20270902); System.out.println("本公司職工列表:"); for(int i=0;i<10;i++){ e[i].print(); } Arrays.sort(e); System.out.println("\n排序之後:"); for(int i=0;i<10;i++){ e[i].print(); } } } class Datetime{ private int time; public Datetime(int time){ this.setTime(time); } public void setTime(int time){ this.time=time; } public int getTime(){ return this.time; } } class Employee implements Comparable<Employee>{ private String num; private String name; private String sex; private Datetime birth; private String apart; private Datetime worktime; public Employee(){ } public Employee(String num,String name,String sex,int time01,String apart,int time02){ this.setNum(num); this.setName(name); this.setSex(sex); this.setBirth(time01); this.setApart(apart); this.setWorkwtime(time02); } public void setNum(String num){ this.num=num; } public String getNum(){ return this.num; } public void setName(String name){ this.name=name; } public String getName(){ return this.name; } public void setSex(String sex){ this.sex=sex; } public String getSex(){ return this.sex; } public void setBirth(int time01){ this.birth=new Datetime(time01); } public int getBirth(){ int temp=this.birth.getTime(); return temp; } public void setApart(String apart){ this.apart=apart; } public String getApart(){ return this.apart; } public void setWorkwtime(int time02){ this.worktime=new Datetime(time02); } public int getWorkwtime(){ int temp=this.worktime.getTime(); return temp; } public void print(){ System.out.println(this.num+" "+this.name+" "+this.sex+" "+birth.getTime()+" "+this.apart+" "+worktime.getTime()); } @Override public int compareTo(Employee arg0) { // TODO Auto-generated method stub return this.getBirth()-arg0.getBirth(); } }
項目三(2)——職工表+比較器