workshop 02 #1

Open
HesamGhazi wants to merge 1 commits from develop into main
4 changed files with 69 additions and 24 deletions
Generated
+1
View File
@@ -0,0 +1 @@
Course.java
+28 -7
View File
@@ -2,13 +2,34 @@ 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
// 1. Create courses
Course c1 = new Course("Advanced Programming", "AP1404", 2);
Course c2 = new Course("Data Structures", "DS2201", 1);
Course c3 = new Course("Database Systems", "DB3301", 3);
// 2. Create student
Student student = new Student("Ali", "402222001");
// 3. Create registration system
RegistrationSystem system = new RegistrationSystem();
// 4. Add courses to system
system.addCourseToSystem(c1);
system.addCourseToSystem(c2);
system.addCourseToSystem(c3);
// 5. Register student in courses
student.addCourse(system.findCourseByCode("AP1404"));
student.addCourse(system.findCourseByCode("DS2201"));
// 6. Drop one course
// 7. Print the final registered course list
student.dropCourse(system.findCourseByCode("DS2201"));
// 7. Print final results
System.out.println("===== FINAL STUDENT COURSES =====");
student.showRegisteredCourses();
System.out.println("\n===== SYSTEM COURSES =====");
system.showAllCourses();
}
}
@@ -9,16 +9,26 @@ public class RegistrationSystem {
}
public void addCourseToSystem(Course c) {
// TODO: Add the course to the system
courses.add(c);
}
// we loop through all courses and check it with its code
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().equals(code)) {
return c;
}
}
return null;
}
// List all courses
public void showAllCourses() {
// TODO: Print all courses in the system
System.out.println("All Courses in System:");
for (Course c : courses) {
System.out.println(
c.getCourseName() + " | " +
c.getCourseCode() + " | Capacity: " +
c.getCapacity()
);
}
}
}
+24 -11
View File
@@ -2,9 +2,9 @@ package CourseRegistrationStarter;
import java.util.ArrayList;
public class Student {
private String name;
private String studentId;
private ArrayList<Course> registeredCourses;
private final String name;
private final String studentId;
private final ArrayList<Course> registeredCourses;
public Student(String name, String studentId) {
this.name = name;
@@ -21,19 +21,32 @@ 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
// Prevent duplicate registration
if (registeredCourses.contains(c)) {
return false;
}
// Try to reduce capacity first
if (c.reduceCapacity()) {
registeredCourses.add(c);
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
public boolean dropCourse(Course c) {if (registeredCourses.remove(c)) {
// after removing course, its capacity will be increased
c.increaseCapacity();
return true;
}
return false;
}
// List all registered courses of student
public void showRegisteredCourses() {
// TODO: Print all registered courses
System.out.println("Registered courses for " + name + ":");
for (Course c : registeredCourses) {
System.out.println(c.getCourseName() + " (" + c.getCourseCode() + ")");
}
}
}