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.
56 lines
1.8 KiB
56 lines
1.8 KiB
package com.student.service;
|
|
|
|
import com.student.bean.Course;
|
|
import com.student.bean.Enrollment;
|
|
import com.student.dao.CourseDao;
|
|
import com.student.dao.EnrollmentDao;
|
|
import com.student.dao.impl.CourseDaoImpl;
|
|
import com.student.dao.impl.EnrollmentDaoImpl;
|
|
|
|
import java.util.List;
|
|
|
|
public class EnrollmentService {
|
|
|
|
private final EnrollmentDao enrollmentDao;
|
|
private final CourseDao courseDao;
|
|
|
|
public EnrollmentService() {
|
|
this(new EnrollmentDaoImpl(), new CourseDaoImpl());
|
|
}
|
|
|
|
EnrollmentService(EnrollmentDao enrollmentDao, CourseDao courseDao) {
|
|
this.enrollmentDao = enrollmentDao;
|
|
this.courseDao = courseDao;
|
|
}
|
|
|
|
public boolean enroll(int studentId, int courseId) {
|
|
if (enrollmentDao.exists(studentId, courseId)) {
|
|
throw new RuntimeException("已选过该课程");
|
|
}
|
|
Course course = courseDao.findById(courseId);
|
|
if (course == null) {
|
|
throw new RuntimeException("课程不存在");
|
|
}
|
|
int count = enrollmentDao.countByCourse(courseId);
|
|
if (course.getMaxStudents() != null && count >= course.getMaxStudents()) {
|
|
throw new RuntimeException("课程已满");
|
|
}
|
|
return enrollmentDao.enroll(studentId, courseId);
|
|
}
|
|
|
|
public boolean cancel(int studentId, int courseId) {
|
|
return enrollmentDao.cancel(studentId, courseId);
|
|
}
|
|
|
|
public List<Enrollment> findByStudent(int studentId) {
|
|
return enrollmentDao.findByStudent(studentId);
|
|
}
|
|
|
|
public List<Enrollment> findByCourse(int courseId) {
|
|
return enrollmentDao.findByCourse(courseId);
|
|
}
|
|
|
|
public int countByCourse(int courseId) {
|
|
return enrollmentDao.countByCourse(courseId);
|
|
}
|
|
}
|
|
|