WS2 bonus concluded!
This commit is contained in:
@@ -1,14 +1,28 @@
|
|||||||
package CourseRegistrationStarter;
|
package CourseRegistrationStarter;
|
||||||
|
import java.util.ArrayList;
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// TODO:
|
// TODO:
|
||||||
// 1. Create at least 3 courses
|
// 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
|
// 2. Create 1 student
|
||||||
|
Student student = new Student( "Ali","402222001");
|
||||||
|
|
||||||
// 3. Create a registration system
|
// 3. Create a registration system
|
||||||
|
RegistrationSystem regi = new RegistrationSystem();
|
||||||
// 4. Add courses to the system
|
// 4. Add courses to the system
|
||||||
|
for(Course c:courses)
|
||||||
|
regi.addCourseToSystem(c);
|
||||||
// 5. Register the student in some courses
|
// 5. Register the student in some courses
|
||||||
|
student.addCourse(courses.get(0));
|
||||||
|
student.addCourse(courses.get(1));
|
||||||
// 6. Drop one course
|
// 6. Drop one course
|
||||||
|
student.dropCourse(courses.get(1));
|
||||||
// 7. Print the final registered course list
|
// 7. Print the final registered course list
|
||||||
|
student.showRegisteredCourses();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,15 +10,30 @@ public class RegistrationSystem {
|
|||||||
|
|
||||||
public void addCourseToSystem(Course c) {
|
public void addCourseToSystem(Course c) {
|
||||||
// TODO: Add the course to the system
|
// 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
|
// TODO: Search for a course by course code
|
||||||
// Return the matching course if found, otherwise return null
|
// 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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showAllCourses() {
|
public void showAllCourses() {
|
||||||
// TODO: Print all courses in the system
|
// 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) {
|
public boolean addCourse(Course c) {
|
||||||
// TODO: Register the student in the course if capacity allows
|
// TODO: Register the student in the course if capacity allows
|
||||||
// Return true if registration was successful, otherwise false
|
// 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) {
|
public boolean dropCourse(Course c) {
|
||||||
// TODO: Remove the course from the student's list
|
// TODO: Remove the course from the student's list
|
||||||
// Restore the course capacity if removal was successful
|
// Restore the course capacity if removal was successful
|
||||||
// Return true if the course was dropped, otherwise false
|
// Return true if the course was dropped, otherwise false
|
||||||
|
if(registeredCourses.contains(c)){
|
||||||
|
registeredCourses.remove(registeredCourses.indexOf(c));
|
||||||
|
c.increaseCapacity();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showRegisteredCourses() {
|
public void showRegisteredCourses() {
|
||||||
// TODO: Print all registered courses
|
// TODO: Print all registered courses
|
||||||
|
for(Course c : registeredCourses)
|
||||||
|
System.out.println(c.getCourseName()+" "+c.getCourseCode());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user