diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..d0d677e --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Course.java \ No newline at end of file diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 8f06373..02b39c0 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -2,13 +2,34 @@ 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 + // 1. Create courses + Course c1 = new Course("Advanced Programming", "AP1404", 2); + Course c2 = new Course("Data Structures", "DS2201", 1); + Course c3 = new Course("Database Systems", "DB3301", 3); + + // 2. Create student + Student student = new Student("Ali", "402222001"); + + // 3. Create registration system + RegistrationSystem system = new RegistrationSystem(); + + // 4. Add courses to system + system.addCourseToSystem(c1); + system.addCourseToSystem(c2); + system.addCourseToSystem(c3); + + // 5. Register student in courses + student.addCourse(system.findCourseByCode("AP1404")); + student.addCourse(system.findCourseByCode("DS2201")); + // 6. Drop one course - // 7. Print the final registered course list + student.dropCourse(system.findCourseByCode("DS2201")); + + // 7. Print final results + System.out.println("===== FINAL STUDENT COURSES ====="); + student.showRegisteredCourses(); + + System.out.println("\n===== SYSTEM COURSES ====="); + system.showAllCourses(); } } diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 934f222..61048f8 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -9,16 +9,26 @@ public class RegistrationSystem { } public void addCourseToSystem(Course c) { - // TODO: Add the course to the system + courses.add(c); } - +// we loop through all courses and check it with its 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 c : courses) { + if (c.getCourseCode().equals(code)) { + return c; + } + } return null; } - +// List all courses public void showAllCourses() { - // TODO: Print all courses in the system + System.out.println("All Courses in 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..f146232 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -2,9 +2,9 @@ package CourseRegistrationStarter; import java.util.ArrayList; public class Student { - private String name; - private String studentId; - private ArrayList registeredCourses; + private final String name; + private final String studentId; + private final ArrayList registeredCourses; public Student(String name, String studentId) { this.name = name; @@ -21,19 +21,32 @@ 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 + // Prevent duplicate registration + if (registeredCourses.contains(c)) { + return false; + } + + // Try to reduce capacity first + if (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 + public boolean dropCourse(Course c) {if (registeredCourses.remove(c)) { + // after removing course, its capacity will be increased + c.increaseCapacity(); + return true; + } return false; } - +// List all registered courses of student public void showRegisteredCourses() { - // TODO: Print all registered courses + System.out.println("Registered courses for " + name + ":"); + for (Course c : registeredCourses) { + System.out.println(c.getCourseName() + " (" + c.getCourseCode() + ")"); + } } } \ No newline at end of file