2021-08-27 基于JDK8新特性的学生管理系统

这是一个使用Java实现的学生管理系统,包含添加、删除、修改和查询学生信息的功能,并能根据多个字段对学生进行排序,如姓名、学号、年龄、性别、出生日期和各科成绩。此外,代码还提供了按成绩总和和平均分排序的方法,但存在潜在问题,如无法处理重复学号的情况。系统可进一步完善,增加对各科成绩的独立操作和更全面的错误处理。
摘要由CSDN通过智能技术生成

原始数据

static {
        stuList.add(new Student(1001, "jack", 1, 12, "男", "2009-05-12", Collections.singletonList(new Score(90, 98, 89, 86))));
        stuList.add(new Student(1002, "tom", 2, 13, "男", "2008-03-10", Collections.singletonList(new Score(77, 78, 89, 84))));
        stuList.add(new Student(1003, "bill", 3, 13, "男", "2008-10-29", Collections.singletonList(new Score(88, 95, 78, 86))));
        stuList.add(new Student(1004, "gary", 4, 15, "女", "2007-06-06", Collections.singletonList(new Score(69, 86, 96, 77))));
    }

效果图

菜单栏

在这里插入图片描述

添加学生

在这里插入图片描述

删除学生

  • 将刚新增的学生删除

在这里插入图片描述

修改学生

  • 失败的结果
    在这里插入图片描述
  • 成功的结果
    在这里插入图片描述

查询所有学生

在这里插入图片描述

根据姓名首字母排序

在这里插入图片描述

根据学号排序

在这里插入图片描述

根据年龄排序

在这里插入图片描述

根据性别分组

在这里插入图片描述

根据出生日期排序

在这里插入图片描述

根据语文成绩排序

在这里插入图片描述

根据数学成绩排序

在这里插入图片描述

根据英语成绩排序

在这里插入图片描述

根据Java成绩排序

在这里插入图片描述

根据成绩总和排序

在这里插入图片描述

根据成绩总和平均分排序(多余的)

在这里插入图片描述

退出

在这里插入图片描述

完整代码

  • Manage.java
import java.util.*;
import java.util.stream.Collectors;

public class Manage {

    private static List<Student> stuList = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    static {
        stuList.add(new Student(1001, "jack", 1, 12, "男", "2009-05-12", Collections.singletonList(new Score(90, 98, 89, 86))));
        stuList.add(new Student(1002, "tom", 2, 13, "男", "2008-03-10", Collections.singletonList(new Score(77, 78, 89, 84))));
        stuList.add(new Student(1003, "bill", 3, 13, "男", "2008-10-29", Collections.singletonList(new Score(88, 95, 78, 86))));
        stuList.add(new Student(1004, "gary", 4, 15, "女", "2007-06-06", Collections.singletonList(new Score(69, 86, 96, 77))));
    }

    public static void main(String[] args) {

        while (true) {
            menu();
            System.out.print("请输入编号:");
            String num = scanner.next();
            switch (num) {
                case "1":
                    addStudent();
                    System.out.println("添加成功");
                    break;
                case "2":
                    System.out.println(delStudent() ? "删除成功" : "删除失败或没有此学生");
                    break;
                case "3":
                    System.out.println(updateStudent() ? "修改成功" : "修改失败或没有此学生");
                    break;
                case "4":
                    queryStudent();
                    break;
                case "5":
                    sortByName();
                    break;
                case "6":
                    sortBySno();
                    break;
                case "7":
                    sortByAge();
                    break;
                case "8":
                    groupBySex();
                    break;
                case "9":
                    sortByBirthDate();
                    break;
                case "10":
                    sortByChineseScore();
                    break;
                case "11":
                    sortByMathScore();
                    break;
                case "12":
                    groupByEngLishScore();
                    break;
                case "13":
                    sortByJavaScore();
                    break;
                case "14":
                    sortByScoreSum();
                    break;
                case "15":
                    sortByScoreAvg();
                    break;
                case "16":
                    System.out.println("欢迎下次来临!");
                    return;
                default:
                    System.out.println("编号有误,请重新输入!");
                    break;
            }
        }

    }

    public static void sortByScoreAvg() {
        stuList.stream().sorted(Comparator.comparing(Student::getAvgScore)).collect(Collectors.toList()).forEach(System.out::println);
    }

