第三阶段BLOG

OOP10-16周总结

22201303-范宇

前言

这几周主要学习了java的集合框架一系列知识,如,HashSet,HashMap,TreeMap等,以及对该知识的应用。

这几周的作业让我们进一步掌握OOP的继承和多态性,而其中具有代表性的就是课程成绩统计系统。因此,这次blog就围绕课程成绩统计系统展开。

设计与分析

课程成绩统计系统进行了三次迭代

课程成绩统计系统-1

某高校课程从性质上分为:必修课、选修课,从考核方式上分为:考试、考察。

考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重0.3,期末成绩权重0.7,总成绩=平时成绩*0.3+期末成绩*0.7。

考察的总成绩直接等于期末成绩

必修课的考核方式必须为考试,选修课可以选择考试、考察任一考核方式。

1、输入:

包括课程、课程成绩两类信息。

课程信息包括:课程名称、课程性质、考核方式(可选,如果性质是必修课,考核方式可以没有)三个数据项。

课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式

课程性质输入项:必修、选修

考核方式输入选项:考试、考察

课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩

课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩

以上信息的相关约束:

1)平时成绩和期末成绩的权重默认为0.3、0.7

2)成绩是整数,不包含小数部分,成绩的取值范围是【0,100】

3)学号由8位数字组成

4)姓名不超过10个字符

5)课程名称不超过10个字符

6)不特别输入班级信息,班级号是学号的前6位。

2、输出:

输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程成绩平均分、单门课程总成绩平均分、班级所有课程总成绩平均分。

为避免误差,平均分的计算方法为累加所有符合条件的单个成绩,最后除以总数。

1)学生课程总成绩平均分按学号由低到高排序输出

格式:学号+英文空格+姓名+英文空格+总成绩平均分

如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"

2)单门课程成绩平均分分为三个分值:平时成绩平均分(可选)、期末考试平均分、总成绩平均分,按课程名称的字符顺序输出

格式:课程名称+英文空格+平时成绩平均分+英文空格+期末考试平均分+英文空格+总成绩平均分

如果某门课程没有任何成绩信息,输出:课程名称+英文空格+"has no grades yet"

3)班级所有课程总成绩平均分按班级由低到高排序输出

格式:班级号+英文空格+总成绩平均分

如果某个班级没有任何成绩信息,输出:班级名称+英文空格+ "has no grades yet"

异常情况:

1)如果解析某个成绩信息时,课程名称不在已输入的课程列表中,输出:学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"

2)如果解析某个成绩信息时,输入的成绩数量和课程的考核方式不匹配,输出:学号+英文空格+姓名+英文空格+": access mode mismatch"

以上两种情况如果同时出现,按第一种情况输出结果。

3)如果解析某个课程信息时,输入的课程性质和课程的考核方式不匹配,输出:课程名称+" : course type & access mode mismatch"

4)格式错误以及其他信息异常如成绩超出范围等,均按格式错误处理,输出"wrong format"

5)若出现重复的课程/成绩信息,只保留第一个课程信息,忽略后面输入的。

信息约束:

1)成绩平均分只取整数部分,小数部分丢弃

参考类图:


image.png

 

设计分析:

首先对输入数据进行区分。输入数据分为两种,一种是课程信息导入,一种是成绩信息导入,每条信息都只有一行。先对输入信息进行处理,课程信息与成绩信息最大的区别就是:课程信息第一个字符串是不超过10个字符的课程名称(包括中文字符和英文字符),而成绩信息第一个字符串一定是八个数字,由此可以将两种信息区分开。

接着对数据进行异常分析,判断数据的合理性,根据题目的要求,合理分析并处理异常的数据;

然后对输入数据进行储存。这里创建了三个动态数组ArrayList<Course> courseArrayList; ArrayList<Student> studentArrayList ;ArrayList<Score> scoreArrayList; 在判断信息无误后,将处理后的数据存入三个动态数组。

最后通过三个数组创建SelectCourse对象,并调用其方法对信息进行处理后输出。

类图如下:

SelectCourse类:选课系统,

Student类:创建学生对象;

ClassRoom类:创建班级对象;

ScoreComparator类:Score类成绩比较,实现Comparator接口;

Score抽象类:成绩对象;

InspectScore类:考察成绩对象,继承自Score类;

ExamScore类:考试成绩对象,继承自Score类;

Course类:课程类,创建课程对象;

