Java实验项目三——职工类对象数组按照职工生日排序

时间:2023-03-10 06:00:29
Java实验项目三——职工类对象数组按照职工生日排序

Program:

修改项目三(1)中的第3题,实现比较方法,将对象数组的数据按照生日的大小给职工排序。

Description:令日期类MyDate和员工类Worker类实现接口Comparable,并实现方法compareTo()

代码如下:

MyDate类:

 1 /*
2 * Description:定义日期时间类,实现Comparable接口
3 *
4 * Written By:Cai
5 *
6 * Date Written:2017-10-18
7 *
8 *
9 * */
10
11 package entity;
12
13
14 public class MyDate implements Comparable<MyDate> {
15
16 private String year; //年
17 private String month; //月
18 private String day; //日
19
20
21 public MyDate() {
22
23 }
24
25 public MyDate(String year,String month,String day) {
26
27 this.year = year;
28 this.month = month;
29 this.day = day;
30 }
31
32
33 //定义setter和getter方法
34 public String getYear() {
35 return year;
36 }
37
38 public void setYear(String year) {
39 this.year = year;
40 }
41
42 public String getMonth() {
43 return month;
44 }
45
46 public void setMonth(String month) {
47 this.month = month;
48 }
49
50 public String getDay() {
51 return day;
52 }
53
54 public void setDay(String day) {
55 this.day = day;
56 }
57
58 //覆写toString()方法
59 public String toString() {
60 return this.year + "年" + this.month + "月" + this.day + "日";
61 }
62
63
64 //实现Comparable接口的compareTo()方法
65 @Override
66 public int compareTo(MyDate temp) {
67
68 //将对象的日期连接成时间戳一起比较
69 String str1 = this.year + this.month + this.day;
70 String str2 = temp.getYear() + temp.getMonth() + temp.getDay();
71
72 //直接调用String类的compareTo()方法,按字符串从小到大排序
73 return str1.compareTo(str2);
74 }
75
76 }

Worker类:

  1 /*
2 * Description:定义职工类,实现Comparable接口
3 *
4 * Written By:Cai
5 *
6 * Date Written:2017-10-18
7 *
8 *
9 * */
10
11 package entity;
12
13 import entity.MyDate;
14
15 public class Worker implements Comparable<Worker> {
16
17 String workerId;   //职工号
18 String workerName; //职工姓名
19 String workerSex; //职工性别
20 String workerPartment; //职工部门
21 MyDate workerBirthday; //职工生日
22 MyDate workerBeginDate; //职工开始工作时间
23
24
25 //定义构造方法
26 public Worker() {
27
28 }
29
30 public Worker(String id,String name,String sex,String partment,MyDate birthday,MyDate beginDate) {
31
32 this.workerId = id;
33 this.workerName = name;
34 this.workerSex = sex;
35 this.workerPartment = partment;
36 this.workerBirthday = birthday;
37 this.workerBeginDate = beginDate;
38 }
39
40
41 //定义setter和getter方法
42 public String getWorkerId() {
43 return workerId;
44 }
45
46 public void setWorkerId(String workerId) {
47 this.workerId = workerId;
48 }
49
50 public String getWorkerName() {
51 return workerName;
52 }
53
54 public void setWorkerName(String workerName) {
55 this.workerName = workerName;
56 }
57
58 public String getWorkerSex() {
59 return workerSex;
60 }
61
62 public void setWorkerSex(String workerSex) {
63 this.workerSex = workerSex;
64 }
65
66 public String getWorkerPartment() {
67 return workerPartment;
68 }
69
70 public void setWorkerPartment(String workerPartment) {
71 this.workerPartment = workerPartment;
72 }
73
74 public MyDate getWorkerBirthday() {
75 return workerBirthday;
76 }
77
78 public void setWorkerBirthday(MyDate workerBirthday) {
79 this.workerBirthday = workerBirthday;
80 }
81
82 public MyDate getWorkerBeginDate() {
83 return workerBeginDate;
84 }
85
86 public void setWorkerBeginDate(MyDate workerBeginDate) {
87 this.workerBeginDate = workerBeginDate;
88 }
89
90 //覆写toString方法
91 @Override
92 public String toString() {
93 return "Worker [workerId=" + workerId + ", workerName=" + workerName
94 + ", workerSex=" + workerSex + ", workerPartment="
95 + workerPartment + ", workerBirthday=" + workerBirthday
96 + ", workerBeginDate=" + workerBeginDate + "]";
97 }
98
99 //实现接口中compareTo()方法,按照出生日期,从大到小排序
100 @Override
101 public int compareTo(Worker temp) {
102
103 return this.getWorkerBirthday().compareTo(temp.getWorkerBirthday());
104 }
105
106 }

TestDemo类:

 1 /*
2 * Description:定义测试类,模拟十个职工的单位,并且排序
3 *
4 * Written By:Cai
5 *
6 * Date Written:2017-10-09
7 *
8 * */
9
10 package main;
11
12 import entity.*;
13
14 public class TestDemo {
15
16 public static void main(String args[]) {
17
18 Worker[] workers = new Worker[10]; //声明Worker类数组对象,大小为10
19
20 //为数组对象依次赋值
21 for( int i = 0; i < 10; i++ ) {
22
23 workers[i] = new Worker("00000" + i,"worker_" + i,"男","办公室" + i,new MyDate("199" + (9-i), ((9-i)) + "",((9-i)) + "" ),
24 new MyDate("200" + i, (i+1) + "",(i+1) + "" ));
25 }
26
27 //对数组按照排序:按照生日由大到小排序
28 java.util.Arrays.sort(workers);
29
30 //打印数组对象
31 display(workers);
32
33 }
34
35 //定义方法,打印Worker类的数组对象信息
36 public static void display(Worker[] workers) {
37
38 for( int i = 0; i < workers.length; i++ ) {
39 System.out.println( workers[i] );
40 }
41 }
42
43 }