From 60684400ad4cc597c36eb50bba785c51d4deabc3 Mon Sep 17 00:00:00 2001 From: emad Date: Mon, 20 Apr 2026 22:11:26 +0430 Subject: [PATCH] implement course registration system --- src/CourseRegistrationStarter/Main.java | 20 ++++++++ .../RegistrationSystem.java | 46 +++++++++++++++++-- src/CourseRegistrationStarter/Student.java | 30 ++++++++++-- 3 files changed, 89 insertions(+), 7 deletions(-) diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 8f06373..39a111d 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -4,11 +4,31 @@ public class Main { public static void main(String[] args) { // TODO: // 1. Create at least 3 courses + Course course1 = new Course("AP", "AP1404", 30); + Course course2 = new Course("python", "python1405", 25); + Course course3 = new Course("math", "math1406", 60); + Course course4 = new Course("physics", "Ph1404", 15); // 2. Create 1 student + Student student1 = new Student("emad", "1422"); // 3. Create a registration system + RegistrationSystem Rs = new RegistrationSystem(); // 4. Add courses to the system + Rs.addCourseToSystem(course1); + Rs.addCourseToSystem(course2); + Rs.addCourseToSystem(course3); + Rs.addCourseToSystem(course4); // 5. Register the student in some courses + student1.addCourse(course1); + student1.addCourse(course3); + student1.addCourse(course4); + student1.addCourse(course1); // 6. Drop one course + student1.dropCourse(course3); // 7. Print the final registered course list + student1.showRegisteredCourses(); + System.out.println(); + Rs.showAllCourses(); + System.out.println(); + Rs.showCoursesCapacity(); } } diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 934f222..b4ef933 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -1,24 +1,64 @@ package CourseRegistrationStarter; import java.util.ArrayList; -public class RegistrationSystem { +public class RegistrationSystem +{ private ArrayList courses; public RegistrationSystem() { courses = new ArrayList<>(); } - public void addCourseToSystem(Course c) { + public void addCourseToSystem(Course c) + { // TODO: Add the course to the system + courses.add(c); } - public Course findCourseByCode(String code) { + public Course findCourseByCode(String code) + { // TODO: Search for a course by course code // Return the matching course if found, otherwise return null + for(Course course: courses) + { + if(course.getCourseCode().equals(code)) + { + return course; + } + } + return null; + } + public Course findCourseByName(String name) + { + // TODO: Search for a course by course code + // Return the matching course if found, otherwise return null + for(Course course: courses) + { + if(course.getCourseName().equals(name)) + { + return course; + } + } return null; } public void showAllCourses() { // TODO: Print all courses in the system + for(Course course: courses) + { + String name = course.getCourseName(); + String code = course.getCourseCode(); + int capacity = course.getCapacity(); + System.out.printf("%s:\n", name); + System.out.printf(" %s: %s\n %s: %d\n", "code", code, "capacity", capacity); + } + } + public void showCoursesCapacity() + { + System.out.println("Capacities:"); + for(Course c: courses) + { + System.out.printf(" %S: %d", c.getCourseName(), c.getCapacity()); + } } } diff --git a/src/CourseRegistrationStarter/Student.java b/src/CourseRegistrationStarter/Student.java index 02c11d8..fc4b68b 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -5,7 +5,7 @@ public class Student { private String name; private String studentId; private ArrayList registeredCourses; - + private int limitation = 5; //you can't get more than 5 courses public Student(String name, String studentId) { this.name = name; this.studentId = studentId; @@ -20,20 +20,42 @@ public class Student { return studentId; } - public boolean addCourse(Course c) { + public boolean addCourse(Course c) + { // TODO: Register the student in the course if capacity allows // Return true if registration was successful, otherwise false + if(c.getCapacity() > 0 && !registeredCourses.contains(c) + && registeredCourses.size() < limitation) + { + registeredCourses.add(c); + c.reduceCapacity(); + return true; + } return false; } - public boolean dropCourse(Course c) { + 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); + if (!registeredCourses.contains(c)) + { + c.increaseCapacity(); + return true; + } return false; } - public void showRegisteredCourses() { + public void showRegisteredCourses() + { // TODO: Print all registered courses + for (Course course : registeredCourses) + { + String courseName = course.getCourseName(); + String courseCode = course.getCourseCode(); + System.out.printf("%s: %s\n", courseName, courseCode); + } } } \ No newline at end of file -- 2.54.0