Complete Student class (addCourse, dropCourse and showRegisteredCourses methods)
This commit is contained in:
@@ -23,6 +23,17 @@ 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)){
|
||||
System.out.printf("%s is already registered for %s\n", c.getCourseName(), name);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(c.reduceCapacity()){ // if enough capacity, reduction happens in this line
|
||||
registeredCourses.add(c);
|
||||
System.out.printf("%s registered successfully.\n", c.getCourseName());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -30,10 +41,20 @@ public class Student {
|
||||
// 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.printf("%s removed successfully.\n", c.getCourseName());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void showRegisteredCourses() {
|
||||
// TODO: Print all registered courses
|
||||
for(Course thisCourse : registeredCourses){
|
||||
System.out.printf("%s <%s>\n", thisCourse.getCourseName(), thisCourse.getCourseCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user