Package com.project.system
Class StudentService
java.lang.Object
com.project.system.StudentService
The
StudentService
class provides CRUD operations for managing students in the MongoDB database.
It handles adding, updating, deleting, and searching student records.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final com.mongodb.client.MongoCollection
<org.bson.Document> The MongoDB collection for storing student information. -
Constructor Summary
ConstructorsConstructorDescriptionStudentService
(com.mongodb.client.MongoDatabase database) Constructs a newStudentService
instance with the specified MongoDB database. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addStudent
(Student student) Adds a new student to the MongoDB collection.protected void
deleteStudent
(String id) Deletes a student by their ID from the MongoDB collection.protected void
Displays all students stored in the MongoDB collection.protected void
searchStudent
(String id) 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.
-
Field Details
-
studentCollection
private final com.mongodb.client.MongoCollection<org.bson.Document> studentCollectionThe MongoDB collection for storing student information.
-
-
Constructor Details
-
StudentService
public StudentService(com.mongodb.client.MongoDatabase database) Constructs a newStudentService
instance with the specified MongoDB database.- Parameters:
database
- The MongoDB database used for storing student data.
-
-
Method Details
-
addStudent
Adds a new student to the MongoDB collection.Also updates the course document to include the student in its enrolled list.
- Parameters:
student
- TheStudent
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
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
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
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.
-