From b6c2286570289b4d346729cf426508ffd4665136 Mon Sep 17 00:00:00 2001 From: Zahra Gharagozloo Mazlaghan Date: Mon, 20 Apr 2026 22:01:20 +0330 Subject: [PATCH] Compeleted the Student class (and Bonus). --- .../RegistrationSystem.java | 9 +------ src/CourseRegistrationStarter/Student.java | 24 ++++++++++++++----- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 04ae66f..bd5f2bf 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -10,14 +10,7 @@ public class RegistrationSystem { } public void addCourseToSystem(Course c) { - Scanner scanner = new Scanner(System.in); - - String courseName = scanner.next(); - String courseCode = scanner.next(); - int capacity = scanner.nextInt(); - - Course newCourse = new Course(courseName, courseCode, capacity); - courses.add(newCourse); + courses.add(c); } public Course findCourseByCode(String code) { diff --git a/src/CourseRegistrationStarter/Student.java b/src/CourseRegistrationStarter/Student.java index 0d7726e..567c8ae 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -21,18 +21,30 @@ public class Student { } public boolean addCourse(Course c) { - - return false; + if ((registeredCourses.size() < 3)&&(!(registeredCourses.contains(c))&&(c.getCapacity() > 0))) { + registeredCourses.add(c); + } + else if (registeredCourses.size() >= 3) + System.out.println("You can only attend 3 courses!"); + else if (registeredCourses.contains(c)) + System.out.println("You can't register twice!"); + return c.reduceCapacity(); } 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 + registeredCourses.remove(c); + int temp = c.getCapacity(); + c.increaseCapacity(); + if ((temp > c.getCapacity())&&(!(registeredCourses.contains(c)))) + return true; return false; } public void showRegisteredCourses() { - // TODO: Print all registered courses + for (Course i : registeredCourses) + { + System.out.println(i.getCourseName()); + System.out.println(i.getCourseCode()); + } } } \ No newline at end of file