Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
07c0cd4885 |
Generated
+1
@@ -0,0 +1 @@
|
|||||||
|
Course.java
|
||||||
@@ -2,13 +2,34 @@ package CourseRegistrationStarter;
|
|||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// TODO:
|
// 1. Create courses
|
||||||
// 1. Create at least 3 courses
|
Course c1 = new Course("Advanced Programming", "AP1404", 2);
|
||||||
// 2. Create 1 student
|
Course c2 = new Course("Data Structures", "DS2201", 1);
|
||||||
// 3. Create a registration system
|
Course c3 = new Course("Database Systems", "DB3301", 3);
|
||||||
// 4. Add courses to the system
|
|
||||||
// 5. Register the student in some courses
|
// 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
|
// 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) {
|
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) {
|
public Course findCourseByCode(String code) {
|
||||||
// TODO: Search for a course by course code
|
for (Course c : courses) {
|
||||||
// Return the matching course if found, otherwise return null
|
if (c.getCourseCode().equals(code)) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
// List all courses
|
||||||
public void showAllCourses() {
|
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()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ package CourseRegistrationStarter;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Student {
|
public class Student {
|
||||||
private String name;
|
private final String name;
|
||||||
private String studentId;
|
private final String studentId;
|
||||||
private ArrayList<Course> registeredCourses;
|
private final ArrayList<Course> registeredCourses;
|
||||||
|
|
||||||
public Student(String name, String studentId) {
|
public Student(String name, String studentId) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@@ -21,19 +21,32 @@ public class Student {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean addCourse(Course c) {
|
public boolean addCourse(Course c) {
|
||||||
// TODO: Register the student in the course if capacity allows
|
// Prevent duplicate registration
|
||||||
// Return true if registration was successful, otherwise false
|
if (registeredCourses.contains(c)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean dropCourse(Course c) {
|
// Try to reduce capacity first
|
||||||
// TODO: Remove the course from the student's list
|
if (c.reduceCapacity()) {
|
||||||
// Restore the course capacity if removal was successful
|
registeredCourses.add(c);
|
||||||
// Return true if the course was dropped, otherwise false
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return 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() {
|
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() + ")");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user