Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e27906245 |
@@ -10,5 +10,25 @@ public class Main {
|
||||
// 5. Register the student in some courses
|
||||
// 6. Drop one course
|
||||
// 7. Print the final registered course list
|
||||
|
||||
Course c1 = new Course("Math", "M101", 3);
|
||||
Course c2 = new Course("Physics", "P202", 2);
|
||||
Course c3 = new Course("Programming", "CS303", 1);
|
||||
Student s1 = new Student("Ali", "S123");
|
||||
|
||||
RegistrationSystem system = new RegistrationSystem();
|
||||
|
||||
system.addCourseToSystem(c1);
|
||||
system.addCourseToSystem(c2);
|
||||
system.addCourseToSystem(c3);
|
||||
|
||||
s1.addCourse(c1);
|
||||
s1.addCourse(c2);
|
||||
|
||||
s1.dropCourse(c1);
|
||||
|
||||
s1.showRegisteredCourses();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -10,15 +10,27 @@ 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
|
||||
//return null;
|
||||
for (Course c : courses) {
|
||||
if (c.getCourseCode().equals(code)) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void showAllCourses() {
|
||||
// TODO: Print all courses in the system
|
||||
for (Course c : courses) {
|
||||
System.out.println(c.getCourseName() + " (" + c.getCourseCode() + ") Capacity: " + c.getCapacity());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,17 +23,45 @@ 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 (registeredCourses.contains(c)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (c.reduceCapacity()) {
|
||||
registeredCourses.add(c);
|
||||
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
|
||||
//return false;
|
||||
if (registeredCourses.contains(c)) {
|
||||
registeredCourses.remove(c);
|
||||
c.increaseCapacity();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
public void showRegisteredCourses() {
|
||||
// TODO: Print all registered courses
|
||||
if (registeredCourses.isEmpty()) {
|
||||
System.out.println("No courses registered.");
|
||||
return;
|
||||
}
|
||||
|
||||
for (Course c : registeredCourses) {
|
||||
System.out.println(c.getCourseName() + " (" + c.getCourseCode() + ")");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user