11.6

public class StudentServiceImpl implements StudentService {
private SelectionDAO selectionDAO = new SelectionDAOImpl();
private CourseDAO courseDAO = new CourseDAOImpl();

@Override
public boolean selectCourse(String courseId, String studentId) {
    // 1. 验证课程是否已满
    Course course = courseDAO.findById(courseId);
    if (course.getCurrentNum() >= course.getMaxNum()) {
        return false;
    }
    // 2. 验证是否已选该课程
    if (selectionDAO.exists(courseId, studentId)) {
        return false;
    }
    // 3. 新增选课记录 + 更新课程当前人数
    selectionDAO.insert(new Selection(courseId, studentId));
    courseDAO.updateCurrentNum(courseId, course.getCurrentNum() + 1);
    return true;
}

}

posted @ 2025-11-10 17:02  Cx330。  阅读(1)  评论(0)    收藏  举报