From cf89e4d1b8e7fc060440de85706426e52eec9f43 Mon Sep 17 00:00:00 2001 From: Matin-Ardestani Date: Mon, 20 Apr 2026 23:59:00 +0330 Subject: [PATCH 1/4] 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 From 759133554481b84af8def188668d55081103b1a5 Mon Sep 17 00:00:00 2001 From: Matin-Ardestani Date: Tue, 21 Apr 2026 00:06:06 +0330 Subject: [PATCH 2/4] Complete RegistrationSystem class (addCourseToSystem, findCourseByCode and showAllCourses methods) --- src/CourseRegistrationStarter/RegistrationSystem.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 934f222..fabedaf 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -10,15 +10,25 @@ public class RegistrationSystem { public void addCourseToSystem(Course c) { // TODO: Add the course to the system + courses.add(c); } public Course findCourseByCode(String code) { // TODO: Search for a course by course code // Return the matching course if found, otherwise return null + for(Course thisCourse : courses){ + if(thisCourse.getCourseCode() == code){ + return thisCourse; + } + } return null; } public void showAllCourses() { // TODO: Print all courses in the system + System.out.println("Course Name | Course Code | Capacity"); + for(Course thisCourse : courses){ + System.out.printf("%s | %s | %d\n", thisCourse.getCourseName(), thisCourse.getCourseCode(), thisCourse.getCapacity()); + } } } From 680d7afeee33c963777a708c40606ffbe9de9321 Mon Sep 17 00:00:00 2001 From: Matin-Ardestani Date: Tue, 21 Apr 2026 00:20:54 +0330 Subject: [PATCH 3/4] Compelete Main class with an example scenario) --- src/CourseRegistrationStarter/Main.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 8f06373..06b727b 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -4,11 +4,34 @@ public class Main { public static void main(String[] args) { // TODO: // 1. Create at least 3 courses + Course course1 = new Course("Advanced Programming", "AP1404", 2); + Course course2 = new Course("Data Structures", "Ds2201", 1); + Course course3 = new Course("Database Systems", "DB3301", 3); + // 2. Create 1 student + Student student1 = new Student("Ali", "402222001"); + // 3. Create a registration system + RegistrationSystem SBU = new RegistrationSystem(); + // 4. Add courses to the system + SBU.addCourseToSystem(course1); + SBU.addCourseToSystem(course2); + SBU.addCourseToSystem(course3); + // 5. Register the student in some courses + student1.addCourse(course1); + student1.addCourse(course2); + // 6. Drop one course + student1.dropCourse(course2); + // 7. Print the final registered course list + System.out.printf("%s registered courses:\n", student1.getName()); + student1.showRegisteredCourses(); + + System.out.println("\nRemaining informations of courses are as follows: "); + SBU.showAllCourses(); + } } From 193137771e2dcf6d53eefd7e8445743cf69d6cc8 Mon Sep 17 00:00:00 2001 From: Matin-Ardestani Date: Tue, 21 Apr 2026 00:28:53 +0330 Subject: [PATCH 4/4] add bonus features --- src/CourseRegistrationStarter/Main.java | 2 +- src/CourseRegistrationStarter/RegistrationSystem.java | 9 +++++++++ src/CourseRegistrationStarter/Student.java | 7 ++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 06b727b..2a295bb 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -27,7 +27,7 @@ public class Main { student1.dropCourse(course2); // 7. Print the final registered course list - System.out.printf("%s registered courses:\n", student1.getName()); + System.out.printf("\n%s\'s registered courses:\n", student1.getName()); student1.showRegisteredCourses(); System.out.println("\nRemaining informations of courses are as follows: "); diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index fabedaf..402afa6 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -24,6 +24,15 @@ public class RegistrationSystem { return null; } + public Course findCourseByName(String name){ // search by course name + for(Course thisCourse : courses){ + if(thisCourse.getCourseName() == name){ + return thisCourse; + } + } + return null; + } + public void showAllCourses() { // TODO: Print all courses in the system System.out.println("Course Name | Course Code | Capacity"); diff --git a/src/CourseRegistrationStarter/Student.java b/src/CourseRegistrationStarter/Student.java index 6d38f7c..73e0cf0 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -5,6 +5,7 @@ public class Student { private String name; private String studentId; private ArrayList registeredCourses; + private int allowedCourses = 2; public Student(String name, String studentId) { this.name = name; @@ -24,11 +25,15 @@ public class Student { // TODO: Register the student in the course if capacity allows // Return true if registration was successful, otherwise false - if(registeredCourses.contains(c)){ + if(registeredCourses.contains(c)){ // Preventing duplicate course registration System.out.printf("%s is already registered for %s\n", c.getCourseName(), name); return false; } + if(registeredCourses.size() >= allowedCourses){ // Limiting the number of courses per student + System.out.printf("%s has reached his limit for registration.\n", name); + } + if(c.reduceCapacity()){ // if enough capacity, reduction happens in this line registeredCourses.add(c); System.out.printf("%s registered successfully.\n", c.getCourseName());