学生成绩管理系统(Java)

开发环境: Windows 11 IDEA 2021.3.3


需求:


java 复制代码
package com.it.neu;

import java.util.ArrayList;
import java.util.Scanner;

import static java.time.Clock.system;

class Student {      //创建学生类
    private String Stu_name;
    private String Stu_id;

    public Student(String id, String name) {
        this.Stu_id = id;
        this.Stu_name = name;
    }

    public String getStu_name() {
        return Stu_name;
    }

    public void setStu_name(String stu_name) {
        Stu_name = stu_name;
    }

    public String getStu_id() {
        return Stu_id;
    }

    public void setStu_id(String stu_id) {
        Stu_id = stu_id;
    }
}

class Course {       //创建课程类
    private String Course_name;

    public Course(String course_name) {
        this.Course_name = course_name;
    }

    public String getCourse_name() {
        return Course_name;
    }

    public void setCourse_name(String course_name) {
        Course_name = course_name;
    }
}

class Score {        //创建成绩表类
    private int rank;       //序号--代表进入成绩管理系统的位次
    private Student student;
    private Course course;
    private double grade;  //代表成绩的意思

    public Score(int rank, Student student, Course course, double grade) {
        this.rank = rank;
        this.student = student;
        this.course = course;
        this.grade = grade;

    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public double getGrade() {
        return grade;
    }

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

class StudentScoreManager {      //创建一个成绩管理的类

    private ArrayList<Score> scores;//新建一个类的数组

    public StudentScoreManager() {
        scores = new ArrayList<>();
    } //初始化这个类成员数组

    //通过调用manager.addScore(score)方法,将score对象添加到scores列表中。
    // 如果这个方法成功执行并返回true,那么score对象就会被添加到scores列表中。
    public boolean addScore(Score score) {
        return scores.add(score); // 将score对象添加到列表中,并返回添加操作的结果
    }


    //删除学生成绩的功能,某一门课//同时判断是否删除成功
    public boolean removeScore(Student student, Course course) {
        for (Score score : scores) {
            if (score.getStudent().getStu_id().equals(student.getStu_id()) && score.getStudent().getStu_name().equals(student.getStu_name()) && score.getCourse().getCourse_name().equals(course.getCourse_name())) {
                scores.remove(score);
                return true;
            }

        }
        return false;
    }

    //实现修改成学生成绩功能
    public boolean changeScore(Student student, Course course, double newGrade) {
        for (Score score : scores) {
            if (score.getStudent().getStu_id().equals(student.getStu_id()) && score.getStudent().getStu_name().equals(student.getStu_name()) && score.getCourse().getCourse_name().equals(course.getCourse_name())) {
                score.setGrade(newGrade);
                return true;
            }
        }
        return false;
    }

    //实现计算某门课平均成绩的功能
    public double getAverageGrade(Course course) {
        double sum = 0;
        int count = 0;
        for (Score score : scores) {
            if (score.getCourse().getCourse_name().equals(course.getCourse_name())) { //找到这门课了
                sum += score.getGrade();
                count++;
            }
        }
        return count > 0 ? sum / count : 0;
    }

    //查找某门课的最高成绩
    public double getHighesrGrade(Course course) {       //某门课的最高成绩
        double higest = 0;
        for (Score score : scores) {
            if (score.getCourse().getCourse_name().equals(course.getCourse_name()) && score.getGrade() > higest) {
                higest = score.getGrade();
            }
        }
        return higest;
    }

    //查找某门课的最低成绩
    public double getLowestGrade(Course course) {
        double lowest = 100;
        for (Score score : scores) {
            if (score.getCourse().getCourse_name().equals(course.getCourse_name()) && score.getGrade() < lowest) {
                lowest = score.getGrade();
            }
        }
        return lowest;
    }

    //实现成绩排序功能
    public void sortScores() {
        scores.sort((s1, s2) -> Double.compare(s2.getGrade(), s1.getGrade()));
    }

    public void printScores() {
        sortScores();
        for (Score score : scores) {
            System.out.println(score.getRank() + " " + score.getStudent().getStu_id() + " " + score.getStudent().getStu_name() + " " + score.getCourse().getCourse_name() + " " + score.getGrade());
        }
    }

    public boolean printfixScores(Student student) {
        int count = 0;

        for (Score score : scores) {
            if (score.getStudent().getStu_id().equals(student.getStu_id()) && score.getStudent().getStu_name().equals(student.getStu_name())) {
                System.out.println(score.getCourse().getCourse_name() + "的成绩是" + score.getGrade());
                count++;
            }
        }
        return count > 0 ? true : false;
    }

    //获取scores类list的长度
    public int getScoresLength() {
        return scores.size();
    }
}


public class Work {

    public static void printmenu() {

        System.out.println("---------------------------------------");
        System.out.println("欢迎使用学生成绩管理系统");
        System.out.println("---------------------------------------");
        System.out.println("1. 添加学生成绩");
        System.out.println("2. 删除学生成绩");
        System.out.println("3. 修改学生成绩");
        System.out.println("4. 计算课程平均-最高-最低-成绩");
        System.out.println("5. 查询学生成绩");
        System.out.println("6. 对成绩进行排序并打印所有成绩");
        System.out.println("0. 退出系统");
        System.out.println("---------------------------------------");
        System.out.print("请输入操作序号:");
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        StudentScoreManager manager = new StudentScoreManager();

        while (true) {

            printmenu();

            int opeartion = scanner.nextInt();
            if (opeartion < 0 || opeartion > 6) {
                System.out.println("没有这个选项,请重新选择!");
                continue;
            }
            scanner.nextLine();

            switch (opeartion) {
                case 1: {
                    System.out.println("您正在使用功能一:添加学生成绩");
                    System.out.println("---------------------------------------");
                    System.out.println("请输入学生学号:");
                    String id = scanner.nextLine();
                    System.out.println("请输入学生姓名:");
                    String name = scanner.nextLine();
                    System.out.println("请输入课程名称:");
                    String courseName = scanner.nextLine();
                    System.out.println("请输入成绩:");
                    double grade = scanner.nextDouble();

                    //新建临时类来存储输入的数据
                    Student student = new Student(id, name);
                    Course course = new Course(courseName);
                    Score score = new Score(manager.getScoresLength() + 1, student, course, grade);

                    //将这个临时的数据塞入,manager这个类的scores-list中,
                    if (manager.addScore(score)) {
                        System.out.println("添加成绩数据成功!");
                    } else {
                        System.out.println("添加成绩数据失败!");
                    }

                    // 等待用户按下回车键继续执行
                    System.out.println("按下回车键继续...");
                    scanner.nextLine();
                    break;
                }
                case 2: {
                    System.out.println("您正在使用功能二:删除学生成绩");
                    System.out.println("---------------------------------------");
                    System.out.println("请输入学生学号:");
                    String id = scanner.nextLine();
                    System.out.println("请输入学生姓名:");
                    String name = scanner.nextLine();
                    System.out.println("请输入课程名称:");
                    String courseName = scanner.nextLine();

                    Student student = new Student(id, name);
                    Course course = new Course(courseName);

                    if (manager.removeScore(student, course)) {
                        System.out.println("删除学生成绩成功!");
                    } else {
                        System.out.println("删除学生成绩失败!没有找到此人");
                    }

                    // 等待用户按下回车键继续执行
                    System.out.println("按下回车键继续...");
                    scanner.nextLine();
                    break;
                }
                case 3: {
                    System.out.println("您正在使用功能三:修改学生成绩");
                    System.out.println("---------------------------------------");
                    System.out.println("请输入学生学号:");
                    String id = scanner.nextLine();
                    System.out.println("请输入学生姓名:");
                    String name = scanner.nextLine();
                    System.out.println("请输入课程名称:");
                    String courseName = scanner.nextLine();
                    System.out.println("请输入新的成绩:");
                    double newGrade = scanner.nextDouble();

                    Student student = new Student(id, name);
                    Course course = new Course(courseName);

                    if (manager.changeScore(student, course, newGrade)) {
                        System.out.println("修改学生成绩成功!");
                    } else {
                        System.out.println("修改学生成绩失败!没有找到此人");
                    }

                    break;
                }
                case 4: {
                    System.out.println("您正在使用功能四:计算课程平均-最高-最低-成绩");
                    System.out.println("---------------------------------------");
                    System.out.println("请输入课程名称:");
                    String courseName = scanner.nextLine();

                    Course course = new Course(courseName);
                    double average = manager.getAverageGrade(course);
                    double higest = manager.getHighesrGrade(course);
                    double lowest = manager.getLowestGrade(course);
                    System.out.println(courseName + "的平均成绩为:" + average);
                    System.out.println(courseName + "的最高成绩为:" + higest);
                    System.out.println(courseName + "的最低成绩为:" + lowest);


                    // 等待用户按下回车键继续执行
                    System.out.println("按下回车键继续...");
                    scanner.nextLine();
                    break;
                }
                case 5: {
                    System.out.println("您正在使用功能五:查询学生成绩");
                    System.out.println("---------------------------------------");
                    System.out.println("请输入学生学号:");
                    String id = scanner.nextLine();
                    System.out.println("请输入学生姓名:");
                    String name = scanner.nextLine();

                    Student student = new Student(id, name);
                    if (manager.printfixScores(student)) {
                        //判断的同时可以调用
                    } else {
                        System.out.println("没有找到此人");
                    }

                    // 等待用户按下回车键继续执行
                    System.out.println("按下回车键继续...");
                    scanner.nextLine();
                    break;
                }
                case 6: {
                    System.out.println("您正在使用功能六:对成绩进行排序并打印所有成绩");
                    System.out.println("---------------------------------------");
                    manager.printScores();
                    // 等待用户按下回车键继续执行
                    System.out.println("按下回车键继续...");
                    scanner.nextLine();
                    break;
                }

            }
        }
    }
}
相关推荐
木向10 分钟前
leetcode22:括号问题
开发语言·c++·leetcode
comli_cn12 分钟前
使用清华源安装python包
开发语言·python
筑基.18 分钟前
basic_ios及其衍生库(附 GCC libstdc++源代码)
开发语言·c++
m0_7482451731 分钟前
Web第一次作业
java
小码的头发丝、32 分钟前
Java进阶学习笔记|面向对象
java·笔记·学习
雨颜纸伞(hzs)32 分钟前
C语言介绍
c语言·开发语言·软件工程
J总裁的小芒果34 分钟前
THREE.js 入门(六) 纹理、uv坐标
开发语言·javascript·uv
m0_5485147735 分钟前
前端Pako.js 压缩解压库 与 Java 的 zlib 压缩与解压 的互通实现
java·前端·javascript
坊钰1 小时前
【Java 数据结构】移除链表元素
java·开发语言·数据结构·学习·链表