From c96fbd2d52ad2b6781a01b1295c3a9d95a6ffc41 Mon Sep 17 00:00:00 2001 From: fdai7600 Date: Thu, 8 Feb 2024 18:51:07 +0100 Subject: [PATCH] Added the new Enroll Student in course --- src/main/java/org/example/Administration.java | 22 +++++++++++++++++++ src/main/java/org/example/Main.java | 9 ++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/example/Administration.java b/src/main/java/org/example/Administration.java index e3fe0ee..768006d 100644 --- a/src/main/java/org/example/Administration.java +++ b/src/main/java/org/example/Administration.java @@ -1,6 +1,7 @@ package org.example; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.Scanner; @@ -418,6 +419,27 @@ public class Administration { System.out.println("Course " + courseName + " (" + courseID + ") created successfully."); } } + + public boolean enrollStudentInCourse(String studentId, String courseId) { + Student student = findStudentById(studentId); + Course course = findCourseByID(courseId); + if (student != null && course != null) { + // If the course is not already enrolled + if (!student.getCourseGrades().containsKey(course)) { + student.getCourseGrades().put(course, "Not Graded"); + System.out.println("Student " + studentId + " enrolled in course " + courseId + "."); + return true; + } else { + System.out.println("Student " + studentId + " is already enrolled in course " + courseId + "."); + return false; + } + } else { + if (student == null) System.out.println("Student ID not found."); + if (course == null) System.out.println("Course ID not found."); + return false; + } + } + } diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index 49a7587..bcc2022 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -40,8 +40,13 @@ public class Main { // Register User administration.registerUser(); break; - case 2: - break; + case 2: // Enroll Student in Course + System.out.println("Enter Student ID:"); + String studentId = scanner.next(); + System.out.println("Enter Course ID:"); + String courseId = scanner.next(); + administration.enrollStudentInCourse(studentId, courseId); + break; case 3: // Enroll Prof to Course break;