Class CourseService

java.lang.Object
com.project.system.CourseService

public class CourseService extends Object
The CourseService class handles CRUD operations related to courses in the MongoDB database. It allows adding, deleting, searching, and displaying course information.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private 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

    Constructors
    Constructor
    Description
    CourseService(com.mongodb.client.MongoDatabase database)
    Constructs a new CourseService instance using the provided MongoDB database connection.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    addCourse(Course course)
    Adds a new course to the MongoDB collection.
    protected void
    Deletes a course by its name from the MongoDB collection.
    protected void
    Displays all courses stored in the MongoDB collection.
    protected void
    Searches for courses by name (case-insensitive) using a regular expression.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • courseCollection

      private final com.mongodb.client.MongoCollection<org.bson.Document> courseCollection
      The MongoDB collection for storing course information.
    • studentCollection

      private final com.mongodb.client.MongoCollection<org.bson.Document> studentCollection
      The MongoDB collection for storing student information.
  • Constructor Details

    • CourseService

      public CourseService(com.mongodb.client.MongoDatabase database)
      Constructs a new CourseService instance using the provided MongoDB database connection.
      Parameters:
      database - The MongoDB database to interact with.
  • Method Details

    • addCourse

      protected void addCourse(Course course)
      Adds a new course to the MongoDB collection.
      Parameters:
      course - The Course 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

      protected void deleteCourse(String name)
      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

      protected void searchCourse(String name)
      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).