-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourseService.java
More file actions
112 lines (96 loc) · 3.63 KB
/
CourseService.java
File metadata and controls
112 lines (96 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package org.app.service;
import org.app.model.*;
import java.util.*;
import java.util.stream.Collectors;
/**
* Course Management Service for BUP UCAM Assignment Tracker
* Simplified to avoid persistence issues - courses will be created fresh each session
*/
public class CourseService {
private Map<String, Course> courses;
private int courseCounter;
public CourseService() {
this.courses = new HashMap<>();
this.courseCounter = 1;
System.out.println("✓ CourseService initialized (in-memory storage)");
}
/**
* Create a new course
*/
public Course createCourse(String courseName, String courseCode, String department,
int creditHours, String semester, Teacher instructor) {
String courseId = "CRS-" + String.format("%04d", courseCounter++);
Course course = new Course(courseId, courseName, courseCode, department,
creditHours, semester, instructor);
courses.put(courseId, course);
instructor.addCourse(course);
System.out.println("✓ Course created successfully: " + courseName);
return course;
}
/**
* Enroll student in course
*/
public void enrollStudent(String courseId, Student student) {
Course course = courses.get(courseId);
if (course == null) {
throw new IllegalArgumentException("Course not found");
}
course.enrollStudent(student);
System.out.println("✓ Student enrolled: " + student.getName() + " in " + course.getCourseName());
}
/**
* Get courses by department
*/
public List<Course> getCoursesByDepartment(String department) {
return courses.values().stream()
.filter(course -> course.getDepartment().equalsIgnoreCase(department))
.collect(Collectors.toList());
}
/**
* Get courses by semester
*/
public List<Course> getCoursesBySemester(String semester) {
return courses.values().stream()
.filter(course -> course.getSemester().equalsIgnoreCase(semester))
.collect(Collectors.toList());
}
/**
* Get courses taught by teacher
*/
public List<Course> getCoursesByTeacher(Teacher teacher) {
return courses.values().stream()
.filter(course -> course.getInstructor().equals(teacher))
.collect(Collectors.toList());
}
/**
* Get courses for student
*/
public List<Course> getCoursesForStudent(Student student) {
return courses.values().stream()
.filter(course -> course.getEnrolledStudents().contains(student))
.collect(Collectors.toList());
}
/**
* Display course statistics
*/
public void displayCourseStatistics(Course course) {
System.out.println("=== Course Statistics ===");
System.out.println("Course: " + course.getCourseName());
System.out.println("Enrolled Students: " + course.getEnrolledStudents().size());
System.out.println("Total Assignments: " + course.getAssignments().size());
System.out.println("Instructor: " + course.getInstructor().getName());
}
// Getters
public Course getCourse(String courseId) {
return courses.get(courseId);
}
public List<Course> getAllCourses() {
return new ArrayList<>(courses.values());
}
public Course findCourseByCode(String courseCode) {
return courses.values().stream()
.filter(course -> course.getCourseCode().equalsIgnoreCase(courseCode))
.findFirst()
.orElse(null);
}
}