WS2 bonus concluded!
This commit is contained in:
@@ -1,14 +1,28 @@
|
||||
package CourseRegistrationStarter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// TODO:
|
||||
// 1. Create at least 3 courses
|
||||
ArrayList<Course> courses = new ArrayList<Course>();
|
||||
courses.add(new Course("Advanced Programming", "AP1404", 2));
|
||||
courses.add(new Course("Data Structures", "DS2201", 1));
|
||||
courses.add(new Course(" Database Systems", "DB3301", 3));
|
||||
|
||||
// 2. Create 1 student
|
||||
Student student = new Student( "Ali","402222001");
|
||||
|
||||
// 3. Create a registration system
|
||||
RegistrationSystem regi = new RegistrationSystem();
|
||||
// 4. Add courses to the system
|
||||
for(Course c:courses)
|
||||
regi.addCourseToSystem(c);
|
||||
// 5. Register the student in some courses
|
||||
student.addCourse(courses.get(0));
|
||||
student.addCourse(courses.get(1));
|
||||
// 6. Drop one course
|
||||
student.dropCourse(courses.get(1));
|
||||
// 7. Print the final registered course list
|
||||
student.showRegisteredCourses();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,15 +10,30 @@ 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 (Course c:courses)
|
||||
if(c.getCourseCode()==code)
|
||||
return c;
|
||||
return null;
|
||||
}
|
||||
|
||||
public Course searchByCourseName(String name) {
|
||||
// bonus point 3
|
||||
for (Course c:courses)
|
||||
if(c.getCourseName()==name)
|
||||
return c;
|
||||
return null;
|
||||
}
|
||||
|
||||
public void showAllCourses() {
|
||||
// TODO: Print all courses in the system
|
||||
for(Course c : courses)
|
||||
System.out.println( c.getCourseName()+" "+c.getCourseCode()+" "+c.getCapacity() );
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,17 +23,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
|
||||
return false;
|
||||
//bonus point1 & bonus point2
|
||||
if(c.getCapacity()<0 || registeredCourses.contains(c)||registeredCourses.size()==3)
|
||||
return false;
|
||||
c.reduceCapacity();
|
||||
registeredCourses.add(c);
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
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.contains(c)){
|
||||
registeredCourses.remove(registeredCourses.indexOf(c));
|
||||
c.increaseCapacity();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void showRegisteredCourses() {
|
||||
// TODO: Print all registered courses
|
||||
for(Course c : registeredCourses)
|
||||
System.out.println(c.getCourseName()+" "+c.getCourseCode());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user