diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 8f06373..06d7547 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -2,13 +2,25 @@ 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 AdvancedProgramming = new Course("AdvancedProgramming", "AP1404", 2); + Course DataStructures = new Course("DataStructures", "DS2201", 1); + Course DatabaseSystems = new Course("DatabaseSystems", "DB3301", 3); + + Student fateme = new Student("fateme", "404222000", 10); + + RegistrationSystem A = new RegistrationSystem(); + + A.addCourseToSystem(AdvancedProgramming); + A.addCourseToSystem(DataStructures); + A.addCourseToSystem(DatabaseSystems); + + fateme.addCourse(AdvancedProgramming); + fateme.addCourse(DataStructures); + fateme.addCourse(DatabaseSystems); + + fateme.dropCourse(DataStructures); + + fateme.showRegisteredCourses(); } } diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 934f222..09acb07 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -9,16 +9,46 @@ public class RegistrationSystem { } public void addCourseToSystem(Course c) { - // TODO: Add the course to the system + + for (int i=0 ; i < courses.size() ; i++) { + + if(courses.get(i).getCourseCode().equals(c.getCourseCode()) || + courses.get(i).getCourseName().equals(c.getCourseName())) { + + System.out.println("Course name or code already exists."); + return; + } + } + + 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 registeredCourses; + private int courseLimit; - public Student(String name, String studentId) { + public Student(String name, String studentId, int courseLimit) { this.name = name; this.studentId = studentId; this.registeredCourses = new ArrayList<>(); + this.courseLimit = courseLimit; } public String getName() { @@ -21,19 +23,35 @@ 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(c.getCapacity() > 0 && registeredCourses.size() < courseLimit) { + registeredCourses.add(c); + c.reduceCapacity(); + return true; + } return false; } + public void setCourseLimit(int amount) { + if(amount>0) + courseLimit = amount; + } + 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.print("Course"); + System.out.print(i+1); + System.out.print(" : " + registeredCourses.get(i).getCourseName() + " " + registeredCourses.get(i).getCourseCode()); + System.out.println(", capacity : " +registeredCourses.get(i).getCapacity()); + } } } \ No newline at end of file