import java.text.Collator;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        ArrayList<Course> courseArrayList = new ArrayList<>();
        ArrayList<Student> studentArrayList = new ArrayList<>();
        ArrayList<Score> scoreArrayList = new ArrayList<>();
        while (!s.equals("end")) {
            String[] strings = s.split(" ");
            if (strings.length >= 3 && strings[0].matches("[0-9]{8}") && strings[1].matches("\\S{1,10}") && strings[2].matches("\\S{1,10}")) {
                if (scoreIsRepeated(strings[0], strings[1], strings[2], studentArrayList, scoreArrayList)) {
                    Score score = null;
                    int f = 1;
                    for (int i = 3; i < strings.length; i++) {
                        if (!strings[i].matches("([1-9]?[0-9]|100)")) {
                            f = 0;
                            break;
                        }
                    }
                    if (f == 1) {
                        int t = 1;
                        int flag = 0;
                        for (Course course : courseArrayList) {
                            if (course.getName().equals(strings[2])) {
                                flag = 1;
                                if (course.getMode().equals("考试")) {
                                    if (strings.length == 5) {
                                        score = new ExamScore(strings[2], Integer.parseInt(strings[3]), Integer.parseInt(strings[4]));
                                    } else if (strings.length == 4){
                                        System.out.println(strings[0] + " " + strings[1] + " : access mode mismatch");
                                    }else {
                                        t = 0;
                                        System.out.println("wrong format");
                                    }
                                } else if (course.getMode().equals("考察")) {
                                    if (strings.length == 4) {
                                        score = new InspectScore(strings[2], -1, Integer.parseInt(strings[3]));
                                    } else if (strings.length == 5){
                                        System.out.println(strings[0] + " " + strings[1] + " : access mode mismatch");
                                    }else {
                                        t = 0;
                                        System.out.println("wrong format");
                                    }
                                } else if (course.getMode().equals("实验")) {
                                    if (Integer.parseInt(strings[3]) >= 4 && Integer.parseInt(strings[3]) <= 9) {
                                        if ((strings.length - 4) == Integer.parseInt(strings[3])) {
                                            int sum = 0;
                                            for (int i = 0; i < strings.length - 4; i++) {
                                                sum += Integer.parseInt(strings[4 + i]);
                                            }
                                            int grade = sum / Integer.parseInt(strings[3]);
                                            score = new ExperimentScore(strings[2], -2, grade);
                                        } else
                                            System.out.println(strings[0] + " " + strings[1] + " : access mode mismatch");
                                    } else {
                                        t = 0;
                                        System.out.println("wrong format");
                                    }
                                }
                            }
                        }
                        if (t == 1) {
                            scoreArrayList.add(score);
                            Student student = new Student(strings[0], strings[1]);
                            studentArrayList.add(student);
                        }
                        if (flag == 0) {
                            System.out.println(strings[2] + " does not exist");
                        }
                    } else
                        System.out.println("wrong format");
                }
            } else if (strings.length == 3 && strings[0].matches("\\S{1,10}") && (strings[1].matches("(选修|必修|实验)")) && (strings[2].matches("(考试|考察|实验)"))) {
                if (courseIsRepeated(strings[0], courseArrayList)) {
                    if (courseIsMatched(strings[1], strings[2])) {
                        Course course = new Course(strings[0], strings[1], strings[2]);
                        courseArrayList.add(course);
                    } else
                        System.out.println(strings[0] + " : course type & access mode mismatch");
                }
            } else if (strings.length == 2 && strings[0].matches("\\S{1,10}") && strings[1].equals("必修")) {
                if (courseIsRepeated(strings[0], courseArrayList)) {
                    Course course = new Course(strings[0], strings[1], "考试");
                    courseArrayList.add(course);
                }
            } else {
                System.out.println("wrong format");
            }
            s = input.nextLine();
        }
        SelectCourse selectCourse = new SelectCourse(courseArrayList, studentArrayList, scoreArrayList);
        for (Student student : selectCourse.student_sort()) {
            if (student.getScore() == -1)
                System.out.println(student.getNumber() + " " + student.getName() + " did not take any exams");
            else
                System.out.println(student.getNumber() + " " + student.getName() + " " + student.getScore());
        }
        for (Map.Entry<Score, Integer> entry : selectCourse.scoreIntegerMap().entrySet()) {
            if (entry.getKey().getPerformance() == -1 && entry.getKey().getGrade() == -1 && entry.getKey().allGrade() == -1) {
                System.out.println(entry.getKey().getCourse() + " has no grades yet");
            } else if (entry.getKey().getPerformance() == -1)
                System.out.println(entry.getKey().getCourse() + " " + entry.getKey().getGrade() + " " + entry.getValue());
            else if (entry.getKey().getPerformance() == -2)
                System.out.println(entry.getKey().getCourse() + " " + entry.getValue());
            else
                System.out.println(entry.getKey().getCourse() + " " + entry.getKey().getPerformance() + " " + entry.getKey().getGrade() + " " + entry.getValue());
        }
        for (ClassRoom classRoom : selectCourse.classHashSet()) {
            if (classRoom.getScore() == -1)
                System.out.println(classRoom.getNum() + " has no grades yet");
            else
                System.out.println(classRoom.getNum() + " " + classRoom.getScore());
        }
    }

    public static boolean courseIsMatched(String character, String mode) {
        if (character.equals("实验") && !mode.equals("实验"))
            return false;
        else if (character.equals("必修") && mode.matches("(考察|实验)"))
            return false;
        else if (character.equals("选修") && mode.matches("(实验)"))
            return false;
        else
            return true;
    }

    public static boolean courseIsRepeated(String name, ArrayList<Course> courseArrayList) {
        int flag = 1;
        for (Course course : courseArrayList) {
            if (name.equals(course.getName())) {
                flag = 0;
                break;
            }
        }
        return flag != 0;
    }

    public static boolean scoreIsRepeated(String number, String name, String course, ArrayList<Student> studentArrayList, ArrayList<Score> scoreArrayList) {
        int flag = 1;
        int i = 0;
        for (Student student : studentArrayList) {
            if (student.getNumber().equals(number) && student.getName().equals(name)) {
                i = studentArrayList.indexOf(student);
                flag = 0;
            }
        }
        if (flag == 1)
            return true;
        else {
            if (scoreArrayList.get(i) == null) {
                return true;
            } else {
                return !scoreArrayList.get(i).getCourse().equals(course);
            }
        }
    }
}

class SelectCourse {
    private ArrayList<Course> courseArrayList;
    private ArrayList<Student> studentArrayList;
    private ArrayList<Score> scoreArrayList;

    public SelectCourse(ArrayList<Course> courseArrayList, ArrayList<Student> studentArrayList, ArrayList<Score> scoreArrayList) {
        this.courseArrayList = courseArrayList;
        this.studentArrayList = studentArrayList;
        this.scoreArrayList = scoreArrayList;
    }

    public ArrayList<ClassRoom> classHashSet() {
        ArrayList<ClassRoom> classArrayList = new ArrayList<>();
        ArrayList<ClassRoom> classHashSet = new ArrayList<>();
        ArrayList<Student> studentHashSet = student_sort();

        for (Student student : studentHashSet) {
            int flag = 1;
            String num = student.getNumber().substring(0, 6);
            ClassRoom classRoom = new ClassRoom(num);
            for (ClassRoom classRoom0 : classHashSet) {
                if (classRoom0.isSame(classRoom))
                    flag = 0;
            }
            if (flag == 1)
                classHashSet.add(classRoom);
            ClassRoom classRoom1 = new ClassRoom(student.getScore(), num);
            classArrayList.add(classRoom1);
        }
        for (ClassRoom classRoom : classHashSet) {
            int score = 0;
            int count = 0;
            for (ClassRoom classRoom1 : classArrayList) {
                if (classRoom.isSame(classRoom1)) {
                    if (classRoom1.getScore() != -1) {
                        score += classRoom1.getScore();
                        count++;
                    }
                }
            }
            if (count != 0) {
                score = score / count;
                classRoom.setScore(score);
            } else {
                classRoom.setScore(-1);
            }
        }
        classHashSet.sort(ClassRoom::compareTo);
        return classHashSet;
    }

    public Map<Score, Integer> scoreIntegerMap() {
        Map<Score, Integer> scoreIntegerMap = new TreeMap<>(new ScoreComparator());
        for (Course course : courseArrayList) {
            int aver_grade = 0;
            int aver_performance = 0;
            int allGrade = 0;
            int count = 0;
            for (Score score : scoreArrayList) {
                if (score != null) {
                    if (score.getCourse().equals(course.getName())) {
                        aver_performance += score.getPerformance();
                        aver_grade += score.getGrade();
                        allGrade += score.allGrade();
                        count++;
                    }
                }

            }
            if (count != 0) {
                aver_grade = aver_grade / count;
                aver_performance = aver_performance / count;
                allGrade = allGrade / count;
                Score score = new ExamScore(course.getName(), aver_performance, aver_grade);
                scoreIntegerMap.put(score, allGrade);
            } else {
                Score score = new ExamScore(course.getName(), -1, -1);
                scoreIntegerMap.put(score, -1);
            }
        }
        return scoreIntegerMap;
    }

    public ArrayList<Student> student_sort() {
        HashSet<Student> studentHashSet = new HashSet<>();
        for (Student student : studentArrayList) {
            int flag = 1;
            for (Student student1 : studentHashSet) {
                if (student.isSame(student1)) {
                    flag = 0;
                }
            }
            if (flag == 1)
                studentHashSet.add(student);
        }
        ArrayList<Student> student_sort = new ArrayList<>();
        for (Student student : studentHashSet) {
            int score = 0;
            int count = 0;
            for (int i = 0; i < studentArrayList.size(); i++) {
                if (student.isSame(studentArrayList.get(i))) {
                    if (scoreArrayList.get(i) != null) {
                        if (scoreArrayList.get(i).allGrade() != -1) {
                            score += scoreArrayList.get(i).allGrade();
                            count++;
                        }
                    }
                }
            }
            if (count != 0) {
                score = score / count;
                student.setScore(score);
            } else {
                student.setScore(-1);
            }
            student_sort.add(student);
        }
        student_sort.sort(Student::compareTo);
        return student_sort;
    }
}


