1 Commits
Author SHA1 Message Date
avasanatkar 7e27906245 hal tamrin 2026-04-21 00:26:14 +03:30
3 changed files with 62 additions and 2 deletions
+22 -2
View File
@@ -10,5 +10,25 @@ public class Main {
// 5. Register the student in some courses // 5. Register the student in some courses
// 6. Drop one course // 6. Drop one course
// 7. Print the final registered course list // 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) { public void addCourseToSystem(Course c) {
// TODO: Add the course to the system // 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 // TODO: Search for a course by course code
// Return the matching course if found, otherwise return null // Return the matching course if found, otherwise return null
//return null;
for (Course c : courses) {
if (c.getCourseCode().equals(code)) {
return c;
}
}
return null; return null;
} }
public void showAllCourses() { public void showAllCourses() {
// TODO: Print all courses in the system // 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) { public boolean addCourse(Course c) {
// TODO: Register the student in the course if capacity allows // TODO: Register the student in the course if capacity allows
// Return true if registration was successful, otherwise false // 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; return false;
} }
public boolean dropCourse(Course c) { public boolean dropCourse(Course c) {
// TODO: Remove the course from the student's list // TODO: Remove the course from the student's list
// Restore the course capacity if removal was successful // Restore the course capacity if removal was successful
// Return true if the course was dropped, otherwise false // Return true if the course was dropped, otherwise false
//return false;
if (registeredCourses.contains(c)) {
registeredCourses.remove(c);
c.increaseCapacity();
return true;
}
return false; return false;
} }
public void showRegisteredCourses() { public void showRegisteredCourses() {
// TODO: Print all registered courses // 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() + ")");
}
} }
} }