From 714be1d6fbe005a3062609127f02f570e54df5d7 Mon Sep 17 00:00:00 2001 From: Arshia Date: Mon, 6 Jul 2026 21:19:29 +0330 Subject: [PATCH] WS2 bonus concluded! --- src/CourseRegistrationStarter/Main.java | 16 +++++++++++++++- .../RegistrationSystem.java | 15 +++++++++++++++ src/CourseRegistrationStarter/Student.java | 15 ++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 8f06373..23d54de 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -1,14 +1,28 @@ package CourseRegistrationStarter; - +import java.util.ArrayList; public class Main { public static void main(String[] args) { // TODO: // 1. Create at least 3 courses + ArrayList courses = new ArrayList(); + courses.add(new Course("Advanced Programming", "AP1404", 2)); + courses.add(new Course("Data Structures", "DS2201", 1)); + courses.add(new Course(" Database Systems", "DB3301", 3)); + // 2. Create 1 student + Student student = new Student( "Ali","402222001"); + // 3. Create a registration system + RegistrationSystem regi = new RegistrationSystem(); // 4. Add courses to the system + for(Course c:courses) + regi.addCourseToSystem(c); // 5. Register the student in some courses + student.addCourse(courses.get(0)); + student.addCourse(courses.get(1)); // 6. Drop one course + student.dropCourse(courses.get(1)); // 7. Print the final registered course list + student.showRegisteredCourses(); } } diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 934f222..6eba453 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -10,15 +10,30 @@ 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()==code) + return c; + return null; + } + + public Course searchByCourseName(String name) { + // bonus point 3 + for (Course c:courses) + if(c.getCourseName()==name) + 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()+" "+c.getCapacity() ); + } } diff --git a/src/CourseRegistrationStarter/Student.java b/src/CourseRegistrationStarter/Student.java index 02c11d8..e0d31d6 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -23,17 +23,30 @@ 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; + //bonus point1 & bonus point2 + if(c.getCapacity()<0 || registeredCourses.contains(c)||registeredCourses.size()==3) + return false; + c.reduceCapacity(); + registeredCourses.add(c); + return true; + } 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.contains(c)){ + registeredCourses.remove(registeredCourses.indexOf(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