package com.studentmanagement; import com.studentmanagement.controller.StudentController; import com.studentmanagement.model.Student; import com.studentmanagement.util.InputUtil; import com.studentmanagement.view.StudentConsoleView; import com.studentmanagement.view.StudentView; /** * 学生管理系统主应用类 * 应用程序的入口点,初始化MVC组件并启动系统 */ public class MainApp { public static void main(String[] args) { // 初始化MVC组件 StudentView studentView = new StudentConsoleView(); StudentController studentController = new StudentController(studentView); // 启动应用 runApplication(studentController, (StudentConsoleView) studentView); } /** * 运行应用程序主循环 * @param controller 学生控制器 * @param view 学生控制台视图 */ private static void runApplication(StudentController controller, StudentConsoleView view) { boolean running = true; while (running) { // 显示主菜单 view.displayMainMenu(); // 获取用户选择 int choice = InputUtil.getIntInput(""); // 根据选择执行相应操作 switch (choice) { case 1: addStudent(controller); break; case 2: controller.displayAllStudents(); break; case 3: findStudentById(controller); break; case 4: updateStudent(controller); break; case 5: deleteStudent(controller); break; case 6: filterStudentsByCourse(controller); break; case 7: filterStudentsByGradeRange(controller); break; case 8: calculateAverageGradeByCourse(controller); break; case 9: controller.calculateAverageAge(); break; case 0: running = false; System.out.println("\n感谢使用学生管理系统,再见!"); break; default: System.out.println("无效的选择,请重新输入"); break; } // 如果不是退出,等待用户确认继续 if (running && choice != 0) { InputUtil.waitForEnter(); } } } /** * 添加学生 * @param controller 学生控制器 */ private static void addStudent(StudentController controller) { String id = InputUtil.getStringInput("请输入学生ID: "); String name = InputUtil.getStringInput("请输入学生姓名: "); int age = InputUtil.getIntInput("请输入学生年龄: "); String gender = InputUtil.getStringInput("请输入学生性别: "); Student student = new Student(id, name, age, gender); // 添加课程成绩 boolean addMoreCourses = true; while (addMoreCourses) { String course = InputUtil.getStringInput("请输入课程名称 (输入空字符串结束): "); if (course.isEmpty()) { addMoreCourses = false; } else { double grade = InputUtil.getDoubleInput("请输入该课程成绩: "); student.addCourseGrade(course, grade); } } controller.addStudent(student); } /** * 根据ID查找学生 * @param controller 学生控制器 */ private static void findStudentById(StudentController controller) { String id = InputUtil.getStringInput("请输入要查找的学生ID: "); controller.findStudentById(id); } /** * 更新学生信息 * @param controller 学生控制器 */ private static void updateStudent(StudentController controller) { String id = InputUtil.getStringInput("请输入要更新的学生ID: "); // 先查找学生是否存在 try { // 这里我们直接创建一个新的学生对象来更新 String name = InputUtil.getStringInput("请输入新的学生姓名: "); int age = InputUtil.getIntInput("请输入新的学生年龄: "); String gender = InputUtil.getStringInput("请输入新的学生性别: "); Student updatedStudent = new Student(id, name, age, gender); // 添加课程成绩 boolean addCourses = InputUtil.getConfirmation("是否添加/更新课程成绩"); if (addCourses) { boolean addMoreCourses = true; while (addMoreCourses) { String course = InputUtil.getStringInput("请输入课程名称 (输入空字符串结束): "); if (course.isEmpty()) { addMoreCourses = false; } else { double grade = InputUtil.getDoubleInput("请输入该课程成绩: "); updatedStudent.addCourseGrade(course, grade); } } } controller.updateStudent(updatedStudent); } catch (Exception e) { System.out.println("更新失败: " + e.getMessage()); } } /** * 删除学生 * @param controller 学生控制器 */ private static void deleteStudent(StudentController controller) { String id = InputUtil.getStringInput("请输入要删除的学生ID: "); if (InputUtil.getConfirmation("确定要删除该学生吗?")) { controller.deleteStudent(id); } else { System.out.println("删除操作已取消"); } } /** * 根据课程筛选学生 * @param controller 学生控制器 */ private static void filterStudentsByCourse(StudentController controller) { String course = InputUtil.getStringInput("请输入课程名称: "); controller.filterStudentsByCourse(course); } /** * 根据成绩范围筛选学生 * @param controller 学生控制器 */ private static void filterStudentsByGradeRange(StudentController controller) { double minGrade = InputUtil.getDoubleInput("请输入最低成绩: "); double maxGrade = InputUtil.getDoubleInput("请输入最高成绩: "); controller.filterStudentsByGradeRange(minGrade, maxGrade); } /** * 计算课程平均成绩 * @param controller 学生控制器 */ private static void calculateAverageGradeByCourse(StudentController controller) { String course = InputUtil.getStringInput("请输入课程名称: "); controller.calculateAverageGradeByCourse(course); } }