Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3890a4a484 | ||
|
|
53248f4b4f |
@@ -10,5 +10,17 @@ 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("Python for beginners", "41842373", 10);
|
||||
Course c2 = new Course("Advanced Python", "42842335", 22);
|
||||
Course c3 = new Course("Java for beginners", "17289682", 7);
|
||||
Student s1 = new Student("Barbod", "9341264180");
|
||||
RegistrationSystem r1 = new RegistrationSystem();
|
||||
r1.addCourseToSystem(c1);
|
||||
r1.addCourseToSystem(c2);
|
||||
r1.addCourseToSystem(c3);
|
||||
s1.addCourse(c1);
|
||||
s1.addCourse(c3);
|
||||
s1.dropCourse(c1);
|
||||
s1.showRegisteredCourses();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,15 +10,32 @@ 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 void showAllCourses() {
|
||||
// TODO: Print all courses in the system
|
||||
|
||||
for (int i = 0; i < courses.size(); i++) {
|
||||
System.out.print("Course name: ");
|
||||
System.out.print(courses.get(i).getCourseName());
|
||||
System.out.print(" Code ");
|
||||
System.out.print(courses.get(i).getCourseCode());
|
||||
System.out.print("Remaining capacity: ");
|
||||
System.out.println(courses.get(i).getCourseName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,18 @@ 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
|
||||
|
||||
for (int i = 0; i < registeredCourses.size(); i++) { //Bonus
|
||||
if (registeredCourses.get(i).equals(c)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
int limit = 10; //Bonus
|
||||
if (c.getCapacity() > 0 && registeredCourses.size() <= limit) {
|
||||
registeredCourses.add(c);
|
||||
c.reduceCapacity();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -30,10 +42,34 @@ public class Student {
|
||||
// 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
|
||||
|
||||
for (int i = 0; i < registeredCourses.size(); i++) {
|
||||
if (registeredCourses.get(i).equals(c)) {
|
||||
registeredCourses.remove(c);
|
||||
c.increaseCapacity();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean findCourseByName(String name) { //Bonus
|
||||
for (int i = 0; i < registeredCourses.size(); i++) {
|
||||
if (registeredCourses.get(i).getCourseName().equals(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void showRegisteredCourses() {
|
||||
// TODO: Print all registered courses
|
||||
|
||||
for (int i = 0; i < registeredCourses.size(); i++) {
|
||||
System.out.print("Course name: ");
|
||||
System.out.print(registeredCourses.get(i).getCourseName());
|
||||
System.out.print(" Code ");
|
||||
System.out.println(registeredCourses.get(i).getCourseCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user