finishing TODO parts

This commit is contained in:
2026-04-20 23:15:32 +03:30
parent 8ddd529c85
commit 8f1cd0d721
3 changed files with 43 additions and 0 deletions
+21
View File
@@ -2,6 +2,27 @@ package CourseRegistrationStarter;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Course BP = new Course("Basic Programing", "1000", 5);
Course AP = new Course("Advanced Programing", "1001", 5);
Course M = new Course("Math", "1002", 5);
Student shayan = new Student("Shayan", "404212345");
RegistrationSystem regSys = new RegistrationSystem();
regSys.addCourseToSystem(AP);
regSys.addCourseToSystem(BP);
regSys.addCourseToSystem(M);
shayan.addCourse(BP);
shayan.addCourse(AP);
shayan.addCourse(M);
shayan.dropCourse(M);
shayan.showRegisteredCourses();
// TODO: // TODO:
// 1. Create at least 3 courses // 1. Create at least 3 courses
// 2. Create 1 student // 2. Create 1 student
@@ -9,10 +9,15 @@ public class RegistrationSystem {
} }
public void addCourseToSystem(Course c) { public void addCourseToSystem(Course c) {
courses.add(c);
// TODO: Add the course to the system // TODO: Add the course to the system
} }
public Course findCourseByCode(String code) { public Course findCourseByCode(String code) {
for(int i = 0; i < courses.size(); i++){
if (courses.get(i).getCourseCode().equals(code))
return courses.get(i);
}
// 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; return null;
@@ -21,12 +21,26 @@ public class Student {
} }
public boolean addCourse(Course c) { public boolean addCourse(Course c) {
if(registeredCourses.contains(c))
return false;
if((c.getCapacity() > 0)){
this.registeredCourses.add(c);
c.reduceCapacity();
return true;
}
// 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; return false;
} }
public boolean dropCourse(Course c) { public boolean dropCourse(Course c) {
for(int i = 0; i < this.registeredCourses.size(); i++){
if (this.registeredCourses.get(i) == c){
this.registeredCourses.remove(c);
c.increaseCapacity();
return true;
}
}
// 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
@@ -34,6 +48,9 @@ public class Student {
} }
public void showRegisteredCourses() { public void showRegisteredCourses() {
for(int i = 0; i < registeredCourses.size(); i++){
System.out.println(this.registeredCourses.get(i).getCourseName() + ", code: " + this.registeredCourses.get(i).getCourseCode());
}
// TODO: Print all registered courses // TODO: Print all registered courses
} }
} }