diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 8f06373..2a295bb 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -4,11 +4,34 @@ public class Main { public static void main(String[] args) { // TODO: // 1. Create at least 3 courses + Course course1 = new Course("Advanced Programming", "AP1404", 2); + Course course2 = new Course("Data Structures", "Ds2201", 1); + Course course3 = new Course("Database Systems", "DB3301", 3); + // 2. Create 1 student + Student student1 = new Student("Ali", "402222001"); + // 3. Create a registration system + RegistrationSystem SBU = new RegistrationSystem(); + // 4. Add courses to the system + SBU.addCourseToSystem(course1); + SBU.addCourseToSystem(course2); + SBU.addCourseToSystem(course3); + // 5. Register the student in some courses + student1.addCourse(course1); + student1.addCourse(course2); + // 6. Drop one course + student1.dropCourse(course2); + // 7. Print the final registered course list + System.out.printf("\n%s\'s registered courses:\n", student1.getName()); + student1.showRegisteredCourses(); + + System.out.println("\nRemaining informations of courses are as follows: "); + SBU.showAllCourses(); + } } diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 934f222..402afa6 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -10,15 +10,34 @@ 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 thisCourse : courses){ + if(thisCourse.getCourseCode() == code){ + return thisCourse; + } + } + return null; + } + + public Course findCourseByName(String name){ // search by course name + for(Course thisCourse : courses){ + if(thisCourse.getCourseName() == name){ + return thisCourse; + } + } return null; } public void showAllCourses() { // TODO: Print all courses in the system + System.out.println("Course Name | Course Code | Capacity"); + for(Course thisCourse : courses){ + System.out.printf("%s | %s | %d\n", thisCourse.getCourseName(), thisCourse.getCourseCode(), thisCourse.getCapacity()); + } } } diff --git a/src/CourseRegistrationStarter/Student.java b/src/CourseRegistrationStarter/Student.java index 02c11d8..73e0cf0 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -5,6 +5,7 @@ public class Student { private String name; private String studentId; private ArrayList registeredCourses; + private int allowedCourses = 2; public Student(String name, String studentId) { this.name = name; @@ -23,6 +24,21 @@ 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.contains(c)){ // Preventing duplicate course registration + System.out.printf("%s is already registered for %s\n", c.getCourseName(), name); + return false; + } + + if(registeredCourses.size() >= allowedCourses){ // Limiting the number of courses per student + System.out.printf("%s has reached his limit for registration.\n", name); + } + + if(c.reduceCapacity()){ // if enough capacity, reduction happens in this line + registeredCourses.add(c); + System.out.printf("%s registered successfully.\n", c.getCourseName()); + return true; + } return false; } @@ -30,10 +46,20 @@ public class Student { // 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.contains(c)){ + registeredCourses.remove(c); + c.increaseCapacity(); + System.out.printf("%s removed successfully.\n", c.getCourseName()); + return true; + } return false; } public void showRegisteredCourses() { // TODO: Print all registered courses + for(Course thisCourse : registeredCourses){ + System.out.printf("%s <%s>\n", thisCourse.getCourseName(), thisCourse.getCourseCode()); + } } } \ No newline at end of file