From c0c283e0c50b2df4b17703bd8f64c71681bfe607 Mon Sep 17 00:00:00 2001 From: Arefe Date: Mon, 20 Apr 2026 20:48:20 +0330 Subject: [PATCH] complete all TODO sections --- src/CourseRegistrationStarter/Main.java | 77 +++++++++++++++++-- .../RegistrationSystem.java | 27 ++++++- src/CourseRegistrationStarter/Student.java | 54 +++++++++++-- 3 files changed, 139 insertions(+), 19 deletions(-) diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 8f06373..282f0cd 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -1,14 +1,75 @@ package CourseRegistrationStarter; +import java.util.Scanner; + 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 course1 = new Course("َAP", "1414", 2); + Course course2 = new Course("DS", "2201", 1); + Course course3 = new Course("DB", "3301", 3); + Student student1 = new Student("Ali", "402222001"); + RegistrationSystem registrationSystem = new RegistrationSystem(); + registrationSystem.addCourseToSystem(course1); + registrationSystem.addCourseToSystem(course2); + registrationSystem.addCourseToSystem(course3); + + boolean isChosen = false; + while (!isChosen) + { + System.out.println("\n--- Menu ---"); + System.out.println("1) Add course"); + System.out.println("2) Drop course"); + System.out.println("3) Show registered courses"); + System.out.println("4) Show all courses"); + System.out.println("5) Exit"); + System.out.print("Enter your choice: "); + Scanner in = new Scanner(System.in); + int chosen = in.nextInt(); + if (chosen == 1) + { + System.out.print("Enter Course Code to add: "); + String codeToAdd = in.next(); + Course courseToAdd = registrationSystem.findCourseByCode(codeToAdd); + + if (courseToAdd != null) + { + student1.addCourse(courseToAdd); + } + else + { + System.out.println("Error: Course not found in system."); + } + } + else if (chosen == 2) + { + System.out.println("\n--- Drop Course ---"); + System.out.print("Enter Course Code to drop: "); + String codeToDrop = in.next(); + Course courseToDrop = registrationSystem.findCourseByCode(codeToDrop); + + if (courseToDrop != null) + { + student1.dropCourse(courseToDrop); + } + } + else if (chosen == 3) + { + System.out.println("\n--- Registered Courses ---"); + student1.showRegisteredCourses(); + } + else if (chosen == 4) + { + System.out.println("\n--- All Available Courses ---"); + registrationSystem.showAllCourses(); + } + else if (chosen == 5) + { + isChosen = true; + } + else + { + System.out.println("Invalid! Please enter a number between 1 and 5."); + } + } } } diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 934f222..13c7942 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -9,16 +9,35 @@ 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 a : courses) + { + if (a.getCourseCode().equals(code)) + { + return a; + } + } + return null; + } + public Course findCourseByName(String name) + { + for (Course a : courses) + { + if (a.getCourseName().equals(name)) + { + return a; + } + } 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..7fa838f 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -21,19 +21,59 @@ 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.size() == 5) + { + System.out.println("Maximum course limit (5) reached."); + return false; + } + for (Course a : registeredCourses) + { + if (a.getCourseCode().equals(c.getCourseCode())) + { + System.out.println("Error: you are already registered for this course!"); + return false; + } + } + if (c.getCapacity() > 0) + { + registeredCourses.add(c); + c.reduceCapacity(); + System.out.println("Registration was successful."); + 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; + Course courseToRempove = null; + for (Course a : registeredCourses) + { + if (a.getCourseCode().equals(c.getCourseCode())) + { + courseToRempove = a; + break; + } + } + if (courseToRempove == null) + { + System.out.println("Error: you are not registered for this course!"); + return false; + } + registeredCourses.remove(courseToRempove); + System.out.println("Successfully dropped course."); + c.increaseCapacity(); + return true; } public void showRegisteredCourses() { - // TODO: Print all registered courses + if (registeredCourses.isEmpty()) + { + System.out.println("No courses registered."); + return; + } + for(Course a : registeredCourses) + { + System.out.println(a.getCourseName() + " - " + a.getCourseCode()); + } } } \ No newline at end of file