class Student implements Comparable<Student> {
    private final String number;
    private final String name;

    private int score;

    public Student(String number, String name) {
        this.number = number;
        this.name = name;
    }

    public boolean isSame(Student student) {
        return this.number.equals(student.getNumber()) && this.name.equals(student.getName());
    }

    public int getScore() {
        return score;
    }

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

    public String getNumber() {
        return number;
    }

    public String getName() {
        return name;
    }

    @Override
    public int compareTo(Student o) {
        return Integer.compare(number.compareTo(o.getNumber()), 0);
    }
}

class ClassRoom implements Comparable<ClassRoom> {
    private int score;
    private final String num;

    public ClassRoom(String num) {
        this.num = num;
    }

    public ClassRoom(int score, String num) {
        this.score = score;
        this.num = num;
    }

    public boolean isSame(ClassRoom classRoom) {
        return this.num.equals(classRoom.getNum());
    }

    public int getScore() {
        return score;
    }

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

    public String getNum() {
        return num;
    }

    @Override
    public int compareTo(ClassRoom o) {
        return num.compareTo(o.getNum());
    }
}

class ScoreComparator implements Comparator<Score> {

    @Override
    public int compare(Score o1, Score o2) {
        Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA);
        return compare.compare(o1.getCourse(), o2.getCourse());
    }
}

abstract class Score {
    private final String course;
    private int grade;
    private int performance;

    public Score(String course, int performance, int grade) {
        this.course = course;
        this.grade = grade;
        this.performance = performance;
    }

    public String getCourse() {
        return course;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public int getPerformance() {
        return performance;
    }

    public abstract int allGrade();
}

class InspectScore extends Score {

    public InspectScore(String course, int performance, int grade) {
        super(course, performance, grade);
    }

    public int allGrade() {
        return super.getGrade();
    }
}

class ExamScore extends Score {

    public ExamScore(String course, int performance, int grade) {
        super(course, performance, grade);
    }

    public int allGrade() {
        return (int) (super.getGrade() * 0.7 + super.getPerformance() * 0.3);
    }
}

class ExperimentScore extends Score {

    public ExperimentScore(String course, int performance, int grade) {
        super(course, performance, grade);
    }

    public int allGrade() {
        return super.getGrade();
    }
}

class Course {
    private final String name;
    private final String character;//性质
    private final String mode;//考核方式

    public Course(String name, String character, String mode) {
        this.name = name;
        this.character = character;
        this.mode = mode;
    }

    public String getName() {
        return name;
    }

    public String getMode() {
        return mode;
    }
}

 

课程成绩统计程序-2

课程成绩统计程序-2在第一次的基础上增加了实验课,以下加粗字体显示为本次新增的内容。

某高校课程从性质上分为:必修课、选修课、实验课,从考核方式上分为:考试、考察、实验

考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重0.3,期末成绩权重0.7,总成绩=平时成绩*0.3+期末成绩*0.7。

考察的总成绩直接等于期末成绩

实验的总成绩等于课程每次实验成绩的平均分

必修课的考核方式必须为考试,选修课可以选择考试、考察任一考核方式。实验课的成绩必须为实验。

1、输入:

包括课程、课程成绩两类信息。

课程信息包括:课程名称、课程性质、考核方式(可选,如果性质是必修课,考核方式可以没有)三个数据项。

课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式

课程性质输入项:必修、选修、实验

考核方式输入选项:考试、考察、实验

考试/考查课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩

考试/考查课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩

实验课程成绩信息包括:学号、姓名、课程名称、实验次数、每次成绩

实验次数至少4次,不超过9次

实验课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+实验次数+英文空格+第一次实验成绩+...+英文空格+最后一次实验成绩

以上信息的相关约束:

1)平时成绩和期末成绩的权重默认为0.3、0.7

2)成绩是整数,不包含小数部分,成绩的取值范围是【0,100】

3)学号由8位数字组成

4)姓名不超过10个字符

5)课程名称不超过10个字符

6)不特别输入班级信息,班级号是学号的前6位。

2、输出:

输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程成绩平均分、单门课程总成绩平均分、班级所有课程总成绩平均分。

为避免误差,平均分的计算方法为累加所有符合条件的单个成绩,最后除以总数。

1)学生课程总成绩平均分按学号由低到高排序输出

格式:学号+英文空格+姓名+英文空格+总成绩平均分

如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"

2)单门课程成绩平均分分为三个分值:平时成绩平均分(可选)、期末考试平均分、总成绩平均分,按课程名称的字符顺序输出

考试/考察课程成绩格式:课程名称+英文空格+平时成绩平均分+英文空格+期末考试平均分+英文空格+总成绩平均分

实验课成绩格式:课程名称+英文空格+总成绩平均分

如果某门课程没有任何成绩信息,输出:课程名称+英文空格+"has no grades yet"

3)班级所有课程总成绩平均分按班级由低到高排序输出

格式:班级号+英文空格+总成绩平均分

如果某个班级没有任何成绩信息,输出:班级名称+英文空格+ "has no grades yet"

异常情况:

1)如果解析某个成绩信息时,课程名称不在已输入的课程列表中,输出:学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"

2)如果解析某个成绩信息时,输入的成绩数量和课程的考核方式不匹配,输出:学号+英文空格+姓名+英文空格+": access mode mismatch"

以上两种情况如果同时出现,按第一种情况输出结果。

3)如果解析某个课程信息时,输入的课程性质和课程的考核方式不匹配,输出:课程名称+" : course type & access mode mismatch"

4)格式错误以及其他信息异常如成绩超出范围等,均按格式错误处理,输出"wrong format"

5)若出现重复的课程/成绩信息,只保留第一个课程信息,忽略后面输入的。

信息约束:

1)成绩平均分只取整数部分,小数部分丢弃

参考类图(与第一次相同,其余内容自行补充):


e724fa4193aa9ee32e78a68cd96fd6df_22401e04-c501-4b28-bb65-dabe39d374e7.png

设计分析:

这次迭代主要在信息输入上,这次多了实验课,而且实验课的成绩形式也与之前不同,实验次数至少4次,不超过9次,每次实验都有成绩,最终成绩为每次成绩的平均值。

这次改进在判断信息合理性上加入了实验课,并且还加入了判断试验次数与实验成绩个数是否匹配,如果信息无误,则直接保存最终成绩。

