You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MVC/StudentController.java

130 lines
3.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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<Student> 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<Student> getStudentsByCourse(String course) {
return studentDAO.getStudentsByCourse(course);
}
/**
* 获取成绩高于指定分数的学生
* @param grade 最低分数
* @return 成绩高于指定分数的学生列表
*/
public List<Student> getStudentsByGradeAbove(double grade) {
return studentDAO.getStudentsByGradeAbove(grade);
}
/**
* 计算指定课程的平均成绩
* @param course 课程名称
* @return 平均成绩
*/
public double calculateAverageGradeByCourse(String course) {
List<Student> students = getStudentsByCourse(course);
if (students.isEmpty()) {
return 0;
}
return students.stream()
.mapToDouble(Student::getGrade)
.average()
.orElse(0);
}
/**
* 获取所有学生的平均年龄
* @return 平均年龄
*/
public double calculateAverageAge() {
List<Student> students = getAllStudents();
if (students.isEmpty()) {
return 0;
}
return students.stream()
.mapToDouble(Student::getAge)
.average()
.orElse(0);
}
}