Package com.project.system
Class CourseService
java.lang.Object
com.project.system.CourseService
The
CourseService
class handles CRUD operations related to courses in the MongoDB database.
It allows adding, deleting, searching, and displaying course information.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final com.mongodb.client.MongoCollection
<org.bson.Document> The MongoDB collection for storing course information.private final com.mongodb.client.MongoCollection
<org.bson.Document> The MongoDB collection for storing student information. -
Constructor Summary
ConstructorsConstructorDescriptionCourseService
(com.mongodb.client.MongoDatabase database) Constructs a newCourseService
instance using the provided MongoDB database connection. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Adds a new course to the MongoDB collection.protected void
deleteCourse
(String name) Deletes a course by its name from the MongoDB collection.protected void
Displays all courses stored in the MongoDB collection.protected void
searchCourse
(String name) Searches for courses by name (case-insensitive) using a regular expression.
-
Field Details
-
courseCollection
private final com.mongodb.client.MongoCollection<org.bson.Document> courseCollectionThe MongoDB collection for storing course information. -
studentCollection
private final com.mongodb.client.MongoCollection<org.bson.Document> studentCollectionThe MongoDB collection for storing student information.
-
-
Constructor Details
-
CourseService
public CourseService(com.mongodb.client.MongoDatabase database) Constructs a newCourseService
instance using the provided MongoDB database connection.- Parameters:
database
- The MongoDB database to interact with.
-
-
Method Details
-
addCourse
Adds a new course to the MongoDB collection.- Parameters:
course
- TheCourse
object representing the course details.
-
displayAllCourses
protected void displayAllCourses()Displays all courses stored in the MongoDB collection.For each course, it prints the course name, description, and the list of enrolled students (if any).
-
deleteCourse
Deletes a course by its name from the MongoDB collection.Also unenrolls students from the deleted course by removing the course reference from their records.
- Parameters:
name
- The name of the course to delete.
-
searchCourse
Searches for courses by name (case-insensitive) using a regular expression.- Parameters:
name
- The name of the course to search for (or a partial name).
-