Finished "TODO" and Bonus sections #1

Open
farnam_jhn wants to merge 1 commits from develop into main
3 changed files with 68 additions and 17 deletions
Showing only changes of commit de5cddfe11 - Show all commits
+21 -8
View File
@@ -2,13 +2,26 @@ package CourseRegistrationStarter;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// TODO: Course AP = new Course("Advanced Programming", "4041122334", 40);
// 1. Create at least 3 courses Course FM = new Course("Foundation of Math", "4041212334", 30);
// 2. Create 1 student Course GM = new Course("General Math","4041234123",60);
// 3. Create a registration system
// 4. Add courses to the system Student farnam = new Student("Farnam", "404111234");
// 5. Register the student in some courses farnam.setLimitOfCourses(3);
// 6. Drop one course RegistrationSystem regSys = new RegistrationSystem();
// 7. Print the final registered course list
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) { public void addCourseToSystem(Course c) {
// 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 for (int i = 0; i < courses.size(); i++) {
// Return the matching course if found, otherwise return null 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; return null;
} }
public void showAllCourses() { public void showAllCourses() {
// TODO: Print all courses in the system // 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 name;
private String studentId; private String studentId;
private ArrayList<Course> registeredCourses; private ArrayList<Course> registeredCourses;
private int limitOfCourses;
public Student(String name, String studentId) { public Student(String name, String studentId) {
this.name = name; this.name = name;
this.studentId = studentId; this.studentId = studentId;
this.registeredCourses = new ArrayList<>(); this.registeredCourses = new ArrayList<>();
this.limitOfCourses = 18;
} }
public String getName() { public String getName() {
@@ -20,20 +22,37 @@ public class Student {
return studentId; return studentId;
} }
public void setLimitOfCourses(int limit){
this.limitOfCourses = limit;
}
public boolean addCourse(Course c) { 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; return false;
} }
public boolean dropCourse(Course c) { public boolean dropCourse(Course c) {
// TODO: Remove the course from the student's list
// Restore the course capacity if removal was successful if (registeredCourses.remove(c)){
// Return true if the course was dropped, otherwise false c.increaseCapacity();
return true;
}
return false; return false;
} }
public void showRegisteredCourses() { 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');
}
} }
} }