Exceise

import java.util.ArrayList;
import java.util.Comparator;
//可以改进:在进行比较时,先进行验证,验证输入的类型是不是Employee
//对于MyDate的比较应该封装到MyDate里面
//定义Employee类
//        1)该类包含:private成员变量name,sal,birthday,其中 birthday为 MyDate 类的对
//        象;
//        2)为每-个属性定义 getter, setter 方法;
//        3)重写 toString 方法输出 name, sal, birthday
//        4)MyDate类包含: private成员变量month,day,year;并为每一个属性定义 getter
//        setter 方法;
//        5)创建该类的3个对象,并把这些对象放入 ArrayList 集合中(ArrayList 需使用泛
//        型来定义),对集合中的元素进行排序,并遍历输出:
//        调用ArrayList 的 sort 方法,传入 Comparator对象[使用泛型],先按照
//        排序方式:i
//        name排序,如果name相同,则按生日日期的先后排序。【即:定制排序】
public class Exceise {
    public static void main(String[] args) {
        ArrayList<Employee> list = new ArrayList<>();
        list.add(new Employee("xj",10000,new MyDate(10,1,2001)));
        list.add(new Employee("xj",1000,new MyDate(1,1,2011)));
        list.add(new Employee("xj",100,new MyDate(1,1,2021)));
        list.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                int i;
                i = o1.getName().compareTo(o2.getName());
                if(i == 0){
                    int j = o1.getBirthday().getYear() - o2.getBirthday().getYear();
                    if(j == 0){
                        int k = o1.getBirthday().getMonth() - o2.getBirthday().getMonth();
                        if(k == 0){
                            int n = o1.getBirthday().getDay() - o2.getBirthday().getDay();
                            return n;
                        }
                        return k;
                    }
                    return j;
                }
                return i;
            }
        });//这里传Comparator的匿名内部类,而Comparator是一个接口,所以要new一个Comparator
        //再将这个新创建的Comparator实现
        System.out.println(list);
    }

}
class Employee{
    private String name;
    private double sal;
    private MyDate birthday;
    public Employee(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "\n"+"Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }
}
class MyDate{
    private int month;
    private int day;
    private int year;

    public MyDate(int month, int day, int year) {
        this.month = month;
        this.day = day;
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "month=" + month +
                ", day=" + day +
                ", year=" + year +
                '}';
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值