WS-02-intro-to-oop #1
@@ -1,6 +1,7 @@
|
||||
package CourseRegistrationStarter;
|
||||
|
||||
public class Main {
|
||||
public class Main
|
||||
{
|
||||
public static void main(String[] args) {
|
||||
// TODO:
|
||||
// 1. Create at least 3 courses
|
||||
@@ -10,5 +11,21 @@ public class Main {
|
||||
// 5. Register the student in some courses
|
||||
// 6. Drop one course
|
||||
// 7. Print the final registered course list
|
||||
}
|
||||
|
||||
Course calculus = new Course("Calculus", "MATH101", 30);
|
||||
Course combination = new Course("Combination", "MATH102", 25);
|
||||
Course AP = new Course("AP Computer Science", "CS201", 20);
|
||||
|
||||
Student mobina = new Student("Mobina", "404222");
|
||||
|
||||
RegistrationSystem system = new RegistrationSystem();
|
||||
|
||||
system.addCourseToSystem(calculus);
|
||||
system.addCourseToSystem(combination);
|
||||
system.addCourseToSystem(AP);
|
||||
mobina.addCourse(AP);
|
||||
mobina.addCourse(calculus);
|
||||
mobina.dropCourse(calculus);
|
||||
mobina.showRegisteredCourses();
|
||||
}
|
||||
}
|
||||
@@ -10,15 +10,24 @@ public class RegistrationSystem {
|
||||
|
||||
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
|
||||
|
||||
for(int i=0; i<courses.size(); i++){
|
||||
if (((courses.get(i)).getCourseCode()).equals(code))
|
||||
return courses.get(i);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void showAllCourses() {
|
||||
// TODO: Print all courses in the system
|
||||
for (int i = 0; i < courses.size(); i++) {
|
||||
System.out.println(courses.get(i).getCourseName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,17 +23,34 @@ 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
|
||||
return false;
|
||||
|
||||
if (c.reduceCapacity())
|
||||
registeredCourses.add(c);
|
||||
return true;
|
||||
else
|
||||
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
|
||||
|
||||
for(int i=0; i<registeredCourses.size(); i++){
|
||||
if (c.getCourseName().equals(registeredCourses.get(i).getCourseName())){
|
||||
registeredCourses.remove(i);
|
||||
c.increaseCapacity();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void showRegisteredCourses() {
|
||||
// TODO: Print all registered courses
|
||||
for (int i=0; i<registeredCourses.size(); i++)
|
||||
{
|
||||
System.out.println(registeredCourses.get(i).getCourseName());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user