package com.studentmanagement; import com.studentmanagement.controller.StudentController; import com.studentmanagement.model.Student; import com.studentmanagement.view.StudentConsoleView; import com.studentmanagement.exception.StudentNotFoundException; import com.studentmanagement.exception.DuplicateStudentException; import com.studentmanagement.util.InputUtil; /** * 主应用程序类,系统入口点 * 初始化MVC组件并启动用户界面 */ public class MainApp { private StudentController controller; private StudentConsoleView view; /** * 构造函数,初始化控制器和视图 */ public MainApp() { this.controller = new StudentController(); this.view = new StudentConsoleView(); } /** * 启动应用程序 */ public void start() { view.displayWelcome(); boolean running = true; while (running) { view.displayMenu(); int choice = InputUtil.getIntInput("", 0, 9); view.displaySeparator(); switch (choice) { case 1: displayAllStudents(); break; case 2: findStudentById(); break; case 3: addNewStudent(); break; case 4: updateStudent(); break; case 5: deleteStudent(); break; case 6: findStudentsByCourse(); break; case 7: findStudentsWithHighGrades(); break; case 8: showCourseAverageGrade(); break; case 9: showAverageAge(); break; case 0: running = false; view.displayExit(); break; } } InputUtil.close(); } /** * 显示所有学生 */ private void displayAllStudents() { view.displayStudentList(controller.getAllStudents()); } /** * 根据ID查找学生 */ private void findStudentById() { int id = InputUtil.getIntInput("请输入学生ID: "); try { Student student = controller.getStudentById(id); view.displayStudent(student); } catch (StudentNotFoundException e) { view.displayErrorMessage(e.getMessage()); } } /** * 添加新学生 */ private void addNewStudent() { int id = InputUtil.getIntInput("请输入学生ID: "); String name = InputUtil.getNonEmptyStringInput("请输入学生姓名: "); int age = InputUtil.getIntInput("请输入学生年龄: ", 1, 100); String course = InputUtil.getNonEmptyStringInput("请输入课程名称: "); double grade = InputUtil.getDoubleInput("请输入成绩: ", 0, 100); Student student = new Student(id, name, age, course, grade); try { controller.addStudent(student); view.displaySuccessMessage("学生添加成功!"); } catch (DuplicateStudentException e) { view.displayErrorMessage(e.getMessage()); } } /** * 更新学生信息 */ private void updateStudent() { int id = InputUtil.getIntInput("请输入要更新的学生ID: "); try { Student existingStudent = controller.getStudentById(id); view.displayStudent(existingStudent); String name = InputUtil.getStringInput("请输入新姓名 (留空保持不变): "); String course = InputUtil.getStringInput("请输入新课程 (留空保持不变): "); // 只有在用户输入了新值时才更新 if (name.isEmpty()) name = existingStudent.getName(); if (course.isEmpty()) course = existingStudent.getCourse(); int age = InputUtil.getIntInput("请输入新年龄 (0保持不变): ", 0, 100); if (age == 0) age = existingStudent.getAge(); double grade = InputUtil.getDoubleInput("请输入新成绩 (-1保持不变): ", -1, 100); if (grade == -1) grade = existingStudent.getGrade(); Student updatedStudent = new Student(id, name, age, course, grade); controller.updateStudent(updatedStudent); view.displaySuccessMessage("学生信息更新成功!"); } catch (StudentNotFoundException e) { view.displayErrorMessage(e.getMessage()); } } /** * 删除学生 */ private void deleteStudent() { int id = InputUtil.getIntInput("请输入要删除的学生ID: "); try { controller.deleteStudent(id); view.displaySuccessMessage("学生删除成功!"); } catch (StudentNotFoundException e) { view.displayErrorMessage(e.getMessage()); } } /** * 根据课程查找学生 */ private void findStudentsByCourse() { String course = InputUtil.getNonEmptyStringInput("请输入课程名称: "); view.displayStudentList(controller.getStudentsByCourse(course)); } /** * 查找成绩优秀的学生 */ private void findStudentsWithHighGrades() { view.displayStudentList(controller.getStudentsByGradeAbove(90)); } /** * 显示课程平均成绩 */ private void showCourseAverageGrade() { String course = InputUtil.getNonEmptyStringInput("请输入课程名称: "); double average = controller.calculateAverageGradeByCourse(course); view.displayStatistics("课程《" + course + "》的平均成绩为: " + String.format("%.2f", average)); } /** * 显示学生平均年龄 */ private void showAverageAge() { double averageAge = controller.calculateAverageAge(); view.displayStatistics("所有学生的平均年龄为: " + String.format("%.2f", averageAge)); } /** * 主方法,应用程序入口 */ public static void main(String[] args) { MainApp app = new MainApp(); app.start(); } }