From 9fad2d020b33abe92f14dad716c9ae303b30594b Mon Sep 17 00:00:00 2001 From: neda khosrojerdi Date: Mon, 27 Apr 2026 02:46:12 +0330 Subject: [PATCH] uummm --- src/CourseRegistrationStarter/Main.java | 92 +++++++++++++++++-- .../RegistrationSystem.java | 28 +++++- src/CourseRegistrationStarter/Student.java | 55 +++++++---- 3 files changed, 147 insertions(+), 28 deletions(-) diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 8f06373..6f0a3e5 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -1,14 +1,90 @@ 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 + + Scanner input = new Scanner(System.in); + + Course c1 = new Course("Advanced Programming", "AP1404", 2); + Course c2 = new Course("Data Structures", "DS2201", 1); + Course c3 = new Course("Database Systems", "DB3301", 3); + + Student student = new Student("Ali", "402222001"); + + RegistrationSystem system = new RegistrationSystem(); + + system.addCourseToSystem(c1); + system.addCourseToSystem(c2); + system.addCourseToSystem(c3); + + int choice; + + do { + + System.out.println(); + 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. Exit"); + System.out.print("Choose: "); + + choice = input.nextInt(); + input.nextLine(); + + switch (choice) { + + case 1: + system.showAllCourses(); + break; + + case 2: + + System.out.print("Enter course code: "); + String code = input.nextLine(); + + Course course = system.findCourseByCode(code); + + if (course != null) { + student.addCourse(course); + } else { + System.out.println("Course not found."); + } + + break; + + case 3: + + System.out.print("Enter course code: "); + String dropCode = input.nextLine(); + + Course dropCourse = system.findCourseByCode(dropCode); + + if (dropCourse != null) { + student.dropCourse(dropCourse); + } else { + System.out.println("Course not found."); + } + + break; + + case 4: + student.showRegisteredCourses(); + break; + + case 5: + System.out.println("Goodbye"); + break; + + default: + System.out.println("Invalid choice"); + + } + + } while (choice != 5); + } } diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 934f222..7d9bef6 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -1,7 +1,9 @@ package CourseRegistrationStarter; + import java.util.ArrayList; public class RegistrationSystem { + private ArrayList courses; public RegistrationSystem() { @@ -9,16 +11,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 c : courses) { + + if (c.getCourseCode().equalsIgnoreCase(code)) { + return c; + } + + } + return null; } public void showAllCourses() { - // TODO: Print all courses in the 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..c05b3e7 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -1,7 +1,9 @@ package CourseRegistrationStarter; + import java.util.ArrayList; public class Student { + private String name; private String studentId; private ArrayList registeredCourses; @@ -9,31 +11,52 @@ public class Student { public Student(String name, String studentId) { this.name = name; this.studentId = studentId; - this.registeredCourses = new ArrayList<>(); - } - - public String getName() { - return name; - } - - public String getStudentId() { - return studentId; + registeredCourses = new ArrayList<>(); } 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)) { + System.out.println("Already registered in this course."); + return false; + } + + if (c.reduceCapacity()) { + registeredCourses.add(c); + System.out.println("Course registered successfully."); + return true; + } + + System.out.println("Course capacity 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 + + if (registeredCourses.remove(c)) { + c.increaseCapacity(); + System.out.println("Course dropped successfully."); + return true; + } + + System.out.println("You are not registered in this course."); return false; } public void showRegisteredCourses() { - // TODO: Print all registered courses + + if (registeredCourses.isEmpty()) { + System.out.println("No registered courses."); + return; + } + + System.out.println("Your Courses:"); + + for (Course c : registeredCourses) { + System.out.println( + c.getCourseName() + " (" + c.getCourseCode() + ")" + ); + } } -} \ No newline at end of file +} +