原始数据
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))));
}
效果图
菜单栏
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/485aff4414bd376a43eaeea20796d083.png#pic_center)
添加学生
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8ffc312802e20c88e162c932a3c7a1d2.png#pic_center)
删除学生
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4efa8800717e51d40de76221f47dd79d.png#pic_center)
修改学生
- 失败的结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9850e094d711f6e84b99b469933dd3ec.png#pic_center)
- 成功的结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0708ea624d3826222d28da05c5dd2464.png#pic_center)
查询所有学生
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5db04d202cd78ea54e8a093e54b553e1.png#pic_center)
根据姓名首字母排序
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2aab117e8c8e5fae3ea1aead0819f83a.png#pic_center)
根据学号排序
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3de9ad27e9c0e4e86b8a6975da4afa22.png#pic_center)
根据年龄排序
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d5627f783944a3bc1da52d5b9096c8fe.png#pic_center)
根据性别分组
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d3b107b340451900e349a19dc71f11b4.png#pic_center)
根据出生日期排序
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b199ce0ed58ade1f49cdfe1c9abf03e1.png#pic_center)
根据语文成绩排序
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b058704a82d3230e1f030d38ead01d6f.png#pic_center)
根据数学成绩排序
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b403706f09ff55710260906603b6c20e.png#pic_center)
根据英语成绩排序
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cb30ee38b7fdfbc2511f29ec1d6bc653.png#pic_center)
根据Java成绩排序
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/624034d818406bb027435a39d9fb738c.png#pic_center)
根据成绩总和排序
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/20c70a6b8ee2288bf781d2c4cafc17f0.png#pic_center)
根据成绩总和平均分排序(多余的)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c3db3a4428137d6b55961dc9a425024c.png#pic_center)
退出
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/dfcd74232498f1852ee32364b7352521.png#pic_center)
完整代码
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("==================================");
}
}
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 +
'}';
}
}
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 +
'}';
}
}
问题
- 本项目还有些漏洞,如添加相同的学生编号,删除时则会一起删除,可自行加方法判断。
- 还可以根据各科科目进行一系列操作,后续待完善!