From 3eea4f7372146d864b41286e8c4baa6838f84ed7 Mon Sep 17 00:00:00 2001 From: Soroush Date: Tue, 21 Apr 2026 13:19:38 +0330 Subject: [PATCH] Workshop-2 --- src/CourseRegistrationStarter/Main.java | 94 +++++++++++++++++-- .../RegistrationSystem.java | 36 ++++++- src/CourseRegistrationStarter/Student.java | 43 +++++++-- 3 files changed, 155 insertions(+), 18 deletions(-) diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 8f06373..4da74a7 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -1,14 +1,92 @@ package CourseRegistrationStarter; +import java.util.Scanner; 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 c1 = new Course("Advanced Programming", "AP1404", 2); + Course c2 = new Course("Data Structures", "DS2201", 1); + Course c3 = new Course("Database Systems", "DB3301", 3); + Course c4 = new Course("Basics of Programming", "BP4041", 0); + + RegistrationSystem system = new RegistrationSystem(); + system.addCourseToSystem(c1); + system.addCourseToSystem(c2); + system.addCourseToSystem(c3); + system.addCourseToSystem(c3); + system.addCourseToSystem(c4); + + + + Student student = new Student("Soroush", "404222000"); + + Scanner scanner = new Scanner(System.in); + + while (true) { + System.out.println("\n===== Course Registration Menu ====="); + System.out.println("1. Show all courses"); + System.out.println("2. Register course"); + System.out.println("3. Drop course"); + System.out.println("4. Show my courses"); + System.out.println("5. Search course by name"); + System.out.println("6. Exit"); + System.out.print("Choose an option: "); + + int choice = scanner.nextInt(); + scanner.nextLine(); + + if (choice == 1) { + system.showAllCourses(); + } + else if (choice == 2) { + System.out.print("Enter course code to register: "); + String code = scanner.nextLine(); + Course course = system.findCourseByCode(code); + if (course == null) { + System.out.print("Course not found."); + } else { + student.addCourse(course); + } + } + else if (choice == 3) { + System.out.print("Enter course code to drop: "); + String code = scanner.nextLine(); + Course course = system.findCourseByCode(code); + if (course == null) { + System.out.print("Course not found."); + } else { + student.dropCourse(course); + } + } + else if (choice == 4) { + student.showRegisteredCourses(); + } + else if (choice == 5) { + System.out.print("Enter course name to search: "); + String name = scanner.nextLine(); + Course course = system.findCourseByName(name); + if (course == null) { + System.out.println("Course not found."); + } else { + //boolean isRegistered = false; + //for (int i = 0; i < student.registeredCourses.size(); i++) { + // if (student.registeredCourses.get(i).getCourseCode().equals(course.getCourseCode())) { + // isRegistered = true; + // break; + // } + //} + System.out.println("Course found: " + course.getCourseName() + " (" + course.getCourseCode() + ") - Remaining capacity: " + course.getCapacity()); + //System.out.println("Status: " + (isRegistered ? "Registered" : "Not Registered")); + } + } + else if (choice == 6) { + System.out.println("Exiting..."); + break; + } + else { + System.out.println("Invalid choice."); + } + } + scanner.close(); } } diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 934f222..036b08e 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -8,17 +8,45 @@ public class RegistrationSystem { courses = new ArrayList<>(); } + // Adds the course to the system 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())) { + return; + } + } + courses.add(c); } + // Searches for a course by course code 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; } + // Searches for a course by course name + public Course findCourseByName(String name) { + for (int i = 0; i < courses.size(); i++) { + if (courses.get(i).getCourseName().equals(name)) { + return courses.get(i); + } + } + return null; + } + + // Prints all courses in the system public void showAllCourses() { - // TODO: Print all courses in the system + if (courses.size() == 0) { + System.out.println("No courses found."); + } else { + for (int i = 0; i < courses.size(); i++) { + Course c = courses.get(i); + System.out.println(c.getCourseName() + " (" + c.getCourseCode() + ") - Remaining capacity: " + c.getCapacity()); + } + } } } diff --git a/src/CourseRegistrationStarter/Student.java b/src/CourseRegistrationStarter/Student.java index 02c11d8..557dfe0 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -20,20 +20,51 @@ public class Student { return studentId; } + // Registers the student in the course if capacity allows 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.size() >= 2) { + System.out.println("You've reached the maximum limit of 2 courses."); + return false; + } + for (int i = 0; i < registeredCourses.size(); i++) { + if (registeredCourses.get(i).getCourseCode().equals(c.getCourseCode())) { + System.out.println("You're already registered in this course."); + return false; + } + } + if (c.getCapacity() > 0) { + registeredCourses.add(c); + c.reduceCapacity(); + System.out.println("Course registered successfully."); + return true; + } + System.out.println("Course capacity is full."); return false; } + // Removes the course from the student's list 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 + for (int i = 0; i < registeredCourses.size(); i++) { + if (registeredCourses.get(i).getCourseCode().equals(c.getCourseCode())) { + registeredCourses.remove(i); + c.increaseCapacity(); + System.out.println("Course dropped successfully."); + return true; + } + } + System.out.println("You're already not registered in this course."); return false; } + // Prints all registered courses public void showRegisteredCourses() { - // TODO: Print all registered courses + if (registeredCourses.size() == 0) { + System.out.println("No courses registered yet."); + } else { + for (int i = 0; i < registeredCourses.size(); i++) { + Course c = registeredCourses.get(i); + System.out.println(c.getCourseName() + " (" + c.getCourseCode() + ") - Remaining capacity: " + c.getCapacity()); + } + } } } \ No newline at end of file