import java.text.Collator;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        ArrayList<Course> courseArrayList = new ArrayList<>();
        ArrayList<Student> studentArrayList = new ArrayList<>();
        ArrayList<Score> scoreArrayList = new ArrayList<>();
        while (!s.equals("end")) {
            String[] strings = s.split(" ");
            if (strings.length >= 3 && strings[0].matches("[0-9]{8}") && strings[1].matches("\\S{1,10}") && strings[2].matches("\\S{1,10}")) {
                if (scoreIsRepeated(strings[0], strings[1], strings[2], studentArrayList, scoreArrayList)) {
                    Score score = null;
                    int f = 1;
                    for (int i = 3; i < strings.length; i++) {
                        if (!strings[i].matches("([1-9]?[0-9]|100)")) {
                            f = 0;
                            break;
                        }
                    }
                    if (f == 1) {
                        int t = 1;
                        int flag = 0;
                        for (Course course : courseArrayList) {
                            if (course.getName().equals(strings[2])) {
                                flag = 1;
                                if (course.getMode().equals("考试")) {
                                    if (strings.length == 5) {
                                        score = new ExamScore(strings[2], Integer.parseInt(strings[3]), Integer.parseInt(strings[4]));
                                    } else if (strings.length == 4){
                                        System.out.println(strings[0] + " " + strings[1] + " : access mode mismatch");
                                    }else {
                                        t = 0;
                                        System.out.println("wrong format");
                                    }
                                } else if (course.getMode().equals("考察")) {
                                    if (strings.length == 4) {
                                        score = new InspectScore(strings[2], -1, Integer.parseInt(strings[3]));
                                    } else if (strings.length == 5){
                                        System.out.println(strings[0] + " " + strings[1] + " : access mode mismatch");
                                    }else {
                                        t = 0;
                                        System.out.println("wrong format");
                                    }
                                } else if (course.getMode().equals("实验")) {
                                    if (Integer.parseInt(strings[3]) >= 4 && Integer.parseInt(strings[3]) <= 9) {
                                        if ((strings.length - 4) == Integer.parseInt(strings[3])) {
                                            int sum = 0;
                                            for (int i = 0; i < strings.length - 4; i++) {
                                                sum += Integer.parseInt(strings[4 + i]);
                                            }
                                            int grade = sum / Integer.parseInt(strings[3]);
                                            score = new ExperimentScore(strings[2], -2, grade);
                                        } else
                                            System.out.println(strings[0] + " " + strings[1] + " : access mode mismatch");
                                    } else {
                                        t = 0;
                                        System.out.println("wrong format");
                                    }
                                }
                            }
                        }
                        if (t == 1) {
                            scoreArrayList.add(score);
                            Student student = new Student(strings[0], strings[1]);
                            studentArrayList.add(student);
                        }
                        if (flag == 0) {
                            System.out.println(strings[2] + " does not exist");
                        }
                    } else
                        System.out.println("wrong format");
                }
            } else if (strings.length == 3 && strings[0].matches("\\S{1,10}") && (strings[1].matches("(选修|必修|实验)")) && (strings[2].matches("(考试|考察|实验)"))) {
                if (courseIsRepeated(strings[0], courseArrayList)) {
                    if (courseIsMatched(strings[1], strings[2])) {
                        Course course = new Course(strings[0], strings[1], strings[2]);
                        courseArrayList.add(course);
                    } else
                        System.out.println(strings[0] + " : course type & access mode mismatch");
                }
            } else if (strings.length == 2 && strings[0].matches("\\S{1,10}") && strings[1].equals("必修")) {
                if (courseIsRepeated(strings[0], courseArrayList)) {
                    Course course = new Course(strings[0], strings[1], "考试");
                    courseArrayList.add(course);
                }
            } else {
                System.out.println("wrong format");
            }
            s = input.nextLine();
        }
        SelectCourse selectCourse = new SelectCourse(courseArrayList, studentArrayList, scoreArrayList);
        for (Student student : selectCourse.student_sort()) {
            if (student.getScore() == -1)
                System.out.println(student.getNumber() + " " + student.getName() + " did not take any exams");
            else
                System.out.println(student.getNumber() + " " + student.getName() + " " + student.getScore());
        }
        for (Map.Entry<Score, Integer> entry : selectCourse.scoreIntegerMap().entrySet()) {
            if (entry.getKey().getPerformance() == -1 && entry.getKey().getGrade() == -1 && entry.getKey().allGrade() == -1) {
                System.out.println(entry.getKey().getCourse() + " has no grades yet");
            } else if (entry.getKey().getPerformance() == -1)
                System.out.println(entry.getKey().getCourse() + " " + entry.getKey().getGrade() + " " + entry.getValue());
            else if (entry.getKey().getPerformance() == -2)
                System.out.println(entry.getKey().getCourse() + " " + entry.getValue());
            else
                System.out.println(entry.getKey().getCourse() + " " + entry.getKey().getPerformance() + " " + entry.getKey().getGrade() + " " + entry.getValue());
        }
        for (ClassRoom classRoom : selectCourse.classHashSet()) {
            if (classRoom.getScore() == -1)
                System.out.println(classRoom.getNum() + " has no grades yet");
            else
                System.out.println(classRoom.getNum() + " " + classRoom.getScore());
        }
    }

    public static boolean courseIsMatched(String character, String mode) {
        if (character.equals("实验") && !mode.equals("实验"))
            return false;
        else if (character.equals("必修") && mode.matches("(考察|实验)"))
            return false;
        else if (character.equals("选修") && mode.matches("(实验)"))
            return false;
        else
            return true;
    }

    public static boolean courseIsRepeated(String name, ArrayList<Course> courseArrayList) {
        int flag = 1;
        for (Course course : courseArrayList) {
            if (name.equals(course.getName())) {
                flag = 0;
                break;
            }
        }
        return flag != 0;
    }

    public static boolean scoreIsRepeated(String number, String name, String course, ArrayList<Student> studentArrayList, ArrayList<Score> scoreArrayList) {
        int flag = 1;
        int i = 0;
        for (Student student : studentArrayList) {
            if (student.getNumber().equals(number) && student.getName().equals(name)) {
                i = studentArrayList.indexOf(student);
                flag = 0;
            }
        }
        if (flag == 1)
            return true;
        else {
            if (scoreArrayList.get(i) == null) {
                return true;
            } else {
                return !scoreArrayList.get(i).getCourse().equals(course);
            }
        }
    }
}

class SelectCourse {
    private ArrayList<Course> courseArrayList;
    private ArrayList<Student> studentArrayList;
    private ArrayList<Score> scoreArrayList;

    public SelectCourse(ArrayList<Course> courseArrayList, ArrayList<Student> studentArrayList, ArrayList<Score> scoreArrayList) {
        this.courseArrayList = courseArrayList;
        this.studentArrayList = studentArrayList;
        this.scoreArrayList = scoreArrayList;
    }

