From 7e279062452a5212763ec3747b8f6f510e889537 Mon Sep 17 00:00:00 2001 From: ava Date: Tue, 21 Apr 2026 00:26:14 +0330 Subject: [PATCH] hal tamrin --- src/CourseRegistrationStarter/Main.java | 24 ++++++++++++++-- .../RegistrationSystem.java | 12 ++++++++ src/CourseRegistrationStarter/Student.java | 28 +++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 8f06373..76fa643 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -10,5 +10,25 @@ public class Main { // 5. Register the student in some courses // 6. Drop one course // 7. Print the final registered course list - } -} + + Course c1 = new Course("Math", "M101", 3); + Course c2 = new Course("Physics", "P202", 2); + Course c3 = new Course("Programming", "CS303", 1); + Student s1 = new Student("Ali", "S123"); + + RegistrationSystem system = new RegistrationSystem(); + + system.addCourseToSystem(c1); + system.addCourseToSystem(c2); + system.addCourseToSystem(c3); + + s1.addCourse(c1); + s1.addCourse(c2); + + s1.dropCourse(c1); + + s1.showRegisteredCourses(); + } + } + + diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 934f222..162120b 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -10,15 +10,27 @@ 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 + //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() + ") Capacity: " + c.getCapacity()); + } + } } diff --git a/src/CourseRegistrationStarter/Student.java b/src/CourseRegistrationStarter/Student.java index 02c11d8..4128a0e 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -23,17 +23,45 @@ 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 + // return false; + if (registeredCourses.contains(c)) { + return false; + } + + 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 + //return false; + if (registeredCourses.contains(c)) { + registeredCourses.remove(c); + c.increaseCapacity(); + return true; + } + return false; + } public void showRegisteredCourses() { // TODO: Print all registered courses + if (registeredCourses.isEmpty()) { + System.out.println("No courses registered."); + return; + } + + for (Course c : registeredCourses) { + System.out.println(c.getCourseName() + " (" + c.getCourseCode() + ")"); + } + } } \ No newline at end of file