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.
employee/CourseManagerImpl.java

75 lines
2.2 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.

/**
* 课程管理实现类
* 提供课程管理接口的具体实现
* 遵循单一职责原则专注于课程的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());
}
}