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:
2026-04-25 21:47:47 +00:00
3 changed files with 68 additions and 0 deletions
+23
View File
@@ -4,11 +4,34 @@ public class Main {
public static void main(String[] args) {
// TODO:
// 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
Student student1 = new Student("Ali", "402222001");
// 3. Create a registration system
RegistrationSystem SBU = new RegistrationSystem();
// 4. Add courses to the system
SBU.addCourseToSystem(course1);
SBU.addCourseToSystem(course2);
SBU.addCourseToSystem(course3);
// 5. Register the student in some courses
student1.addCourse(course1);
student1.addCourse(course2);
// 6. Drop one course
student1.dropCourse(course2);
// 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) {
// 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 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;
}
public void showAllCourses() {
// 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 studentId;
private ArrayList<Course> registeredCourses;
private int allowedCourses = 2;
public Student(String name, String studentId) {
this.name = name;
@@ -23,6 +24,21 @@ 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)){ // 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;
}
@@ -30,10 +46,20 @@ 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
if(registeredCourses.contains(c)){
registeredCourses.remove(c);
c.increaseCapacity();
System.out.printf("%s removed successfully.\n", c.getCourseName());
return true;
}
return false;
}
public void showRegisteredCourses() {
// TODO: Print all registered courses
for(Course thisCourse : registeredCourses){
System.out.printf("%s <%s>\n", thisCourse.getCourseName(), thisCourse.getCourseCode());
}
}
}