From c04246c8858fc85ea3079177a1c472efb1d67d87 Mon Sep 17 00:00:00 2001 From: Saeed Jamali Date: Sun, 19 Apr 2026 23:46:05 +0330 Subject: [PATCH 1/2] implement tasks based on README --- src/CourseRegistrationStarter/Main.java | 14 ++++++++++++++ .../RegistrationSystem.java | 9 +++++++++ src/CourseRegistrationStarter/Student.java | 13 +++++++++++++ 3 files changed, 36 insertions(+) diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 8f06373..de6db0c 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -3,6 +3,20 @@ package CourseRegistrationStarter; public class Main { public static void main(String[] args) { // TODO: + Course BP = new Course("Basic Programming", "BP1404", 3); + Course AP = new Course("Advanced Programming", "AP1405", 2); + Course DS = new Course("Data Structure", "DS1406", 2); + Student saeedJam = new Student("Saeed Jamali", "404222000"); + RegistrationSystem register = new RegistrationSystem(); + + register.addCourseToSystem(BP); + register.addCourseToSystem(AP); + register.addCourseToSystem(DS); + + saeedJam.addCourse(AP); + saeedJam.addCourse(DS); + saeedJam.dropCourse(DS); + saeedJam.showRegisteredCourses(); // 1. Create at least 3 courses // 2. Create 1 student // 3. Create a registration system diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 934f222..1c520f0 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -9,16 +9,25 @@ public class RegistrationSystem { } public void addCourseToSystem(Course c) { + this.courses.add(c); // TODO: Add the course to the system } public Course findCourseByCode(String code) { + for (Course c: this.courses) { + if (c.getCourseCode().equals(code)) { + return c; + } + } // TODO: Search for a course by course code // Return the matching course if found, otherwise return null return null; } public void showAllCourses() { + for(Course c: this.courses) { + System.out.println(c.getCourseName() + ", code: " + c.getCourseCode()); + } // TODO: Print all courses in the system } } diff --git a/src/CourseRegistrationStarter/Student.java b/src/CourseRegistrationStarter/Student.java index 02c11d8..0ddc505 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -21,12 +21,22 @@ public class Student { } public boolean addCourse(Course c) { + if (c.getCapacity() > 0 && !this.registeredCourses.contains(c)) { + this.registeredCourses.add(c); + c.reduceCapacity(); + return true; + } // TODO: Register the student in the course if capacity allows // Return true if registration was successful, otherwise false return false; } public boolean dropCourse(Course c) { + if (this.registeredCourses.contains(c)) { + this.registeredCourses.remove(c); + c.increaseCapacity(); + return true; + } // 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 @@ -34,6 +44,9 @@ public class Student { } public void showRegisteredCourses() { + for(Course c: this.registeredCourses) { + System.out.println(c.getCourseName() + ", code: " + c.getCourseCode()); + } // TODO: Print all registered courses } } \ No newline at end of file From 4337dad762d9b98f4b8f68c3c7ff6ec2d07d78bf Mon Sep 17 00:00:00 2001 From: Saeed Jamali Date: Mon, 27 Apr 2026 14:11:57 +0330 Subject: [PATCH 2/2] add menu for the program --- src/CourseRegistrationStarter/Main.java | 84 ++++++++++++++++++- .../RegistrationSystem.java | 2 +- 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index de6db0c..e6a7d3f 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -1,7 +1,11 @@ package CourseRegistrationStarter; +import java.util.ArrayList; +import java.util.Scanner; + public class Main { public static void main(String[] args) { + ArrayList students= new ArrayList<>(); // TODO: Course BP = new Course("Basic Programming", "BP1404", 3); Course AP = new Course("Advanced Programming", "AP1405", 2); @@ -12,7 +16,6 @@ public class Main { register.addCourseToSystem(BP); register.addCourseToSystem(AP); register.addCourseToSystem(DS); - saeedJam.addCourse(AP); saeedJam.addCourse(DS); saeedJam.dropCourse(DS); @@ -24,5 +27,84 @@ public class Main { // 5. Register the student in some courses // 6. Drop one course // 7. Print the final registered course list + int command = 100; + Scanner scanner = new Scanner(System.in); + while (command != 0) { + if (command == 1) { + String courseName = ""; + String courseCode = ""; + int courseCapacity = 0; + System.out.println("Please enter course name: "); + courseName = scanner.nextLine(); + System.out.println("Enter course code: "); + courseCode = scanner.nextLine(); + System.out.println("Enter course capacity: "); + courseCapacity = scanner.nextInt(); + scanner.nextLine(); + register.addCourseToSystem(new Course(courseName, courseCode, courseCapacity)); + System.out.println("Course added successfully."); + + } else if (command == 2) { + String studentName = ""; + String studentId = ""; + System.out.println("Please enter student name: "); + studentName = scanner.nextLine(); + System.out.println("Enter student ID: "); + studentId = scanner.nextLine(); + students.add(new Student(studentName, studentId)); + System.out.println("Student added successfully."); + + } else if (command == 3) { + String studentId = ""; + String courseCode = ""; + Student studentToAdd; + System.out.println("Please enter student ID: "); + studentId = scanner.nextLine(); + System.out.println("Enter course code: "); + courseCode = scanner.nextLine(); + studentToAdd = null; + studentToAdd = findStudentById(students, studentId); + Course courseToAdd = register.findCourseByCode(courseCode); + studentToAdd.addCourse(courseToAdd); + System.out.println("Course "+ courseCode + " added successfuly for Student " + studentId); + + } else if (command == 4) { + String studentId = ""; + Student studentToShow = null; + System.out.println("Please enter student ID: "); + studentId = scanner.nextLine(); + studentToShow = findStudentById(students, studentId); + studentToShow.showRegisteredCourses(); + } else if (command == 5) { + String courseCode = ""; + System.out.println("Enter course code: "); + courseCode = scanner.nextLine(); + Course courseToShow = register.findCourseByCode(courseCode); + System.out.println(courseToShow.getCourseName() + ", Code: " + + courseToShow.getCourseCode() + ", Capacity: " + courseToShow.getCapacity()); + } else if (command == 6) { + register.showAllCourses(); + } + System.out.println("----------------MENU----------------"); + System.out.println("1-Add course to system"); + System.out.println("2-Add student"); + System.out.println("3-Add course for student"); + System.out.println("4-Show student courses"); + System.out.println("5-Find course by code"); + System.out.println("6-Show all courses"); + System.out.println("0-Exit\n"); + command = scanner.nextInt(); + scanner.nextLine(); + } } + + public static Student findStudentById(ArrayList studentList, String id) { + for (Student s: studentList) { + if (s.getStudentId().equals(id)) { + return s; + } + } + return null; + } + } diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 1c520f0..82be031 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -26,7 +26,7 @@ public class RegistrationSystem { public void showAllCourses() { for(Course c: this.courses) { - System.out.println(c.getCourseName() + ", code: " + c.getCourseCode()); + System.out.println(c.getCourseName() + ", code: " + c.getCourseCode() + ", capacity: " + c.getCapacity()); } // TODO: Print all courses in the system }