    public static void sortByScoreSum() {
        stuList.stream().sorted(Comparator.comparing(Student::getSumScore)).collect(Collectors.toList()).forEach(System.out::println);
    }

    public static void sortByJavaScore() {
        stuList.stream().sorted(Comparator.comparing(Student::getJavaScore)).collect(Collectors.toList()).forEach(System.out::println);
    }

    public static void groupByEngLishScore() {
        stuList.stream().sorted(Comparator.comparing(Student::getEnglishScore)).collect(Collectors.toList()).forEach(System.out::println);
    }

    public static void sortByMathScore() {
        stuList.stream().sorted(Comparator.comparing(Student::getMathScore)).collect(Collectors.toList()).forEach(System.out::println);
    }

    public static void sortByChineseScore() {
        stuList.stream().sorted(Comparator.comparing(Student::getChineseScore)).collect(Collectors.toList()).forEach(System.out::println);
    }

    public static void sortByBirthDate() {
        stuList.stream().sorted(Comparator.comparing(Student::getBirthday)).collect(Collectors.toList()).forEach(System.out::println);
    }

    public static void groupBySex() {
        Map<String, List<Student>> groupBySex = stuList.stream().collect(Collectors.groupingBy(Student::getSex));
        groupBySex.forEach((key, value) -> {
            System.out.println(key);
            value.forEach(System.out::println);
        });
    }

    public static void sortByAge() {
        stuList.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList()).forEach(System.out::println);
    }

    public static void sortBySno() {
        stuList.stream().sorted(Comparator.comparing(Student::getSno)).collect(Collectors.toList()).forEach(System.out::println);
    }

    public static void sortByName() {
        stuList.stream().sorted(Comparator.comparing(Student::getName)).collect(Collectors.toList()).forEach(System.out::println);
    }

    public static void queryStudent() {
        stuList.forEach(System.out::println);
    }

    public static boolean updateStudent() {
        System.out.print("请输入学生编号:");
        Integer num = scanner.nextInt();
        Student filter = null;
        try {
            filter = stuList.stream().filter(student ->
                    student.getNum().equals(num)
            ).collect(Collectors.toList()).get(0);
        } catch (Exception e) {
        }
        if (filter == null) {
            return false;
        }
        System.out.print("请输入学生姓名:");
        String name = scanner.next();
        System.out.print("请输入学生学号:");
        Integer sno = scanner.nextInt();
        System.out.print("请输入学生年龄:");
        Integer age = scanner.nextInt();
        System.out.print("请输入学生性别:");
        String sex = scanner.next();
        System.out.print("请输入学生生日 (格式:yyyy-MM-dd):");
        String birthDate = scanner.next();
        System.out.print("请输入学生语文成绩:");
        Integer chineseScore = scanner.nextInt();
        System.out.print("请输入学生数学成绩:");
        Integer mathScore = scanner.nextInt();
        System.out.print("请输入学生英语成绩:");
        Integer engLiShScore = scanner.nextInt();
        System.out.print("请输入学生Java成绩:");
        Integer javaScore = scanner.nextInt();
        filter.setName(name);
        filter.setSno(sno);
        filter.setAge(age);
        filter.setSex(sex);
        filter.setBirthday(birthDate);
        filter.setScoreList(Collections.singletonList(new Score(chineseScore, mathScore, engLiShScore, javaScore)));
        return true;
    }

    public static boolean delStudent() {
        System.out.print("请输入学生编号:");
        Integer num = scanner.nextInt();
        return stuList.removeIf(student -> student.getNum().equals(num));
    }

    public static void addStudent() {
        System.out.print("请输入学生编号:");
        Integer num = scanner.nextInt();
        System.out.print("请输入学生姓名:");
        String name = scanner.next();
        System.out.print("请输入学生学号:");
        Integer sno = scanner.nextInt();
        System.out.print("请输入学生年龄:");
        Integer age = scanner.nextInt();
        System.out.print("请输入学生性别:");
        String sex = scanner.next();
        System.out.print("请输入学生生日 (格式:yyyy-MM-dd):");
        String birthDate = scanner.next();
        System.out.print("请输入学生语文成绩:");
        Integer chineseScore = scanner.nextInt();
        System.out.print("请输入学生数学成绩:");
        Integer mathScore = scanner.nextInt();
        System.out.print("请输入学生英语成绩:");
        Integer engLiShScore = scanner.nextInt();
        System.out.print("请输入学生Java成绩:");
        Integer javaScore = scanner.nextInt();
        stuList.add(new Student(num, name, sno, age, sex, birthDate, Collections.singletonList(new Score(chineseScore, mathScore, engLiShScore, javaScore))));
    }


    public static void menu() {
        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("7.根据年龄排序");
        System.out.println("8.根据性别分组");
        System.out.println("9.根据出生日期排序");
        System.out.println("10.根据语文成绩排序");
        System.out.println("11.根据数学成绩排序");
        System.out.println("12.根据英语成绩排序");
        System.out.println("13.根据Java成绩排序");
        System.out.println("14.根据成绩总和排序");
        System.out.println("15.根据成绩平均分排序");
        System.out.println("16.退出");
        System.out.println("==================================");
    }
}
  • Student.java
