diff --git a/server/.idea/workspace.xml b/server/.idea/workspace.xml index 0d5a414d..a74db1e5 100644 --- a/server/.idea/workspace.xml +++ b/server/.idea/workspace.xml @@ -78,6 +78,10 @@ + + + + @@ -140,6 +144,7 @@ + diff --git a/server/src/main/java/self/cases/teams/Student.java b/server/src/main/java/self/cases/teams/Student.java new file mode 100644 index 00000000..005c817f --- /dev/null +++ b/server/src/main/java/self/cases/teams/Student.java @@ -0,0 +1,351 @@ +package self.cases.teams; + +import java.io.*; +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.stream.Collectors; + +// 学生类 +class Student implements Serializable { + private String id; + private String name; + private int age; + private String className; + private Date enrollDate; + private Map scores; + + public Student(String id, String name, int age, String className) { + this.id = id; + this.name = name; + this.age = age; + this.className = className; + this.enrollDate = new Date(); + this.scores = new HashMap<>(); + } + + // Getter和Setter方法 + public String getId() { return id; } + public String getName() { return name; } + public int getAge() { return age; } + public String getClassName() { return className; } + public Date getEnrollDate() { return enrollDate; } + public Map getScores() { return scores; } + + public void updateScore(String course, double score) { + scores.put(course, score); + } + + public double calculateAverage() { + return scores.values().stream() + .mapToDouble(Double::doubleValue) + .average() + .orElse(0.0); + } + + @Override + public String toString() { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + return String.format("学号: %s\n姓名: %s\n年龄: %d\n班级: %s\n入学日期: %s", + id, name, age, className, sdf.format(enrollDate)); + } +} + +// 课程管理类 +class CourseManager { + private static final List AVAILABLE_COURSES = Arrays.asList( + "数学", "语文", "英语", "物理", "化学" + ); + + public static boolean isValidCourse(String course) { + return AVAILABLE_COURSES.contains(course); + } + + public static void showAvailableCourses() { + System.out.println("可选课程列表:"); + AVAILABLE_COURSES.forEach(c -> System.out.print(c + " ")); + System.out.println(); + } +} + +// 文件工具类 +class FileUtil { + private static final String DATA_FILE = "students.dat"; + + public static void saveData(List students) { + try (ObjectOutputStream oos = new ObjectOutputStream( + new FileOutputStream(DATA_FILE))) { + oos.writeObject(students); + } catch (IOException e) { + System.err.println("数据保存失败: " + e.getMessage()); + } + } + + @SuppressWarnings("unchecked") + public static List loadData() { + File file = new File(DATA_FILE); + if (!file.exists()) return new ArrayList<>(); + + try (ObjectInputStream ois = new ObjectInputStream( + new FileInputStream(DATA_FILE))) { + return (List) ois.readObject(); + } catch (IOException | ClassNotFoundException e) { + System.err.println("数据加载失败: " + e.getMessage()); + return new ArrayList<>(); + } + } +} + +// 输入工具类 +class InputUtil { + private static final Scanner scanner = new Scanner(System.in); + + public static int getIntInput(String prompt) { + while (true) { + System.out.print(prompt); + try { + return Integer.parseInt(scanner.nextLine()); + } catch (NumberFormatException e) { + System.out.println("输入无效,请输入整数!"); + } + } + } + + public static String getStringInput(String prompt) { + System.out.print(prompt); + return scanner.nextLine().trim(); + } + + public static double getDoubleInput(String prompt) { + while (true) { + System.out.print(prompt); + try { + return Double.parseDouble(scanner.nextLine()); + } catch (NumberFormatException e) { + System.out.println("输入无效,请输入数字!"); + } + } + } +} + +// 主系统类 +class StudentManagementSystem { + private List students; + private boolean isRunning = true; + + public StudentManagementSystem() { + students = FileUtil.loadData(); + } + + public void start() { + while (isRunning) { + showMainMenu(); + int choice = InputUtil.getIntInput("请选择操作:"); + handleMainMenu(choice); + } + } + + private void showMainMenu() { + System.out.println("\n==== 学生信息管理系统 ===="); + 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. 退出系统"); + } + + private void handleMainMenu(int choice) { + switch (choice) { +// case 1 -> addStudent(); +// case 2 -> searchStudent(); +// case 3 -> inputScore(); +// case 4 -> showStatistics(); +// case 5 -> deleteStudent(); +// case 6 -> saveData(); +// case 7 -> exitSystem(); +// default -> System.out.println("无效选项!"); + } + } + + private void addStudent() { + String id = InputUtil.getStringInput("请输入学号:"); + if (isStudentExist(id)) { + System.out.println("该学号已存在!"); + return; + } + + String name = InputUtil.getStringInput("请输入姓名:"); + int age = InputUtil.getIntInput("请输入年龄:"); + String className = InputUtil.getStringInput("请输入班级:"); + + Student student = new Student(id, name, age, className); + students.add(student); + System.out.println("学生添加成功!"); + } + + private void searchStudent() { + System.out.println("\n==== 查询选项 ===="); + System.out.println("1. 按学号查询"); + System.out.println("2. 按姓名查询"); + System.out.println("3. 显示全部"); + int choice = InputUtil.getIntInput("请选择查询方式:"); + + switch (choice) { +// case 1 -> searchById(); +// case 2 -> searchByName(); +// case 3 -> showAllStudents(); +// default -> System.out.println("无效选项!"); + } + } + + private void searchById() { + String id = InputUtil.getStringInput("请输入学号:"); + students.stream() + .filter(s -> s.getId().equals(id)) + .findFirst(); + + } + + private void searchByName() { + String name = InputUtil.getStringInput("请输入姓名:"); + List result = null; + try { + students.stream() + .filter(s -> s.getName().equalsIgnoreCase(name)) + .wait(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + if (result.isEmpty()) { + System.out.println("找不到该学生"); + } else { + result.forEach(System.out::println); + } + } + + private void showAllStudents() { + if (students.isEmpty()) { + System.out.println("当前没有学生记录"); + return; + } + students.forEach(s -> { + System.out.println(s); + System.out.println("--------------------"); + }); + } + + private void inputScore() { + String id = InputUtil.getStringInput("请输入学号:"); + Student student = findStudentById(id); + if (student == null) { + System.out.println("学生不存在!"); + return; + } + + CourseManager.showAvailableCourses(); + String course = InputUtil.getStringInput("请输入课程名称:"); + if (!CourseManager.isValidCourse(course)) { + System.out.println("无效课程!"); + return; + } + + double score = InputUtil.getDoubleInput("请输入成绩:"); + student.updateScore(course, score); + System.out.println("成绩录入成功!"); + } + + private void showStatistics() { + System.out.println("\n==== 统计信息 ===="); + System.out.println("1. 班级人数统计"); + System.out.println("2. 课程平均分"); + System.out.println("3. 学生个人成绩"); + int choice = InputUtil.getIntInput("请选择统计类型:"); + + switch (choice) { +// case 1 -> showClassStatistics(); +// case 2 -> showCourseStatistics(); +// case 3 -> showStudentScores(); +// default -> System.out.println("无效选项!"); + } + } + + private void showClassStatistics() { + Map classCount = students.stream() + .collect(Collectors.groupingBy( + Student::getClassName, + Collectors.counting())); + + classCount.forEach((className, count) -> + System.out.printf("班级 %s: %d 人\n", className, count)); + } + + private void showCourseStatistics() { + Map courseAverages = new HashMap<>(); + students.forEach(student -> + student.getScores().forEach((course, score) -> + courseAverages.merge(course, score, Double::sum))); + + courseAverages.forEach((course, total) -> { + long count = students.stream() + .filter(s -> s.getScores().containsKey(course)) + .count(); + double average = count > 0 ? total / count : 0; + System.out.printf("%s 平均分: %.2f\n", course, average); + }); + } + + private void showStudentScores() { + String id = InputUtil.getStringInput("请输入学号:"); + Student student = findStudentById(id); + if (student == null) { + System.out.println("学生不存在!"); + return; + } + + System.out.println(student); + if (student.getScores().isEmpty()) { + System.out.println("暂无成绩记录"); + return; + } + + student.getScores().forEach((course, score) -> + System.out.printf("%s: %.2f\n", course, score)); + System.out.printf("平均分: %.2f\n", student.calculateAverage()); + } + + private void deleteStudent() { + String id = InputUtil.getStringInput("请输入要删除的学号:"); + boolean removed = students.removeIf(s -> s.getId().equals(id)); + System.out.println(removed ? "删除成功!" : "学生不存在!"); + } + + private void saveData() { + FileUtil.saveData(students); + System.out.println("数据已保存!"); + } + + private void exitSystem() { + isRunning = false; + System.out.println("正在退出系统..."); + saveData(); + System.out.println("谢谢使用!"); + } + + private boolean isStudentExist(String id) { + return students.stream() + .anyMatch(s -> s.getId().equals(id)); + } + + private Student findStudentById(String id) { + return students.stream() + .filter(s -> s.getId().equals(id)) + .findFirst() + .orElse(null); + } + + public static void main(String[] args) { + new StudentManagementSystem().start(); + } +} \ No newline at end of file diff --git a/server/target/classes/self/cases/teams/CourseManager.class b/server/target/classes/self/cases/teams/CourseManager.class new file mode 100644 index 00000000..2b83167f Binary files /dev/null and b/server/target/classes/self/cases/teams/CourseManager.class differ diff --git a/server/target/classes/self/cases/teams/FileUtil.class b/server/target/classes/self/cases/teams/FileUtil.class new file mode 100644 index 00000000..f142cbf3 Binary files /dev/null and b/server/target/classes/self/cases/teams/FileUtil.class differ diff --git a/server/target/classes/self/cases/teams/InputUtil.class b/server/target/classes/self/cases/teams/InputUtil.class new file mode 100644 index 00000000..251a21bb Binary files /dev/null and b/server/target/classes/self/cases/teams/InputUtil.class differ diff --git a/server/target/classes/self/cases/teams/Student.class b/server/target/classes/self/cases/teams/Student.class new file mode 100644 index 00000000..f1825f76 Binary files /dev/null and b/server/target/classes/self/cases/teams/Student.class differ diff --git a/server/target/classes/self/cases/teams/StudentManagementSystem.class b/server/target/classes/self/cases/teams/StudentManagementSystem.class new file mode 100644 index 00000000..120b77ff Binary files /dev/null and b/server/target/classes/self/cases/teams/StudentManagementSystem.class differ diff --git a/server/target/classes/self/cases/teams/utils/Tea.class b/server/target/classes/self/cases/teams/utils/Tea.class new file mode 100644 index 00000000..47323369 Binary files /dev/null and b/server/target/classes/self/cases/teams/utils/Tea.class differ