Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1e03e35f9c | ||
|
|
8f1cd0d721 |
@@ -2,13 +2,23 @@ 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 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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,16 +9,21 @@ 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 (Course course : this.courses) {
|
||||
System.out.println("----------------------------------------------");
|
||||
System.out.printf("Course Name: %s| Course ID: %s | Capacity %d\n", course.getCourseName(), course.getCourseCode(), course.getCapacity());
|
||||
System.out.println("----------------------------------------------");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,19 +21,30 @@ 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
|
||||
if(registeredCourses.contains(c))
|
||||
return false;
|
||||
if((c.getCapacity() > 0)){
|
||||
this.registeredCourses.add(c);
|
||||
c.reduceCapacity();
|
||||
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
|
||||
for(int i = 0; i < this.registeredCourses.size(); i++){
|
||||
if (this.registeredCourses.get(i).getCourseCode().equals(c.getCourseCode())){
|
||||
this.registeredCourses.remove(i);
|
||||
c.increaseCapacity();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void showRegisteredCourses() {
|
||||
// TODO: Print all registered courses
|
||||
for(int i = 0; i < registeredCourses.size(); i++){
|
||||
System.out.println(this.registeredCourses.get(i).getCourseName() + ", code: " + this.registeredCourses.get(i).getCourseCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user