    public ArrayList<ClassRoom> classHashSet() {
        ArrayList<ClassRoom> classArrayList = new ArrayList<>();
        ArrayList<ClassRoom> classHashSet = new ArrayList<>();
        ArrayList<Student> studentHashSet = student_sort();

        for (Student student : studentHashSet) {
            int flag = 1;
            String num = student.getNumber().substring(0, 6);
            ClassRoom classRoom = new ClassRoom(num);
            for (ClassRoom classRoom0 : classHashSet) {
                if (classRoom0.isSame(classRoom))
                    flag = 0;
            }
            if (flag == 1)
                classHashSet.add(classRoom);
            ClassRoom classRoom1 = new ClassRoom(student.getScore(), num);
            classArrayList.add(classRoom1);
        }
        for (ClassRoom classRoom : classHashSet) {
            int score = 0;
            int count = 0;
            for (ClassRoom classRoom1 : classArrayList) {
                if (classRoom.isSame(classRoom1)) {
                    if (classRoom1.getScore() != -1) {
                        score += classRoom1.getScore();
                        count++;
                    }
                }
            }
            if (count != 0) {
                score = score / count;
                classRoom.setScore(score);
            } else {
                classRoom.setScore(-1);
            }
        }
        classHashSet.sort(ClassRoom::compareTo);
        return classHashSet;
    }

    public Map<Score, Integer> scoreIntegerMap() {
        Map<Score, Integer> scoreIntegerMap = new TreeMap<>(new ScoreComparator());
        for (Course course : courseArrayList) {
            int aver_grade = 0;
            int aver_performance = 0;
            int allGrade = 0;
            int count = 0;
            for (Score score : scoreArrayList) {
                if (score != null) {
                    if (score.getCourse().equals(course.getName())) {
                        aver_performance += score.getPerformance();
                        aver_grade += score.getGrade();
                        allGrade += score.allGrade();
                        count++;
                    }
                }

            }
            if (count != 0) {
                aver_grade = aver_grade / count;
                aver_performance = aver_performance / count;
                allGrade = allGrade / count;
                Score score = new ExamScore(course.getName(), aver_performance, aver_grade);
                scoreIntegerMap.put(score, allGrade);
            } else {
                Score score = new ExamScore(course.getName(), -1, -1);
                scoreIntegerMap.put(score, -1);
            }
        }
        return scoreIntegerMap;
    }

    public ArrayList<Student> student_sort() {
        HashSet<Student> studentHashSet = new HashSet<>();
        for (Student student : studentArrayList) {
            int flag = 1;
            for (Student student1 : studentHashSet) {
                if (student.isSame(student1)) {
                    flag = 0;
                }
            }
            if (flag == 1)
                studentHashSet.add(student);
        }
        ArrayList<Student> student_sort = new ArrayList<>();
        for (Student student : studentHashSet) {
            int score = 0;
            int count = 0;
            for (int i = 0; i < studentArrayList.size(); i++) {
                if (student.isSame(studentArrayList.get(i))) {
                    if (scoreArrayList.get(i) != null) {
                        if (scoreArrayList.get(i).allGrade() != -1) {
                            score += scoreArrayList.get(i).allGrade();
                            count++;
                        }
                    }
                }
            }
            if (count != 0) {
                score = score / count;
                student.setScore(score);
            } else {
                student.setScore(-1);
            }
            student_sort.add(student);
        }
        student_sort.sort(Student::compareTo);
        return student_sort;
    }
}


class Student implements Comparable<Student> {
    private final String number;
    private final String name;

    private int score;

    public Student(String number, String name) {
        this.number = number;
        this.name = name;
    }

    public boolean isSame(Student student) {
        return this.number.equals(student.getNumber()) && this.name.equals(student.getName());
    }

    public int getScore() {
        return score;
    }

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

    public String getNumber() {
        return number;
    }

    public String getName() {
        return name;
    }

    @Override
    public int compareTo(Student o) {
        return Integer.compare(number.compareTo(o.getNumber()), 0);
    }
}

class ClassRoom implements Comparable<ClassRoom> {
    private int score;
    private final String num;

    public ClassRoom(String num) {
        this.num = num;
    }

    public ClassRoom(int score, String num) {
        this.score = score;
        this.num = num;
    }

    public boolean isSame(ClassRoom classRoom) {
        return this.num.equals(classRoom.getNum());
    }

    public int getScore() {
        return score;
    }

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

    public String getNum() {
        return num;
    }

    @Override
    public int compareTo(ClassRoom o) {
        return num.compareTo(o.getNum());
    }
}

class ScoreComparator implements Comparator<Score> {

    @Override
    public int compare(Score o1, Score o2) {
        Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA);
        return compare.compare(o1.getCourse(), o2.getCourse());
    }
}

abstract class Score {
    private final String course;
    private int grade;
    private int performance;

    public Score(String course, int performance, int grade) {
        this.course = course;
        this.grade = grade;
        this.performance = performance;
    }

    public String getCourse() {
        return course;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public int getPerformance() {
        return performance;
    }

    public abstract int allGrade();
}

class InspectScore extends Score {

    public InspectScore(String course, int performance, int grade) {
        super(course, performance, grade);
    }

    public int allGrade() {
        return super.getGrade();
    }
}

class ExamScore extends Score {

    public ExamScore(String course, int performance, int grade) {
        super(course, performance, grade);
    }

    public int allGrade() {
        return (int) (super.getGrade() * 0.7 + super.getPerformance() * 0.3);
    }
}

class ExperimentScore extends Score {

    public ExperimentScore(String course, int performance, int grade) {
        super(course, performance, grade);
    }

    public int allGrade() {
        return super.getGrade();
    }
}

class Course {
    private final String name;
    private final String character;//性质
    private final String mode;//考核方式

    public Course(String name, String character, String mode) {
        this.name = name;
        this.character = character;
        this.mode = mode;
    }

    public String getName() {
        return name;
    }

    public String getMode() {
        return mode;
    }
}

课程成绩统计程序-3

课程成绩统计程序-3在第二次的基础上修改了计算总成绩的方式,

要求:修改类结构,将成绩类的继承关系改为组合关系,成绩信息由课程成绩类和分项成绩类组成,课程成绩类组合分项成绩类,分项成绩类由成绩分值和权重两个属性构成。

完成课程成绩统计程序-2、3两次程序后,比较继承和组合关系的区别。思考一下哪一种关系运用上更灵活,更能够适应变更。

题目最后的参考类图未做修改,大家根据要求自行调整,以下内容加粗字体显示的内容为本次新增的内容。

某高校课程从性质上分为:必修课、选修课、实验课,从考核方式上分为:考试、考察、实验。

考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重0.3,期末成绩权重0.7,总成绩=平时成绩*0.3+期末成绩*0.7。

考察的总成绩直接等于期末成绩

实验的总成绩等于课程每次实验成绩乘以权重后累加而得。

课程权重值在录入课程信息时输入。(注意:所有分项成绩的权重之和应当等于1)

必修课的考核方式必须为考试,选修课可以选择考试、考察任一考核方式。实验课的成绩必须为实验。

1、输入:

包括课程、课程成绩两类信息。

课程信息包括:课程名称、课程性质、考核方式、分项成绩数量、每个分项成绩的权重。

考试课信息格式:课程名称+英文空格+课程性质+英文空格+考核方式+英文空格+平时成绩的权重+英文空格+期末成绩的权重

考察课信息格式:课程名称+英文空格+课程性质+英文空格+考核方式

实验课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式+英文空格+分项成绩数量n+英文空格+分项成绩1的权重+英文空格+。。。+英文空格+分项成绩n的权重

实验次数至少4次,不超过9次

课程性质输入项:必修、选修、实验

考核方式输入选项:考试、考察、实验

考试/考查课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩

考试/考查课程成绩信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩

实验课程成绩信息包括:学号、姓名、课程名称、每次成绩{在系列-2的基础上去掉了(实验次数),实验次数要和实验课程信息中输入的分项成绩数量保持一致}

实验课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+第一次实验成绩+...+英文空格+最后一次实验成绩

以上信息的相关约束:

1)成绩是整数,不包含小数部分,成绩的取值范围是【0,100】

