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;
}
}

浙公网安备 33010602011771号