From 8395d8129656b7b6415a893b97750daa908a9e6a Mon Sep 17 00:00:00 2001 From: kasra Date: Thu, 23 Jul 2026 18:57:23 +0330 Subject: [PATCH] WS2 --- src/CourseRegistrationStarter/Main.java | 32 +++++++++++++------ .../RegistrationSystem.java | 15 ++++++--- src/CourseRegistrationStarter/Student.java | 19 +++++++---- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 8f06373..5530681 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -2,13 +2,27 @@ package CourseRegistrationStarter; public class Main { public static void main(String[] args) { - // TODO: - // 1. Create at least 3 courses - // 2. Create 1 student - // 3. Create a registration system - // 4. Add courses to the system - // 5. Register the student in some courses - // 6. Drop one course - // 7. Print the final registered course list + Course c1 = new Course("Advanced Programming", "AP1404", 2); + Course c2 = new Course("Data Structures", "DS2201", 1); + Course c3 = new Course("Database Systems", "DB3301", 3); + + Student student = new Student("Ali", "402222001"); + + RegistrationSystem system = new RegistrationSystem(); + + system.addCourseToSystem(c1); + system.addCourseToSystem(c2); + system.addCourseToSystem(c3); + + student.addCourse(c1); + student.addCourse(c2); + + student.dropCourse(c1); + + System.out.println("Registered Courses:"); + student.showRegisteredCourses(); + + System.out.println("\nAll Courses in System:"); + system.showAllCourses(); } -} +} \ No newline at end of file diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 934f222..3456907 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -9,16 +9,21 @@ 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 c : courses) { + if (c.getCourseCode().equals(code)) { + return c; + } + } return null; } public void showAllCourses() { - // TODO: Print all courses in the system + for (Course c : courses) { + System.out.println(c.getCourseName() + " - " + c.getCourseCode() + " - Remaining Capacity: " + c.getCapacity()); + } } -} +} \ No newline at end of file diff --git a/src/CourseRegistrationStarter/Student.java b/src/CourseRegistrationStarter/Student.java index 02c11d8..f9c7df7 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -21,19 +21,26 @@ 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) && c.getCapacity() > 0) { + if (c.reduceCapacity()) { + registeredCourses.add(c); + return true; + } + } return false; } public boolean dropCourse(Course c) { - // 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.remove(c)) { + c.increaseCapacity(); + return true; + } return false; } public void showRegisteredCourses() { - // TODO: Print all registered courses + for (Course c : registeredCourses) { + System.out.println(c.getCourseName() + " - " + c.getCourseCode()); + } } } \ No newline at end of file -- 2.54.0