|
|
/**
|
|
|
* 课程管理实现类
|
|
|
* 提供课程管理接口的具体实现
|
|
|
* 遵循单一职责原则,专注于课程的CRUD操作
|
|
|
*/
|
|
|
package com.employeetraining.course;
|
|
|
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
public class CourseManagerImpl implements CourseManager {
|
|
|
// 使用线程安全的集合存储课程
|
|
|
private final Map<String, Course> courseMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
@Override
|
|
|
public void addCourse(Course course) {
|
|
|
if (course == null) {
|
|
|
throw new IllegalArgumentException("Course cannot be null");
|
|
|
}
|
|
|
if (course.getCourseId() == null || course.getCourseId().trim().isEmpty()) {
|
|
|
throw new IllegalArgumentException("Course ID cannot be empty");
|
|
|
}
|
|
|
courseMap.put(course.getCourseId(), course);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean updateCourse(Course course) {
|
|
|
if (course == null || course.getCourseId() == null) {
|
|
|
return false;
|
|
|
}
|
|
|
if (courseMap.containsKey(course.getCourseId())) {
|
|
|
courseMap.put(course.getCourseId(), course);
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean deleteCourse(String courseId) {
|
|
|
if (courseId == null) {
|
|
|
return false;
|
|
|
}
|
|
|
return courseMap.remove(courseId) != null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Optional<Course> findCourseById(String courseId) {
|
|
|
if (courseId == null) {
|
|
|
return Optional.empty();
|
|
|
}
|
|
|
return Optional.ofNullable(courseMap.get(courseId));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<Course> findCoursesByCategory(String category) {
|
|
|
if (category == null) {
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
return courseMap.values().stream()
|
|
|
.filter(course -> category.equalsIgnoreCase(course.getCategory()))
|
|
|
.toList();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<Course> findRequiredCourses() {
|
|
|
return courseMap.values().stream()
|
|
|
.filter(Course::isRequired)
|
|
|
.toList();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<Course> getAllCourses() {
|
|
|
return new ArrayList<>(courseMap.values());
|
|
|
}
|
|
|
} |