import java.util.List;

public class Student {

    private Integer num;
    private String name;
    private Integer sno;
    private Integer age;
    private String sex;
    private String birthday;
    private List<Score> scoreList;

    public Student(Integer num, String name, Integer sno, Integer age, String sex, String birthday, List<Score> scoreList) {
        this.num = num;
        this.name = name;
        this.sno = sno;
        this.age = age;
        this.sex = sex;
        this.birthday = birthday;
        this.scoreList = scoreList;
    }

    public Student() {
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    public String getName() {
        return name;
    }

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

    public Integer getSno() {
        return sno;
    }

    public void setSno(Integer sno) {
        this.sno = sno;
    }

    public Integer getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getBirthday() {
        return birthday;
    }

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

    public List<Score> getScoreList() {
        return scoreList;
    }

    public void setScoreList(List<Score> scoreList) {
        this.scoreList = scoreList;
    }

    public Integer getChineseScore() {
        return scoreList.get(0).getChineseScore();
    }

    public Integer getMathScore() {
        return scoreList.get(0).getMathScore();
    }

    public Integer getEnglishScore() {
        return scoreList.get(0).getEnglishScore();
    }

    public Integer getJavaScore() {
        return scoreList.get(0).getJavaScore();
    }

    public Integer getSumScore() {
        return scoreList.get(0).getJavaScore()
                + scoreList.get(0).getEnglishScore()
                + scoreList.get(0).getMathScore()
                + scoreList.get(0).getChineseScore();
    }

    public Integer getAvgScore() {
        return getSumScore() / 4;
    }


    @Override
    public String toString() {
        return "Student{" +
                "num=" + num +
                ", name='" + name + '\'' +
                ", sno=" + sno +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", birthday='" + birthday + '\'' +
                ", scoreList=" + scoreList +
                '}';
    }
}

  • Score.java
public class Score {

    private Integer chineseScore;
    private Integer mathScore;
    private Integer englishScore;
    private Integer javaScore;

    public Integer getChineseScore() {
        return chineseScore;
    }

    public void setChineseScore(Integer chineseScore) {
        this.chineseScore = chineseScore;
    }

    public Integer getMathScore() {
        return mathScore;
    }

    public void setMathScore(Integer mathScore) {
        this.mathScore = mathScore;
    }

    public Integer getEnglishScore() {
        return englishScore;
    }

    public void setEnglishScore(Integer englishScore) {
        this.englishScore = englishScore;
    }

    public Integer getJavaScore() {
        return javaScore;
    }

    public void setJavaScore(Integer javaScore) {
        this.javaScore = javaScore;
    }

    public Score(Integer chineseScore, Integer mathScore, Integer englishScore, Integer javaScore) {
        this.chineseScore = chineseScore;
        this.mathScore = mathScore;
        this.englishScore = englishScore;
        this.javaScore = javaScore;
    }

    public Score() {
    }

    @Override
    public String toString() {
        return "Score{" +
                "chineseScore=" + chineseScore +
                ", mathScore=" + mathScore +
                ", englishScore=" + englishScore +
                ", javaScore=" + javaScore +
                '}';
    }
}

问题

  • 本项目还有些漏洞,如添加相同的学生编号,删除时则会一起删除,可自行加方法判断。
  • 还可以根据各科科目进行一系列操作,后续待完善!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

念之晨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值