From 53c1bca95461b485c37b23e12b1c67472c82d09b Mon Sep 17 00:00:00 2001 From: Amir Ali Amiri Date: Tue, 9 Jun 2026 17:42:47 +0330 Subject: [PATCH] implement student and registration logic --- src/CourseRegistrationStarter/Main.java | 34 ++++++++++++++----- .../RegistrationSystem.java | 23 +++++++++---- src/CourseRegistrationStarter/Student.java | 28 +++++++++++---- 3 files changed, 64 insertions(+), 21 deletions(-) diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 8f06373..77ffb75 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -2,13 +2,31 @@ 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 c1 = new Course("Advanced Programming" , "AP101" , 25); + Course c2 = new Course("Quantum Mechanics" , "QM202" , 20); + Course c3 = new Course("Electromagnetism" , "EL303" , 28); + //تعریف یک دانشجو + Student student = new Student("AmirAli Amiri" , "402216005"); + // ایجاد سیستم ثبت نام + RegistrationSystem system = new RegistrationSystem(); + // اضافه کردن دروس به سیستم ثبت نام + system.addCourseToSystem(c1); + system.addCourseToSystem(c2); + system.addCourseToSystem(c3); + // نشان دادن همه دروس ثبت شده در سیستم + system.showAllCourses(); + // اضافه کردن دو درس به لیست درس دانشجو + System.out.println("شروع انتخاب واحد"); + student.addCourse(c1); + student.addCourse(c3); + // نشان دادن همه دروس ثبت شده در لیست دروس دانشجو + student.showRegisteredCourses(); + // حذف کردن یکی از دروس ثبت شده + System.out.println("فرایند حذف واحد"); + student.dropCourse(c1); + // نشان دادن همه دروس ثبت شده در لیست دروس دانشجو و سیستم + student.showRegisteredCourses(); + system.showAllCourses(); } } diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 934f222..79972b8 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -8,17 +8,26 @@ public class RegistrationSystem { courses = new ArrayList<>(); } - public void addCourseToSystem(Course c) { - // TODO: Add the course to the system + public void addCourseToSystem(Course c) { // درس را به سیستم مرکزی اضافه می کند + if (!courses.contains(c)){ + 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 + public Course findCourseByCode(String code) { // درس را بر اساس کد درس پیدا می کند + for ( Course c : courses){ + if ( c.getCourseCode().equalsIgnoreCase(code)){ + return c; + } + } return null; } - public void showAllCourses() { - // TODO: Print all courses in the system + public void showAllCourses() { // تمامی دروس ثبت شده در سیستم را نشان می دهد + System.out.println("لیست تمام دروس موجود در سیستم \n"); + for(Course c : courses){ + System.out.println("نام درس : " + c.getCourseName()+ "|کد درس :" + c.getCourseCode() + "| ظرفیت باقی مانده درس : " + c.getCapacity()); + } } } + diff --git a/src/CourseRegistrationStarter/Student.java b/src/CourseRegistrationStarter/Student.java index 02c11d8..3cb222c 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -21,19 +21,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(!registeredCourses.contains(c) && c.reduceCapacity()){ // ابتدا چک می کنیم که فرد مورد نظر درس را قبلا اخذ نکرده باشد و درس ظرفیت داشته باشد + registeredCourses.add(c); // اگر شرط بالا رعایت شده باشد درس را اضافه میکند + System.out.println(" درس " + c.getCourseName() + "با موفقیت اخذ شد ."); + return true; + } + System.out.println("ظرفیت درس تکمیل است یا درس قبلا اخذ شده است ."); 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.contains(c)){ // اگر درس در لیست دروس فرد باشد آن را حذف می کند و ظرفیت درس را افزایش می دهد + registeredCourses.remove(c); + c.increaseCapacity(); + System.out.println("درس با موفقیت حذف شد!"); + return true; + } + System.out.println("درس مورد نظر در لیست دروس شما یافت نشد!"); return false; } public void showRegisteredCourses() { - // TODO: Print all registered courses + System.out.println("لیست دروس ثبت نام شده برای " + name + "به شماره دانشجویی" + studentId ); // لیست تمامی دروسی که فرد اخذ کرده است را نشان می دهد + if(registeredCourses.isEmpty()){ + System.out.println("هیچ درسی ثبت نام نکرده اید!" ); + } + else{ + for ( Course c : registeredCourses){ + System.out.println("- درس " + c.getCourseName() + " به شماره درس " + c.getCourseCode()); + } + } } } \ No newline at end of file