From cf89e4d1b8e7fc060440de85706426e52eec9f43 Mon Sep 17 00:00:00 2001 From: Matin-Ardestani Date: Mon, 20 Apr 2026 23:59:00 +0330 Subject: [PATCH] Complete Student class (addCourse, dropCourse and showRegisteredCourses methods) --- src/CourseRegistrationStarter/Student.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/CourseRegistrationStarter/Student.java b/src/CourseRegistrationStarter/Student.java index 02c11d8..6d38f7c 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -23,6 +23,17 @@ public class Student { public boolean addCourse(Course c) { // TODO: Register the student in the course if capacity allows // Return true if registration was successful, otherwise false + + if(registeredCourses.contains(c)){ + System.out.printf("%s is already registered for %s\n", c.getCourseName(), name); + return false; + } + + if(c.reduceCapacity()){ // if enough capacity, reduction happens in this line + registeredCourses.add(c); + System.out.printf("%s registered successfully.\n", c.getCourseName()); + return true; + } return false; } @@ -30,10 +41,20 @@ public class Student { // TODO: Remove the course from the student's list // Restore the course capacity if removal was successful // Return true if the course was dropped, otherwise false + + if(registeredCourses.contains(c)){ + registeredCourses.remove(c); + c.increaseCapacity(); + System.out.printf("%s removed successfully.\n", c.getCourseName()); + return true; + } return false; } public void showRegisteredCourses() { // TODO: Print all registered courses + for(Course thisCourse : registeredCourses){ + System.out.printf("%s <%s>\n", thisCourse.getCourseName(), thisCourse.getCourseCode()); + } } } \ No newline at end of file