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
Showing only changes of commit c0c283e0c5 - Show all commits
+69 -8
View File
@@ -1,14 +1,75 @@
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
Course course1 = new Course("َAP", "1414", 2);
Course course2 = new Course("DS", "2201", 1);
Course course3 = new Course("DB", "3301", 3);
Student student1 = new Student("Ali", "402222001");
RegistrationSystem registrationSystem = new RegistrationSystem();
registrationSystem.addCourseToSystem(course1);
registrationSystem.addCourseToSystem(course2);
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) {
// 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 a : courses)
{
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;
}
public void showAllCourses() {
// TODO: Print all courses in the system
for (Course c : courses)
{
System.out.println(c.getCourseName() + " - " + c.getCourseCode() + " - capacity: " + c.getCapacity());
}
}
}
+47 -7
View File
@@ -21,19 +21,59 @@ 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.size() == 5)
{
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;
}
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
return false;
Course courseToRempove = null;
for (Course a : registeredCourses)
{
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;
}
registeredCourses.remove(courseToRempove);
System.out.println("Successfully dropped course.");
c.increaseCapacity();
return true;
}
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());
}
}
}