From bd02a76215fcee770b634cabe89a9b4fb1aef947 Mon Sep 17 00:00:00 2001 From: Mahdi HP Date: Mon, 27 Apr 2026 02:55:03 -0700 Subject: [PATCH] write main class --- src/CourseRegistrationStarter/Main.java | 122 +++++++++++++++--- .../RegistrationSystem.java | 3 +- src/CourseRegistrationStarter/Student.java | 31 ++++- 3 files changed, 135 insertions(+), 21 deletions(-) diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 67c89d0..a2305e8 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -1,34 +1,120 @@ package CourseRegistrationStarter; +import java.util.Scanner; + public class Main { public static void main(String[] args) { - Course riazi2 = new Course("riazi2" , "200" , 5); - Course ap = new Course("ap" , "201" , 2); - Course bp = new Course("bp" , "202" , 5); - Course riazi1 = new Course("riazi1" , "203" , 7); - Course os = new Course("os" , "204" , 10); - Student daneshjo = new Student("ali" , "11111"); + RegistrationSystem register = new RegistrationSystem(); + Course riazi1 = new Course("riazi1" , "200" , 5); + Course riazi2 = new Course("riazi2" , "201" , 4); + Course ap = new Course("ap" , "202" , 10); + Course bp = new Course("bp" , "203" , 0); + + register.addCourseToSystem(riazi1); + register.addCourseToSystem(riazi2); + register.addCourseToSystem(ap); + register.addCourseToSystem(bp); + + Student daneshjo = new Student("ali" , "404222000"); + boolean yes = true; + + while( yes == true ){ + + System.out.println("----------Menu----------"); + System.out.println("1. show all course"); + System.out.println("2. add course"); + System.out.println("3. delete course"); + System.out.println("4.my course"); + System.out.println("5.exit"); + System.out.print("choose item :"); + + Scanner input = new Scanner(System.in); + int n = input.nextInt(); + n = (n % 5); + + switch (n) + { + case 1: + { + register.showAllCourses(); + break; + } + case 2: + { + System.out.println("please enter the code of course"); + String courseCode = input.next(); + if(register.findCourseByCode(courseCode) != null){ + if(daneshjo.addCourse(register.findCourseByCode(courseCode))){ + System.out.println("done"); + } + else{ + if(register.findCourseByCode(courseCode).getCapacity() <=0){ + System.out.println("cours is full"); + } + else if(daneshjo.existe(register.findCourseByCode(courseCode))){ + System.out.println("you have get this course befor"); + } + else { + System.out.println("you have deleted this course befor"); + } + } + } + else { + System.out.println("invalid course"); + } + break; + } + + case 3: + { + System.out.println("please enter the code of course"); + String courseCode = input.next(); + if(register.findCourseByCode(courseCode) != null){ + if(daneshjo.dropCourse(register.findCourseByCode(courseCode))){ + System.out.println("done"); + } + else { + System.out.println("invaild"); + } + + } + else{ + System.out.println("Invalid course"); + } + break; + + } + + case 4: + { + daneshjo.showRegisteredCourses(); + break; + } + + case 0: + { + yes = false; + break; + } - RegistrationSystem regis = new RegistrationSystem(); + } - regis.addCourseToSystem(riazi1); - regis.addCourseToSystem(riazi2); - regis.addCourseToSystem(ap); - regis.addCourseToSystem(bp); - regis.addCourseToSystem(os); - daneshjo.addCourse(os); - daneshjo.addCourse(bp); - daneshjo.addCourse(riazi1); - daneshjo.dropCourse(os); - daneshjo.showRegisteredCourses(); - regis.showAllCourses(); + + + + + + + + + } // TODO: // 1. Create at least 3 courses // 2. Create 1 student diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 0b4a3fd..6a684a7 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -15,7 +15,7 @@ public class RegistrationSystem { public Course findCourseByCode(String code) { int n = courses.size(); for(int i = 0 ; i < n ; i++){ - if(courses.get(i).getCourseCode() == code){ + if(courses.get(i).getCourseCode().equals(code)){ return courses.get(i); } @@ -29,6 +29,7 @@ public class RegistrationSystem { for(int i = 0 ; i < n ; i++){ System.out.printf("course name: %10s " , courses.get(i).getCourseName()); + System.out.printf("course code: %10s " , courses.get(i).getCourseCode()); System.out.printf("cours capacity: %10s " , courses.get(i).getCapacity()); System.out.println(""); diff --git a/src/CourseRegistrationStarter/Student.java b/src/CourseRegistrationStarter/Student.java index 7257077..6d4a97a 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -5,11 +5,13 @@ public class Student { private String name; private String studentId; private ArrayList registeredCourses; + private ArrayList deletedCourse; public Student(String name, String studentId) { this.name = name; this.studentId = studentId; this.registeredCourses = new ArrayList<>(); + this.deletedCourse = new ArrayList<>(); } public String getName() { @@ -21,7 +23,7 @@ public class Student { } public boolean addCourse(Course c) { - if(c.getCapacity() > 0){ + if(c.getCapacity() > 0&& !existe(c) && !checkdeleted(c)){ registeredCourses.add(c); c.reduceCapacity(); return true; @@ -35,6 +37,7 @@ public class Student { if(registeredCourses.get(i).getCourseName() == c.getCourseName()){ registeredCourses.remove(i); c.increaseCapacity(); + deletedCourse.add(c); return true; } } @@ -50,4 +53,28 @@ public class Student { System.out.println(""); } } -} \ No newline at end of file + + public boolean checkdeleted(Course c){ + int n = deletedCourse.size(); + for(int i = 0 ; i < n ; i++){ + if (deletedCourse.get(i).getCourseCode().equals(c.getCourseCode())){ + return true; + } + } + + return false; + } + + public boolean existe(Course c){ + int n = registeredCourses.size(); + for(int i = 0 ; i < n ; i++){ + if(registeredCourses.get(i).getCourseCode().equals(c.getCourseCode())){ + return true; + } + + } + return false; + } + +} +