Finished "TODO" and Bonus sections

This commit is contained in:
2026-04-19 21:44:23 -07:00
parent fc029ef2a5
commit de5cddfe11
3 changed files with 68 additions and 17 deletions
+21 -8
View File
@@ -2,13 +2,26 @@ package CourseRegistrationStarter;
public class Main {
public static void main(String[] args) {
// TODO:
// 1. Create at least 3 courses
// 2. Create 1 student
// 3. Create a registration system
// 4. Add courses to the system
// 5. Register the student in some courses
// 6. Drop one course
// 7. Print the final registered course list
Course AP = new Course("Advanced Programming", "4041122334", 40);
Course FM = new Course("Foundation of Math", "4041212334", 30);
Course GM = new Course("General Math","4041234123",60);
Student farnam = new Student("Farnam", "404111234");
farnam.setLimitOfCourses(3);
RegistrationSystem regSys = new RegistrationSystem();
regSys.addCourseToSystem(AP);
regSys.addCourseToSystem(FM);
regSys.addCourseToSystem(GM);
farnam.addCourse(AP);
farnam.addCourse(FM);
farnam.addCourse(GM);
farnam.dropCourse(FM);
farnam.showRegisteredCourses();
regSys.showAllCourses();
}
}
@@ -9,16 +9,35 @@ 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 Course findCourseByName(String name) {
for (int i = 0; i < courses.size(); i++) {
if (courses.get(i).getCourseCode().equals(name)){
return courses.get(i);
}
}
return null;
}
public void showAllCourses() {
// TODO: Print all courses in the system
System.out.printf("%25s %15s %18s", "Name","Course code","Remaining capacity");
System.out.print('\n');
for (int i = 0; i < courses.size(); i++) {
System.out.printf("%25s %15s %d", courses.get(i).getCourseName() , courses.get(i).getCourseCode(), courses.get(i).getCapacity());
System.out.print('\n');
}
}
}
+25 -6
View File
@@ -5,11 +5,13 @@ public class Student {
private String name;
private String studentId;
private ArrayList<Course> registeredCourses;
private int limitOfCourses;
public Student(String name, String studentId) {
this.name = name;
this.studentId = studentId;
this.registeredCourses = new ArrayList<>();
this.limitOfCourses = 18;
}
public String getName() {
@@ -20,20 +22,37 @@ public class Student {
return studentId;
}
public void setLimitOfCourses(int limit){
this.limitOfCourses = limit;
}
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)){
return false;
}
if ((c.getCapacity() > 0) && (this.limitOfCourses >= registeredCourses.size() + 1)){
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
if (registeredCourses.remove(c)){
c.increaseCapacity();
return true;
}
return false;
}
public void showRegisteredCourses() {
// TODO: Print all registered courses
for (int i = 0; i < registeredCourses.size(); i++) {
System.out.printf("%25s %15s", registeredCourses.get(i).getCourseName() , registeredCourses.get(i).getCourseCode());
System.out.print('\n');
}
}
}