From de5cddfe119afaff0082f34727cfabe052ed75d2 Mon Sep 17 00:00:00 2001 From: Farnam Jahangard Date: Sun, 19 Apr 2026 21:44:23 -0700 Subject: [PATCH] Finished "TODO" and Bonus sections --- src/CourseRegistrationStarter/Main.java | 29 ++++++++++++----- .../RegistrationSystem.java | 25 +++++++++++++-- src/CourseRegistrationStarter/Student.java | 31 +++++++++++++++---- 3 files changed, 68 insertions(+), 17 deletions(-) diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 8f06373..aea893a 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -2,13 +2,26 @@ 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 AP = new Course("Advanced Programming", "4041122334", 40); + Course FM = new Course("Foundation of Math", "4041212334", 30); + Course GM = new Course("General Math","4041234123",60); + + Student farnam = new Student("Farnam", "404111234"); + farnam.setLimitOfCourses(3); + RegistrationSystem regSys = new RegistrationSystem(); + + regSys.addCourseToSystem(AP); + regSys.addCourseToSystem(FM); + regSys.addCourseToSystem(GM); + + farnam.addCourse(AP); + farnam.addCourse(FM); + farnam.addCourse(GM); + + farnam.dropCourse(FM); + + farnam.showRegisteredCourses(); + + regSys.showAllCourses(); } } diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 934f222..719e159 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 (int i = 0; i < courses.size(); i++) { + if (courses.get(i).getCourseCode().equals(code)){ + return courses.get(i); + } + } + return null; + } + + public Course findCourseByName(String name) { + for (int i = 0; i < courses.size(); i++) { + if (courses.get(i).getCourseCode().equals(name)){ + return courses.get(i); + } + } return null; } public void showAllCourses() { // TODO: Print all courses in the system + System.out.printf("%25s %15s %18s", "Name","Course code","Remaining capacity"); + System.out.print('\n'); + + for (int i = 0; i < courses.size(); i++) { + System.out.printf("%25s %15s %d", courses.get(i).getCourseName() , courses.get(i).getCourseCode(), courses.get(i).getCapacity()); + System.out.print('\n'); + } } } diff --git a/src/CourseRegistrationStarter/Student.java b/src/CourseRegistrationStarter/Student.java index 02c11d8..f5247ce 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -5,11 +5,13 @@ public class Student { private String name; private String studentId; private ArrayList registeredCourses; + private int limitOfCourses; public Student(String name, String studentId) { this.name = name; this.studentId = studentId; this.registeredCourses = new ArrayList<>(); + this.limitOfCourses = 18; } public String getName() { @@ -20,20 +22,37 @@ public class Student { return studentId; } + public void setLimitOfCourses(int limit){ + this.limitOfCourses = limit; + } + 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)){ + return false; + } + + if ((c.getCapacity() > 0) && (this.limitOfCourses >= registeredCourses.size() + 1)){ + 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 (int i = 0; i < registeredCourses.size(); i++) { + System.out.printf("%25s %15s", registeredCourses.get(i).getCourseName() , registeredCourses.get(i).getCourseCode()); + System.out.print('\n'); + } } } \ No newline at end of file -- 2.54.0