complet classes and write tese class
This commit is contained in:
@@ -2,6 +2,33 @@ package CourseRegistrationStarter;
|
|||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
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:
|
// TODO:
|
||||||
// 1. Create at least 3 courses
|
// 1. Create at least 3 courses
|
||||||
// 2. Create 1 student
|
// 2. Create 1 student
|
||||||
|
|||||||
@@ -5,20 +5,34 @@ public class RegistrationSystem {
|
|||||||
private ArrayList<Course> courses;
|
private ArrayList<Course> courses;
|
||||||
|
|
||||||
public RegistrationSystem() {
|
public RegistrationSystem() {
|
||||||
courses = new ArrayList<>();
|
courses = new ArrayList<Course>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addCourseToSystem(Course c) {
|
public void addCourseToSystem(Course c) {
|
||||||
// TODO: Add the course to the system
|
courses.add(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Course findCourseByCode(String code) {
|
public Course findCourseByCode(String code) {
|
||||||
// TODO: Search for a course by course code
|
int n = courses.size();
|
||||||
// Return the matching course if found, otherwise return null
|
for(int i = 0 ; i < n ; i++){
|
||||||
|
if(courses.get(i).getCourseCode() == code){
|
||||||
|
return courses.get(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showAllCourses() {
|
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) {
|
public boolean addCourse(Course c) {
|
||||||
// TODO: Register the student in the course if capacity allows
|
if(c.getCapacity() > 0){
|
||||||
// Return true if registration was successful, otherwise false
|
registeredCourses.add(c);
|
||||||
|
c.reduceCapacity();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean dropCourse(Course c) {
|
public boolean dropCourse(Course c) {
|
||||||
// TODO: Remove the course from the student's list
|
int n = registeredCourses.size();
|
||||||
// Restore the course capacity if removal was successful
|
for(int i = 0 ; i < n ; i++){
|
||||||
// Return true if the course was dropped, otherwise false
|
if(registeredCourses.get(i).getCourseName() == c.getCourseName()){
|
||||||
|
registeredCourses.remove(i);
|
||||||
|
c.increaseCapacity();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showRegisteredCourses() {
|
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