complete all TODO sections #1

Merged
Meraj merged 1 commits from develop into main 2026-04-26 23:21:03 +00:00
3 changed files with 139 additions and 19 deletions
+69 -8
View File
@@ -1,14 +1,75 @@
package CourseRegistrationStarter; package CourseRegistrationStarter;
import java.util.Scanner;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// TODO: Course course1 = new Course("َAP", "1414", 2);
// 1. Create at least 3 courses Course course2 = new Course("DS", "2201", 1);
// 2. Create 1 student Course course3 = new Course("DB", "3301", 3);
// 3. Create a registration system Student student1 = new Student("Ali", "402222001");
// 4. Add courses to the system RegistrationSystem registrationSystem = new RegistrationSystem();
// 5. Register the student in some courses registrationSystem.addCourseToSystem(course1);
// 6. Drop one course registrationSystem.addCourseToSystem(course2);
// 7. Print the final registered course list registrationSystem.addCourseToSystem(course3);
boolean isChosen = false;
while (!isChosen)
{
System.out.println("\n--- Menu ---");
System.out.println("1) Add course");
System.out.println("2) Drop course");
System.out.println("3) Show registered courses");
System.out.println("4) Show all courses");
System.out.println("5) Exit");
System.out.print("Enter your choice: ");
Scanner in = new Scanner(System.in);
int chosen = in.nextInt();
if (chosen == 1)
{
System.out.print("Enter Course Code to add: ");
String codeToAdd = in.next();
Course courseToAdd = registrationSystem.findCourseByCode(codeToAdd);
if (courseToAdd != null)
{
student1.addCourse(courseToAdd);
}
else
{
System.out.println("Error: Course not found in system.");
}
}
else if (chosen == 2)
{
System.out.println("\n--- Drop Course ---");
System.out.print("Enter Course Code to drop: ");
String codeToDrop = in.next();
Course courseToDrop = registrationSystem.findCourseByCode(codeToDrop);
if (courseToDrop != null)
{
student1.dropCourse(courseToDrop);
}
}
else if (chosen == 3)
{
System.out.println("\n--- Registered Courses ---");
student1.showRegisteredCourses();
}
else if (chosen == 4)
{
System.out.println("\n--- All Available Courses ---");
registrationSystem.showAllCourses();
}
else if (chosen == 5)
{
isChosen = true;
}
else
{
System.out.println("Invalid! Please enter a number between 1 and 5.");
}
}
} }
} }
@@ -9,16 +9,35 @@ public class RegistrationSystem {
} }
public void addCourseToSystem(Course c) { public void addCourseToSystem(Course c) {
// 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 for (Course a : courses)
// Return the matching course if found, otherwise return null {
if (a.getCourseCode().equals(code))
{
return a;
}
}
return null;
}
public Course findCourseByName(String name)
{
for (Course a : courses)
{
if (a.getCourseName().equals(name))
{
return a;
}
}
return null; return null;
} }
public void showAllCourses() { public void showAllCourses() {
// TODO: Print all courses in the system for (Course c : courses)
{
System.out.println(c.getCourseName() + " - " + c.getCourseCode() + " - capacity: " + c.getCapacity());
}
} }
} }
+46 -6
View File
@@ -21,19 +21,59 @@ public class Student {
} }
public boolean addCourse(Course c) { public boolean addCourse(Course c) {
// TODO: Register the student in the course if capacity allows if (registeredCourses.size() == 5)
// Return true if registration was successful, otherwise false {
System.out.println("Maximum course limit (5) reached.");
return false;
}
for (Course a : registeredCourses)
{
if (a.getCourseCode().equals(c.getCourseCode()))
{
System.out.println("Error: you are already registered for this course!");
return false;
}
}
if (c.getCapacity() > 0)
{
registeredCourses.add(c);
c.reduceCapacity();
System.out.println("Registration was successful.");
return true;
}
return false; return false;
} }
public boolean dropCourse(Course c) { public boolean dropCourse(Course c) {
// TODO: Remove the course from the student's list Course courseToRempove = null;
// Restore the course capacity if removal was successful for (Course a : registeredCourses)
// Return true if the course was dropped, otherwise false {
if (a.getCourseCode().equals(c.getCourseCode()))
{
courseToRempove = a;
break;
}
}
if (courseToRempove == null)
{
System.out.println("Error: you are not registered for this course!");
return false; return false;
} }
registeredCourses.remove(courseToRempove);
System.out.println("Successfully dropped course.");
c.increaseCapacity();
return true;
}
public void showRegisteredCourses() { public void showRegisteredCourses() {
// TODO: Print all registered courses if (registeredCourses.isEmpty())
{
System.out.println("No courses registered.");
return;
}
for(Course a : registeredCourses)
{
System.out.println(a.getCourseName() + " - " + a.getCourseCode());
}
} }
} }