diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 8f06373..a2305e8 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -1,7 +1,120 @@ package CourseRegistrationStarter; +import java.util.Scanner; + public class Main { public static void main(String[] args) { + + + 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; + } + + + } + + + + + + + + + + + + + + } // TODO: // 1. Create at least 3 courses // 2. Create 1 student diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 934f222..6a684a7 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -5,20 +5,35 @@ public class RegistrationSystem { private ArrayList courses; public RegistrationSystem() { - courses = new ArrayList<>(); + courses = new ArrayList(); } 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 + int n = courses.size(); + for(int i = 0 ; i < n ; i++){ + if(courses.get(i).getCourseCode().equals(code)){ + return courses.get(i); + } + + } return null; } public void showAllCourses() { - // TODO: Print all courses in the system + int n = courses.size(); + System.out.println("All courses :"); + 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 02c11d8..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,19 +23,58 @@ 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(c.getCapacity() > 0&& !existe(c) && !checkdeleted(c)){ + registeredCourses.add(c); + c.reduceCapacity(); + return true; + } 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 + int n = registeredCourses.size(); + for(int i = 0 ; i < n ; i++){ + if(registeredCourses.get(i).getCourseName() == c.getCourseName()){ + registeredCourses.remove(i); + c.increaseCapacity(); + deletedCourse.add(c); + return true; + } + } return false; } public void showRegisteredCourses() { - // TODO: Print all registered courses + int n = registeredCourses.size(); + System.out.println(this.name + "\'s Courses :"); + for(int i = 0 ; i < n ; i++){ + System.out.printf("course name: %10s " , registeredCourses.get(i).getCourseName()); + System.out.printf("cours code: %10s " , registeredCourses.get(i).getCourseCode()); + 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; + } + +} +