diff --git a/StudentController.java b/StudentController.java new file mode 100644 index 0000000..22a2188 --- /dev/null +++ b/StudentController.java @@ -0,0 +1,130 @@ +package com.studentmanagement.controller; + +import com.studentmanagement.model.Student; +import com.studentmanagement.model.StudentDAO; +import com.studentmanagement.exception.StudentNotFoundException; +import com.studentmanagement.exception.DuplicateStudentException; + +import java.util.List; + +/** + * 学生控制器,负责处理学生相关的业务逻辑 + * MVC模式中的Controller部分,连接Model和View + */ +public class StudentController { + private StudentDAO studentDAO; + + /** + * 构造函数 + */ + public StudentController() { + this.studentDAO = StudentDAO.getInstance(); + } + + /** + * 获取所有学生 + * @return 学生列表 + */ + public List getAllStudents() { + return studentDAO.getAllStudents(); + } + + /** + * 根据ID获取学生 + * @param id 学生ID + * @return 学生对象 + * @throws StudentNotFoundException 当学生不存在时抛出 + */ + public Student getStudentById(int id) throws StudentNotFoundException { + Student student = studentDAO.getStudentById(id); + if (student == null) { + throw new StudentNotFoundException(id); + } + return student; + } + + /** + * 添加学生 + * @param student 要添加的学生 + * @throws DuplicateStudentException 当学生ID已存在时抛出 + */ + public void addStudent(Student student) throws DuplicateStudentException { + boolean added = studentDAO.addStudent(student); + if (!added) { + throw new DuplicateStudentException(student.getId()); + } + } + + /** + * 更新学生信息 + * @param student 要更新的学生 + * @throws StudentNotFoundException 当学生不存在时抛出 + */ + public void updateStudent(Student student) throws StudentNotFoundException { + boolean updated = studentDAO.updateStudent(student); + if (!updated) { + throw new StudentNotFoundException(student.getId()); + } + } + + /** + * 删除学生 + * @param id 学生ID + * @throws StudentNotFoundException 当学生不存在时抛出 + */ + public void deleteStudent(int id) throws StudentNotFoundException { + boolean deleted = studentDAO.deleteStudent(id); + if (!deleted) { + throw new StudentNotFoundException(id); + } + } + + /** + * 根据课程查找学生 + * @param course 课程名称 + * @return 该课程的学生列表 + */ + public List getStudentsByCourse(String course) { + return studentDAO.getStudentsByCourse(course); + } + + /** + * 获取成绩高于指定分数的学生 + * @param grade 最低分数 + * @return 成绩高于指定分数的学生列表 + */ + public List getStudentsByGradeAbove(double grade) { + return studentDAO.getStudentsByGradeAbove(grade); + } + + /** + * 计算指定课程的平均成绩 + * @param course 课程名称 + * @return 平均成绩 + */ + public double calculateAverageGradeByCourse(String course) { + List students = getStudentsByCourse(course); + if (students.isEmpty()) { + return 0; + } + return students.stream() + .mapToDouble(Student::getGrade) + .average() + .orElse(0); + } + + /** + * 获取所有学生的平均年龄 + * @return 平均年龄 + */ + public double calculateAverageAge() { + List students = getAllStudents(); + if (students.isEmpty()) { + return 0; + } + return students.stream() + .mapToDouble(Student::getAge) + .average() + .orElse(0); + } +} \ No newline at end of file