From 9fe6cd709dccc9a7d1451804e88f531b0a1fc53a Mon Sep 17 00:00:00 2001 From: Parmis Jamami Date: Mon, 20 Apr 2026 19:28:25 +0330 Subject: [PATCH] complete code --- src/CourseRegistrationStarter/Main.java | 31 +++++++--- .../RegistrationSystem.java | 19 +++++-- src/CourseRegistrationStarter/Student.java | 56 ++++++++++++++++--- 3 files changed, 85 insertions(+), 21 deletions(-) diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 8f06373..6b69fe8 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -2,13 +2,28 @@ 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 course1 = new Course("Advanced Programming","AP1404", 2); + Course course2 = new Course("Data Structures","DS2201",1); + Course course3 = new Course("Database Systems","DB3301",3); + + Student student1 = new Student("Ali" , "402222001"); + + RegistrationSystem registrationSystem = new RegistrationSystem(); + + registrationSystem.addCourseToSystem(course1); + registrationSystem.addCourseToSystem(course2); + registrationSystem.addCourseToSystem(course3); + + student1.addCourse(course1); + student1.addCourse(course3); + student1.dropCourse(course1); + + student1.showRegisteredCourses(); + + System.out.println("Course " + course1.getCourseName() + " capacity: " + course1.getCapacity()); + System.out.println("Course " + course2.getCourseName() + " capacity: " + course2.getCapacity()); + System.out.println("Course " + course3.getCourseName() + " capacity: " + course3.getCapacity()); + + System.out.println("the number of courses: " + student1.countCourse()); } } diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 934f222..56fdeac 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -9,16 +9,27 @@ 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++){ + Course course = courses.get(i); + if(course.getCourseCode().equals(code)){ + return course; + } + } return null; } public void showAllCourses() { - // TODO: Print all courses in the system + System.out.println("Courses in the system: "); + for(int i = 0 ; i < courses.size() ; i++ ){ + Course c = courses.get(i); + System.out.println(" Name: " + c.getCourseName() + + " , code: " + c.getCourseCode() + + ", capicity: " + c.getCapacity()); + + } } } diff --git a/src/CourseRegistrationStarter/Student.java b/src/CourseRegistrationStarter/Student.java index 02c11d8..4e89206 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -1,4 +1,6 @@ package CourseRegistrationStarter; +import com.sun.net.httpserver.Authenticator; + import java.util.ArrayList; public class Student { @@ -21,19 +23,55 @@ 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 - return false; + if(registeredCourses.contains(c)){ + System.out.println( " already registered for " + c.getCourseName()); + return false; + } + if(c.reduceCapacity()){ + registeredCourses.add(c); + System.out.println("Successfully registered for " + c.getCourseName()); + return true; + + } + else{ + System.out.println("Course is full"); + 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 - return false; + boolean removed = registeredCourses.remove(c); + if(removed){ + c.increaseCapacity(); + System.out.println("Course " + c.getCourseName() + " succsessfully removed"); + return true; + } + else{ + System.out.println("Corse " + c.getCourseName() + "is not found"); + return false; + } } public void showRegisteredCourses() { - // TODO: Print all registered courses + System.out.print("Courses registered by " + name + " : "); + if(registeredCourses.isEmpty()){ + System.out.println("No course found! "); + + } + else{ + for(int i = 0 ; i < registeredCourses.size() ; i++ ){ + Course course = registeredCourses.get(i); + System.out.println(course.getCourseName() + "(" + course.getCourseCode() + ")" + " , "); + + } + } } -} \ No newline at end of file + + public int countCourse(){ + int count=0; + for(int i = 0 ; i < registeredCourses.size() ; i++){ + count++; + } + return count; + } +}