diff --git a/.idea/misc.xml b/.idea/misc.xml index a20905f..fb6489a 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,6 @@ + diff --git a/src/CourseRegistrationStarter/Course.java b/src/CourseRegistrationStarter/Course.java index 7f13a14..ffccb07 100644 --- a/src/CourseRegistrationStarter/Course.java +++ b/src/CourseRegistrationStarter/Course.java @@ -35,3 +35,4 @@ public class Course { capacity++; } } + diff --git a/src/CourseRegistrationStarter/Main.java b/src/CourseRegistrationStarter/Main.java index 8f06373..c041039 100644 --- a/src/CourseRegistrationStarter/Main.java +++ b/src/CourseRegistrationStarter/Main.java @@ -1,14 +1,93 @@ -package CourseRegistrationStarter; +import java.util.Scanner; -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 - // 6. Drop one course - // 7. Print the final registered course list +public class Main +{ + + public static void main(String[] args) + { + + Scanner input = new Scanner(System.in); + + // Courses + Course ap = new Course("Advanced Programming", "AP1404", 2); + Course ds = new Course("Data Structures", "DS2201", 1); + Course db = new Course("Database Systems", "DB3301", 3); + + // System + RegistrationSystem system = new RegistrationSystem(); + system.addCourseToSystem(ap); + system.addCourseToSystem(ds); + system.addCourseToSystem(db); + + // Student + System.out.print("Enter student name: "); + String name = input.nextLine(); + + System.out.print("Enter student id: "); + String id = input.nextLine(); + + Student student = new Student(name, id); + + int choice = 0; + + while (choice != 5) + { + + System.out.println("\n--- MENU ---"); + System.out.println("1. Show all courses"); + System.out.println("2. Register course"); + System.out.println("3. Drop course"); + System.out.println("4. Show my courses"); + System.out.println("5. Exit"); + + System.out.print("Choice: "); + choice = input.nextInt(); + input.nextLine(); + + switch (choice) + { + + case 1: + system.showAllCourses(); + break; + + case 2: + System.out.print("Enter course code: "); + String regCode = input.nextLine(); + Course c1 = system.findCourseByCode(regCode); + if (c1 != null) + { + student.addCourse(c1); + } else + { + System.out.println("Course not found"); + } + break; + + case 3: + System.out.print("Enter course code: "); + String dropCode = input.nextLine(); + Course c2 = system.findCourseByCode(dropCode); + if (c2 != null) + { + student.dropCourse(c2); + } else + { + System.out.println("Course not found"); + } + break; + + case 4: + student.showRegisteredCourses(); + break; + + case 5: + System.out.println("Goodbye!"); + break; + + default: + System.out.println("Invalid choice"); + } + } } -} +} \ No newline at end of file diff --git a/src/CourseRegistrationStarter/RegistrationSystem.java b/src/CourseRegistrationStarter/RegistrationSystem.java index 934f222..700763c 100644 --- a/src/CourseRegistrationStarter/RegistrationSystem.java +++ b/src/CourseRegistrationStarter/RegistrationSystem.java @@ -1,24 +1,44 @@ -package CourseRegistrationStarter; import java.util.ArrayList; -public class RegistrationSystem { +public class RegistrationSystem +{ private ArrayList courses; - public RegistrationSystem() { + public RegistrationSystem() + { courses = new ArrayList<>(); } - public void addCourseToSystem(Course c) { - // TODO: Add the course to the system + public void addCourseToSystem(Course c) + { + // 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 + public Course findCourseByCode(String code) + { + // Search for a course by course code + for (Course c : courses) + { + if (c.getCourseCode().equals(code)) + { + return c; + } + } return null; } - public void showAllCourses() { - // TODO: Print all courses in the system + public void showAllCourses() + { + // Print all courses in the system + for (Course c : courses) + { + System.out.println( + c.getCourseName() + " - " + + c.getCourseCode() + + " | Remaining capacity: " + + c.getCapacity() + ); + } } } diff --git a/src/CourseRegistrationStarter/Student.java b/src/CourseRegistrationStarter/Student.java index 02c11d8..858eef7 100644 --- a/src/CourseRegistrationStarter/Student.java +++ b/src/CourseRegistrationStarter/Student.java @@ -1,39 +1,60 @@ -package CourseRegistrationStarter; import java.util.ArrayList; -public class Student { +public class Student +{ private String name; private String studentId; private ArrayList registeredCourses; - public Student(String name, String studentId) { + public Student(String name, String studentId) + { this.name = name; this.studentId = studentId; this.registeredCourses = new ArrayList<>(); } - public String getName() { + public String getName() + { return name; } - public String getStudentId() { + public String getStudentId() + { return studentId; } - public boolean addCourse(Course c) { - // TODO: Register the student in the course if capacity allows - // Return true if registration was successful, otherwise false + public boolean addCourse(Course c) + { + // Check capacity > 0 + if (c.getCapacity() > 0) + { + registeredCourses.add(c); + c.reduceCapacity(); + 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)) + { + c.increaseCapacity(); + return true; + } return false; } - public void showRegisteredCourses() { - // TODO: Print all registered courses + public void showRegisteredCourses() + { + System.out.println("Registered Courses for " + name + ":"); + if (registeredCourses.isEmpty()) + { + System.out.println("No courses registered."); + } + for (Course c : registeredCourses) + { + System.out.println(c.getCourseName() + " - " + c.getCourseCode()); + } } } \ No newline at end of file