Class StudentService

java.lang.Object
com.project.system.StudentService

public class StudentService extends Object
The StudentService class provides CRUD operations for managing students in the MongoDB database. It handles adding, updating, deleting, and searching student records.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final com.mongodb.client.MongoCollection<org.bson.Document>
    The MongoDB collection for storing student information.
  • Constructor Summary

    Constructors
    Constructor
    Description
    StudentService(com.mongodb.client.MongoDatabase database)
    Constructs a new StudentService instance with the specified MongoDB database.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    Adds a new student to the MongoDB collection.
    protected void
    Deletes a student by their ID from the MongoDB collection.
    protected void
    Displays all students stored in the MongoDB collection.
    protected void
    Searches for a student by their ID in the MongoDB collection.
    protected void
    updateStudent(String id, String name, String email, String course)
    Updates the details of an existing student by ID.

    Methods inherited from class java.lang.Object

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

    • studentCollection

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

    • StudentService

      public StudentService(com.mongodb.client.MongoDatabase database)
      Constructs a new StudentService instance with the specified MongoDB database.
      Parameters:
      database - The MongoDB database used for storing student data.
  • Method Details

    • addStudent

      protected void addStudent(Student student)
      Adds a new student to the MongoDB collection.

      Also updates the course document to include the student in its enrolled list.

      Parameters:
      student - The Student object representing the student to add.
    • displayAllStudents

      protected void displayAllStudents()
      Displays all students stored in the MongoDB collection.

      Prints the ID, name, email, and course of each student.

    • updateStudent

      protected void updateStudent(String id, String name, String email, String course)
      Updates the details of an existing student by ID.

      If the student is moved to a new course, it removes them from the old course's list and adds them to the new course's list.

      Parameters:
      id - The MongoDB ID of the student to update.
      name - The new name of the student (if not empty).
      email - The new email of the student (if not empty).
      course - The new course of the student (if not empty).
    • deleteStudent

      protected void deleteStudent(String id)
      Deletes a student by their ID from the MongoDB collection.

      Also removes the student from the course they were enrolled in.

      Parameters:
      id - The MongoDB ID of the student to delete.
    • searchStudent

      protected void searchStudent(String id)
      Searches for a student by their ID in the MongoDB collection.

      Prints the student details if found; otherwise, displays an appropriate message.

      Parameters:
      id - The MongoDB ID of the student to search for.