complet classes and write tese class
This commit is contained in:
@@ -2,6 +2,33 @@ package CourseRegistrationStarter;
|
||||
|
||||
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 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
|
||||
|
||||
@@ -5,20 +5,34 @@ public class RegistrationSystem {
|
||||
private ArrayList<Course> courses;
|
||||
|
||||
public RegistrationSystem() {
|
||||
courses = new ArrayList<>();
|
||||
courses = new ArrayList<Course>();
|
||||
}
|
||||
|
||||
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() == 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("cours capacity: %10s " , courses.get(i).getCapacity());
|
||||
System.out.println("");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,19 +21,33 @@ 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){
|
||||
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();
|
||||
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("");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user