2)学号由8位数字组成

3)姓名不超过10个字符

4)课程名称不超过10个字符

5)不特别输入班级信息,班级号是学号的前6位。

2、输出:

输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程总成绩平均分、班级所有课程总成绩平均分。

为避免四舍五入误差,

计算单个成绩时,分项成绩乘以权重后要保留小数位,计算总成绩时,累加所有分项成绩的权重分以后,再去掉小数位。

学生总成绩/整个班/课程平均分的计算方法为累加所有符合条件的单个成绩,最后除以总数。

1)学生课程总成绩平均分按学号由低到高排序输出

格式:学号+英文空格+姓名+英文空格+总成绩平均分

如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"

2)单门课程成绩按课程名称的字符顺序输出

课程成绩输出格式:课程名称+英文空格+总成绩平均分

如果某门课程没有任何成绩信息,输出:课程名称+英文空格+"has no grades yet"

3)班级所有课程总成绩平均分按班级由低到高排序输出

格式:班级号+英文空格+总成绩平均分

如果某个班级没有任何成绩信息,输出:班级名称+英文空格+ "has no grades yet"

异常情况:

1)如果解析某个成绩信息时,课程名称不在已输入的课程列表中,输出:学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"

2)如果解析某个成绩信息时,输入的成绩数量和课程的考核方式不匹配,输出:学号+英文空格+姓名+英文空格+": access mode mismatch"

以上两种情况如果同时出现,按第一种情况输出结果。

3)如果解析某个课程信息时,输入的课程性质和课程的考核方式不匹配,输出:课程名称+" : course type & access mode mismatch"

4)格式错误以及其他信息异常如成绩超出范围等,均按格式错误处理,输出"wrong format"

5)若出现重复的课程/成绩信息,只保留第一个课程信息,忽略后面输入的。

6)如果解析实验课程信息时,输入的分项成绩数量值和分项成绩权重的个数不匹配,输出:课程名称+" : number of scores does not match"

7)如果解析考试课、实验课时,分项成绩权重值的总和不等于1,输出:课程名称+" : weight value error"

信息约束:

1)成绩平均分只取整数部分,小数部分丢弃

参考类图(与第一次相同,其余内容自行补充):

fdada4ca193119ee30531ab82ffebbfa_9dbcf4e8-1627-4cf6-8764-cccf44947e2a.png

设计分析:

这次迭代修改了成绩的计算方式,输入的数据比之前多了权重这一数据。

成绩类的继承关系改为组合关系,成绩信息由课程成绩类和分项成绩类组成,课程成绩类组合分项成绩类,分项成绩类由成绩分值和权重两个属性构成。

 

PartScore类:分项成绩类,由成绩分值和权重两个属性构成;

Score类:由PartScore类动态数组和课程名称两个属性构成;

