package com.sf.day16.homework;

import java.util.Set;
import java.util.TreeSet;

public class Student implements Comparable<Student>{
    /**
     * 学生姓名
     */
    private String name;
    /**
     * 学生年龄
     */
    private int age;
    /**
     * 学生成绩
     */
    private int score;

    public Student() {
    }

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "name='" + name +
                " age=" + age +
                " score=" + score;
    }

    //如果成绩一样，那在成绩排序的基础上按照年龄由小到大排序，
    // 成绩和年龄都一样，则按照姓名的字典顺序排序。
    @Override
    public int compareTo(Student o) {
        int num = this.score - o.score;
        int num1 = num == 0 ? this.age - o.age : num;
        int num2 = num1 == 0 ? this.name.compareTo(o.name) : num1;
        return num2;
    }


    public static void main(String[] args) {
        //Comparable方式接口对以上同学的成绩做降序排序

        //创建TreeSet集合对象
        Set<Student> treeSet = new TreeSet<Student>();

        //创建学生对象
        treeSet.add(new Student("Tom",20,90));
        treeSet.add(new Student("Jerry",22,95));
        treeSet.add(new Student("John",20,100));
        treeSet.add(new Student("Lily",22,100));
        treeSet.add(new Student("Lucy",22,90));
        treeSet.add(new Student("Kevin",22,90));

        //遍历数组
        for (Student student : treeSet){
            System.out.println(student);
        }

    }
    }
