根据枚举过滤学生班级

时间:2015-12-30 15:42:39

标签: java class enums compare abstract

我有一个抽象类Human和一个派生类Student   - Human类具有名字和姓氏字段。   - Student类有一个名为'type'的新字段,这是一个枚举,其中包含选项 PRIMARY SECONDARY COLLEGE ,和平均成绩字段,这是学生一年中的平均成绩。

我需要帮助用枚举定义Student类。我试过这个:

public class Student extends Human {

    private int averageGrade;

    public enum TYPE {

        PRIMARY(1), SECONDARY(2), COLLEGE(3);

        private int studentType;

        TYPE(int stType) {
            studentType = stType;
        }

        public int GetStudentType() {
            return studentType;
        }

        public void SetStudentType(int value) {
            if (value != 1 || value != 2 || value != 3) {
                throw new IllegalArgumentException(" dsfss");
            }
            this.studentType = value;
        }

        public Student(String firstName, String lastName, int studentType, int averageGrade) {
            super(firstName, lastName);

            this.setAverageGrade(averageGrade);
        }

        public int getAverageGrade() {
            return averageGrade;
        }

        public void setAverageGrade(int averageGrade) {
            if (averageGrade < 2 && averageGrade > 6) {
                throw new IllegalArgumentException("Student grades are between 2 and 6 inclusive");
            }
            this.averageGrade = averageGrade;
        }


}

此外,我必须打印每个学生类型中成绩最高的学生( PRIMARY SECONDARY COLLEGE )。

我不知道用于枚举的比较器是什么类型,averageGrade很简单,但首先我需要根据类型枚举对它们进行排序。

请帮忙!我将不胜感激。

1 个答案:

答案 0 :(得分:0)

我认为最好在不同的文件中创建TYPE,比如StudentType,然后在Student的类主体中添加一个属性。然后你会有这样的事情:

public class Student {

    private String firstName, lastName;
    private Integer averageGrade;
    private StudentType studentType;

    // getters and setters

}

这样的枚举:

public enum StudentType {
    PRIMARY, SECONDARY, COLLEGE;
}

您可以使用compareTo(E o)方法比较两个枚举(https://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html#compareTo(E))。 您可以使用compareTo(Integer anotherInteger)与整数(https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#compareTo(java.lang.Integer))进行比较。

例如:

public class StudentComparator  implements Comparator<Student>{

    public int compare(Student s1, Student s2) {
        // ASC comparing on attribute studentType
        int resultType = s1.getStudentType().compareTo(s2.getStudentType());
        if (resultType == 0) {
            // DESC comparing on attribute average grade
            return s2.getAverageGrade().compareTo(s1.getAverageGrade()); 
        } 
        return resultType; 
    }

    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student("A", "A", 10, StudentType.COLLEGE));
        list.add(new Student("B", "B", 1, StudentType.COLLEGE));
        list.add(new Student("C", "C", 9, StudentType.PRIMARY));
        list.add(new Student("D", "D", 7, StudentType.SECONDARY));
        list.add(new Student("E", "E", 8, StudentType.PRIMARY));


        Collections.sort(list, new StudentComparator());

        for (Student student : list) {
            System.out.println(student);
        }
    }

}

输出将是:

C C at PRIMARY: 9
E E at PRIMARY: 8
D D at SECONDARY: 7
A A at COLLEGE: 10
B B at COLLEGE: 1