import java.text.Collator;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        ArrayList<Course> courseArrayList = new ArrayList<>();
        ArrayList<Student> studentArrayList = new ArrayList<>();
        ArrayList<Score> scoreArrayList = new ArrayList<>();
        while (!s.equals("end")) {
            String[] strings = s.split(" ");
            if (strings.length >= 3 && strings[0].matches("[0-9]{8}") && strings[1].matches("\\S{1,10}") && strings[2].matches("\\S{1,10}")) {
                if (scoreIsRepeated(strings[0], strings[1], strings[2], studentArrayList, scoreArrayList)) {
                    ArrayList<PartScore> partScoreArrayList = new ArrayList<>();
                    //Score score = null;
                    int f = 1;
                    for (int i = 3; i < strings.length; i++) {
                        if (!strings[i].matches("([1-9]?[0-9]|100)")) {
                            f = 0;
                            break;
                        }
                    }
                    if (f == 1) {
                        int t = 1;
                        int flag = 0;
                        for (Course course : courseArrayList) {
                            if (course.getName().equals(strings[2])) {
                                flag = 1;
                                if (course.getMode().equals("考试")) {
                                    if (strings.length == 5) {
                                        PartScore partScore1 = new PartScore(strings[2],Integer.parseInt(strings[3]),course.getWeights().get(0));
                                        partScoreArrayList.add(partScore1);
                                        PartScore partScore2 = new PartScore(strings[2],Integer.parseInt(strings[4]),course.getWeights().get(1));
                                        partScoreArrayList.add(partScore2);
                                        //score = new ExamScore(strings[2], Integer.parseInt(strings[3]), Integer.parseInt(strings[4]));
                                    } else {
                                        System.out.println(strings[0] + " " + strings[1] + " : access mode mismatch");
                                    }
                                } else if (course.getMode().equals("考察")) {
                                    if (strings.length == 4) {
                                        PartScore partScore1 = new PartScore(strings[2],Integer.parseInt(strings[3]),course.getWeights().get(0));
                                        partScoreArrayList.add(partScore1);
                                        //score = new InspectScore(strings[2], -1, Integer.parseInt(strings[3]));
                                    } else {
                                        System.out.println(strings[0] + " " + strings[1] + " : access mode mismatch");
                                    }
                                } else if (course.getMode().equals("实验")) {

                                        if (course.getWeights().size() == strings.length-3) {
                                            for (int i = 0; i < strings.length - 3; i++) {
                                                PartScore partScore1 = new PartScore(strings[2],Integer.parseInt(strings[3 + i]),course.getWeights().get(i));
                                                partScoreArrayList.add(partScore1);
                                            }
                                            //score = new ExperimentScore(strings[2], -2, grade);
                                        } else
                                            System.out.println(strings[0] + " " + strings[1] + " : access mode mismatch");
                                }
                            }
                        }
                        if (t == 1) {
                            Score score1 = new Score(strings[2],partScoreArrayList);
                            scoreArrayList.add(score1);
                            Student student = new Student(strings[0], strings[1]);
                            studentArrayList.add(student);
                        }
                        if (flag == 0) {
                            System.out.println(strings[2] + " does not exist");
                        }
                    } else
                        System.out.println("wrong format");
                }
            }else if(strings.length == 3 && strings[0].matches("\\S{1,10}") && (strings[1].matches("(选修|必修|实验)")) && (strings[2].matches("(考试|考察|实验)"))){
                if (courseIsRepeated(strings[0], courseArrayList)) {
                    if (strings[2].equals("考试")) {
                        if (courseIsMatched(strings[1], strings[2])) {
                            ArrayList<Float> weights = new ArrayList<>();
                            weights.add(0.3F);
                            weights.add(0.7F);
                            Course course = new Course(strings[0], strings[1], strings[2], weights);
                            courseArrayList.add(course);

                        } else
                            System.out.println(strings[0] + " : course type & access mode mismatch");
                    }
                    if (strings[2].equals("考察")) {
                        if (courseIsMatched(strings[1], strings[2])) {
                            ArrayList<Float> weights = new ArrayList<>();
                            weights.add(1.0F);
                            Course course = new Course(strings[0], strings[1], strings[2], weights);
                            courseArrayList.add(course);

                        } else
                            System.out.println(strings[0] + " : course type & access mode mismatch");
                    }
                }
            }
            else if (strings.length > 3 && strings[0].matches("\\S{1,10}") && (strings[1].matches("(选修|必修|实验)")) && (strings[2].matches("(考试|考察|实验)"))) {
                if (courseIsRepeated(strings[0], courseArrayList)) {
                    if(strings[2].equals("考试")){
                        if (courseIsMatched(strings[1], strings[2])) {
                                ArrayList<Float> weights = new ArrayList<>();
                                weights.add(Float.parseFloat(strings[3]));
                                weights.add(Float.parseFloat(strings[4]));
                                float sum = Float.parseFloat(strings[3])+Float.parseFloat(strings[4]);
                                if(sum==1) {
                                    Course course = new Course(strings[0], strings[1], strings[2], weights);
                                    courseArrayList.add(course);
                                }else
                                    System.out.println(strings[0] + " : weight value error");

                        } else
                            System.out.println(strings[0] + " : course type & access mode mismatch");
                    }
                    if(strings[2].equals("考察")){
                        if (courseIsMatched(strings[1], strings[2])) {
                            ArrayList<Float> weights = new ArrayList<>();
                            weights.add(Float.parseFloat(strings[3]));
                            if(Float.parseFloat(strings[3])==1) {
                                Course course = new Course(strings[0], strings[1], strings[2], weights);
                                courseArrayList.add(course);
                            }else
                                System.out.println(strings[0] + " : weight value error");

                        } else
                            System.out.println(strings[0] + " : course type & access mode mismatch");
                    }
                    if(strings[2].equals("实验")) {
                        if (Integer.parseInt(strings[3]) >= 4 && Integer.parseInt(strings[3]) <= 9) {
                            if (courseIsMatched(strings[1], strings[2])) {
                                int n = Integer.parseInt(strings[3]);
                                if (n + 4 == strings.length) {
                                    ArrayList<Float> weights = new ArrayList<>();
                                    float sum = 0;
                                    for (int i = 0; i < n; i++) {
                                        Float weight = Float.parseFloat(strings[4 + i]);
                                        sum += weight;
                                        weights.add(weight);
                                    }
                                    if (sum != 1) {
                                        System.out.println(strings[0] + " : weight value error");
                                    } else {
                                        Course course = new Course(strings[0], strings[1], strings[2], weights);
                                        courseArrayList.add(course);
                                    }
                                } else
                                    System.out.println(strings[0] + " : number of scores does not match");
                            } else
                                System.out.println(strings[0] + " : course type & access mode mismatch");
                        } else {
                            System.out.println("wrong format");
                        }
                    }
                }
            } else if (strings.length == 2 && strings[0].matches("\\S{1,10}") && strings[1].equals("必修")) {
                if (courseIsRepeated(strings[0], courseArrayList)) {
                    ArrayList<Float> weights = new ArrayList<>();
                    weights.add(0.3F);
                    weights.add(0.7F);
                    Course course = new Course(strings[0], strings[1], "考试",weights);
                    courseArrayList.add(course);
                }
            } else {
                System.out.println("wrong format");
            }
            s = input.nextLine();
        }
        SelectCourse selectCourse = new SelectCourse(courseArrayList, studentArrayList, scoreArrayList);
        for (Student student : selectCourse.student_sort()) {
            if (student.getScore() == -1)
                System.out.println(student.getNumber() + " " + student.getName() + " did not take any exams");
            else
                System.out.println(student.getNumber() + " " + student.getName() + " " + student.getScore());
        }
        for (Map.Entry<String, Integer> entry : selectCourse.scoreIntegerMap().entrySet()) {
            if (entry.getValue() == -1) {
                System.out.println(entry.getKey() + " has no grades yet");
            } else
                System.out.println(entry.getKey() + " " + entry.getValue());

        }
        for (ClassRoom classRoom : selectCourse.classHashSet()) {
            if (classRoom.getScore() == -1)
                System.out.println(classRoom.getNum() + " has no grades yet");
            else
                System.out.println(classRoom.getNum() + " " + classRoom.getScore());
        }
    }

    public static boolean courseIsMatched(String character, String mode) {
        if (character.equals("实验") && !mode.equals("实验"))
            return false;
        else if (character.equals("必修") && mode.matches("(考察|实验)"))
            return false;
        else if (character.equals("选修") && mode.matches("(实验)"))
            return false;
        else
            return true;
    }

    public static boolean courseIsRepeated(String name, ArrayList<Course> courseArrayList) {
        int flag = 1;
        for (Course course : courseArrayList) {
            if (name.equals(course.getName())) {
                flag = 0;
                break;
            }
        }
        return flag != 0;
    }

    public static boolean scoreIsRepeated(String number, String name, String course, ArrayList<Student> studentArrayList, ArrayList<Score> scoreArrayList) {
        int flag = 1;
        int i = 0;
        for (Student student : studentArrayList) {
            if (student.getNumber().equals(number) && student.getName().equals(name)) {
                i = studentArrayList.indexOf(student);
                flag = 0;
            }
        }
        if (flag == 1)
            return true;
        else {
            if (scoreArrayList.get(i) == null) {
                return true;
            } else {
                return !scoreArrayList.get(i).getCourse().equals(course);
            }
        }
    }
}

class SelectCourse {
    private ArrayList<Course> courseArrayList;
    private ArrayList<Student> studentArrayList;
    private ArrayList<Score> scoreArrayList;

    public SelectCourse(ArrayList<Course> courseArrayList, ArrayList<Student> studentArrayList, ArrayList<Score> scoreArrayList) {
        this.courseArrayList = courseArrayList;
        this.studentArrayList = studentArrayList;
        this.scoreArrayList = scoreArrayList;
    }

    public ArrayList<ClassRoom> classHashSet() {
        ArrayList<ClassRoom> classArrayList = new ArrayList<>();
        ArrayList<ClassRoom> classHashSet = new ArrayList<>();
        ArrayList<Student> studentHashSet = student_sort();

        for (Student student : studentHashSet) {
            int flag = 1;
            String num = student.getNumber().substring(0, 6);
            ClassRoom classRoom = new ClassRoom(num);
            for (ClassRoom classRoom0 : classHashSet) {
                if (classRoom0.isSame(classRoom))
                    flag = 0;
            }
            if (flag == 1)
                classHashSet.add(classRoom);
            ClassRoom classRoom1 = new ClassRoom(student.getScore(), num);
            classArrayList.add(classRoom1);
        }
        for (ClassRoom classRoom : classHashSet) {
            int score = 0;
            int count = 0;
            for (ClassRoom classRoom1 : classArrayList) {
                if (classRoom.isSame(classRoom1)) {
                    if (classRoom1.getScore() != -1) {
                        score += classRoom1.getScore();
                        count++;
                    }
                }
            }
            if (count != 0) {
                score = score / count;
                classRoom.setScore(score);
            } else {
                classRoom.setScore(-1);
            }
        }
        classHashSet.sort(ClassRoom::compareTo);
        return classHashSet;
    }

