Merge pull request 'Complete assignment' (#1) from develop into main
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
@@ -4,11 +4,34 @@ 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
|
||||||
|
Course course1 = new Course("Advanced Programming", "AP1404", 2);
|
||||||
|
Course course2 = new Course("Data Structures", "Ds2201", 1);
|
||||||
|
Course course3 = new Course("Database Systems", "DB3301", 3);
|
||||||
|
|
||||||
// 2. Create 1 student
|
// 2. Create 1 student
|
||||||
|
Student student1 = new Student("Ali", "402222001");
|
||||||
|
|
||||||
// 3. Create a registration system
|
// 3. Create a registration system
|
||||||
|
RegistrationSystem SBU = new RegistrationSystem();
|
||||||
|
|
||||||
// 4. Add courses to the system
|
// 4. Add courses to the system
|
||||||
|
SBU.addCourseToSystem(course1);
|
||||||
|
SBU.addCourseToSystem(course2);
|
||||||
|
SBU.addCourseToSystem(course3);
|
||||||
|
|
||||||
// 5. Register the student in some courses
|
// 5. Register the student in some courses
|
||||||
|
student1.addCourse(course1);
|
||||||
|
student1.addCourse(course2);
|
||||||
|
|
||||||
// 6. Drop one course
|
// 6. Drop one course
|
||||||
|
student1.dropCourse(course2);
|
||||||
|
|
||||||
// 7. Print the final registered course list
|
// 7. Print the final registered course list
|
||||||
|
System.out.printf("\n%s\'s registered courses:\n", student1.getName());
|
||||||
|
student1.showRegisteredCourses();
|
||||||
|
|
||||||
|
System.out.println("\nRemaining informations of courses are as follows: ");
|
||||||
|
SBU.showAllCourses();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,15 +10,34 @@ 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 thisCourse : courses){
|
||||||
|
if(thisCourse.getCourseCode() == code){
|
||||||
|
return thisCourse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Course findCourseByName(String name){ // search by course name
|
||||||
|
for(Course thisCourse : courses){
|
||||||
|
if(thisCourse.getCourseName() == name){
|
||||||
|
return thisCourse;
|
||||||
|
}
|
||||||
|
}
|
||||||
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.println("Course Name | Course Code | Capacity");
|
||||||
|
for(Course thisCourse : courses){
|
||||||
|
System.out.printf("%s | %s | %d\n", thisCourse.getCourseName(), thisCourse.getCourseCode(), thisCourse.getCapacity());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ 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 allowedCourses = 2;
|
||||||
|
|
||||||
public Student(String name, String studentId) {
|
public Student(String name, String studentId) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@@ -23,6 +24,21 @@ 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
|
||||||
|
|
||||||
|
if(registeredCourses.contains(c)){ // Preventing duplicate course registration
|
||||||
|
System.out.printf("%s is already registered for %s\n", c.getCourseName(), name);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(registeredCourses.size() >= allowedCourses){ // Limiting the number of courses per student
|
||||||
|
System.out.printf("%s has reached his limit for registration.\n", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(c.reduceCapacity()){ // if enough capacity, reduction happens in this line
|
||||||
|
registeredCourses.add(c);
|
||||||
|
System.out.printf("%s registered successfully.\n", c.getCourseName());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,10 +46,20 @@ public class Student {
|
|||||||
// 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(c);
|
||||||
|
c.increaseCapacity();
|
||||||
|
System.out.printf("%s removed successfully.\n", c.getCourseName());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showRegisteredCourses() {
|
public void showRegisteredCourses() {
|
||||||
// TODO: Print all registered courses
|
// TODO: Print all registered courses
|
||||||
|
for(Course thisCourse : registeredCourses){
|
||||||
|
System.out.printf("%s <%s>\n", thisCourse.getCourseName(), thisCourse.getCourseCode());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user