    public Map<String, Integer> scoreIntegerMap() {
        Map<String, Integer> scoreIntegerMap = new TreeMap<>(new ScoreComparator());
        for (Course course : courseArrayList) {
            int allGrade = 0;
            int count = 0;
            for (Score score : scoreArrayList) {
                if (score != null) {
                    if (score.getCourse().equals(course.getName())) {
                        allGrade += score.getAllGrade();
                        count++;
                    }
                }

            }
            if (count != 0) {
                allGrade = allGrade / count;
                scoreIntegerMap.put(course.getName(), allGrade);
            } else {
                scoreIntegerMap.put(course.getName(), -1);
            }
        }
        return scoreIntegerMap;
    }

    public ArrayList<Student> student_sort() {
        HashSet<Student> studentHashSet = new HashSet<>();
        for (Student student : studentArrayList) {
            int flag = 1;
            for (Student student1 : studentHashSet) {
                if (student.isSame(student1)) {
                    flag = 0;
                }
            }
            if (flag == 1)
                studentHashSet.add(student);
        }
        ArrayList<Student> student_sort = new ArrayList<>();
        for (Student student : studentHashSet) {
            int score = 0;
            int count = 0;
            for (int i = 0; i < studentArrayList.size(); i++) {
                if (student.isSame(studentArrayList.get(i))) {
                    if (scoreArrayList.get(i) != null) {
                        if (scoreArrayList.get(i).getAllGrade() != -1) {
                            score += scoreArrayList.get(i).getAllGrade();
                            count++;
                        }
                    }
                }
            }
            if (count != 0) {
                score = score / count;
                student.setScore(score);
            } else {
                student.setScore(-1);
            }
            student_sort.add(student);
        }
        student_sort.sort(Student::compareTo);
        return student_sort;
    }
}


class Student implements Comparable<Student> {
    private final String number;
    private final String name;

    private int score;

    public Student(String number, String name) {
        this.number = number;
        this.name = name;
    }

    public boolean isSame(Student student) {
        return this.number.equals(student.getNumber()) && this.name.equals(student.getName());
    }

    public int getScore() {
        return score;
    }

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

    public String getNumber() {
        return number;
    }

    public String getName() {
        return name;
    }

    @Override
    public int compareTo(Student o) {
        return Integer.compare(number.compareTo(o.getNumber()), 0);
    }
}

class ClassRoom implements Comparable<ClassRoom> {
    private int score;
    private final String num;

    public ClassRoom(String num) {
        this.num = num;
    }

    public ClassRoom(int score, String num) {
        this.score = score;
        this.num = num;
    }

    public boolean isSame(ClassRoom classRoom) {
        return this.num.equals(classRoom.getNum());
    }

    public int getScore() {
        return score;
    }

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

    public String getNum() {
        return num;
    }

    @Override
    public int compareTo(ClassRoom o) {
        return num.compareTo(o.getNum());
    }
}

class ScoreComparator implements Comparator<String> {

    @Override
    public int compare(String o1, String o2) {
        Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA);
        return compare.compare(o1, o2);
    }
}

class Score{
    private String course;
    private ArrayList<PartScore> partScoreArrayList;

    public Score(ArrayList<PartScore> partScoreArrayList) {
        this.partScoreArrayList = partScoreArrayList;
    }

    public Score(String course, ArrayList<PartScore> partScoreArrayList) {
        this.course = course;
        this.partScoreArrayList = partScoreArrayList;
    }

    public ArrayList<PartScore> getPartScoreArrayList() {
        return partScoreArrayList;
    }

    public void setPartScoreArrayList(ArrayList<PartScore> partScoreArrayList) {
        this.partScoreArrayList = partScoreArrayList;
    }

    public String getCourse() {
        return course;
    }

    public int getAllGrade(){
        float sum=0;
        if(partScoreArrayList.size()==0){
            sum=-1;
        }
        for(PartScore partScore:partScoreArrayList){
                sum+=partScore.allGrade();
        }
        return (int)sum;
    }
}

class PartScore{
    private String course;
    private int score;
    private Float w;

    public PartScore(String course, int score, Float w) {
        this.course = course;
        this.score = score;
        this.w = w;
    }

    public String getCourse() {
        return course;
    }

    public PartScore(Float w) {
        this.w = w;
    }

    public int getScore() {
        return score;
    }

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

    public Float getW() {
        return w;
    }

    public void setW(Float w) {
        this.w = w;
    }

    public Float allGrade(){
        return score*w;
    }
}




class Course {
    private final String name;
    private final String character;//性质
    private final String mode;//考核方式
    private ArrayList<Float> weights;

    public Course(String name, String character, String mode) {
        this.name = name;
        this.character = character;
        this.mode = mode;
    }

    public Course(String name, String character, String mode, ArrayList<Float> weights) {
        this.name = name;
        this.character = character;
        this.mode = mode;
        this.weights = weights;
    }

    public String getName() {
        return name;
    }

    public String getMode() {
        return mode;
    }

    public ArrayList<Float> getWeights() {
        return weights;
    }

    public void setWeights(ArrayList<Float> weights) {
        this.weights = weights;
    }
}

 

踩坑心得

1.使用正则表达式要细心,在判断学号是否匹配时,正则表达式应该是[0-9]{8}但是我误写成了[0-8]{8},这个错误找了我一个多小时;

2.main方法太冗长,模块化没有做好;

3.没有成绩和零分没有做好区分,程序里没有成绩默认为0,而零分也为0,为了区分二者,强制令没有成绩时成绩为-1,这样就不会混淆两种情况;

4.冗长的if判断条件有时可以使用正则表达式代替;

改进建议

main方法模块化,数据的输入,数据的处理都可以分离出来,最好实现MVC模式;

灵活使用正则表达式,在一些情况下,使用正则表达式会比if判断简单得多;

总结

这几周的作业,明显感觉到自己对集合框架的知识愈发熟悉,比如ArrayList,HashMap,HashSet等,以及对他们的排序等一些处理。

在设计课程成绩统计系统时,我发现最开始非常困难,有一种无从下手的无力感,后来走一步看一步,先把基本的几个类写好,然后再看需要输入哪些信息以及需要进行哪些相应的处理,可能也正是因为这样不合理的设计步骤,导致最后信息的输入以及处理全都堆积再main方法里。以后应该试着改变一下切入点,应该先研究输入信息,设计相关类对输入的信息进行处理和存储,最后再思考如何得到输出。

在第三次系统迭代的过程中,要求把成绩类由继承关系改成组合关系,继承和组合在大部分情况下是可以互相转换的,他们各有各的优势,也各有各的缺点,继承最大的一个好处就是代码复用,但继承层次过深、过复杂,也会影响到代码的可维护性,所以在设计的时候到底使用继承还是组合要考虑清楚。

posted @ 2023-06-27 00:32  B饭老司机  阅读(13)  评论(0编辑  收藏  举报