completing WS02 #1

Merged
rharokni merged 1 commits from develop into main 2026-05-20 10:41:56 +00:00
4 changed files with 106 additions and 3 deletions
+1
View File
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
+74 -1
View File
@@ -1,14 +1,87 @@
package CourseRegistrationStarter;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO:
// 1. Create at least 3 courses
Course c1 = new Course("Java", "4042", 50);
Course c2 = new Course("C++", "4041", 50);
Course c3 = new Course("Python", "4040", 40);
// 2. Create 1 student
Student student1 = new Student("Sara", "404222061");
// 3. Create a registration system
RegistrationSystem RG1 = new RegistrationSystem();
// 4. Add courses to the system
RG1.addCourseToSystem(c1);
RG1.addCourseToSystem(c2);
RG1.addCourseToSystem(c3);
// 5. Register the student in some courses
// 6. Drop one course
// 7. Print the final registered course list
Scanner input = new Scanner(System.in);
while(true) {
System.out.println("----- Registration Menu ----- ");
System.out.println("1. Show all courses");
System.out.println("2. Register courses");
System.out.println("3. Drop courses");
System.out.println("4. Show my courses");
System.out.println("5. Exit");
System.out.print("Choose Your Option(1 to 5): ");
System.out.println();
int choice = input.nextInt();
if (choice == 1) {
RG1.showAllCourses();
}
else if (choice == 2) {
System.out.println("choose course you want to register: ");
Scanner input1 = new Scanner(System.in);
String c = input1.nextLine();
if (c.equals("c1")) {
student1.addCourse(c1);
}
else if (c.equals("c2")) {
student1.addCourse(c2);
}
else if (c.equals("c3")) {
student1.addCourse(c3);
}
else
break;
}
else if (choice == 3) {
System.out.println("drop your courses: ");
Scanner input2 = new Scanner(System.in);
String c = input2.nextLine();
if (c.equals("c1")) {
student1.dropCourse(c1);
}
else if (c.equals("c2")) {
student1.dropCourse(c2);
}
else if (c.equals("c3")) {
student1.dropCourse(c3);
}
else
break;
}
// I think I should have used findCourseByCode() for choice 3 & 4!
else if (choice == 4) {
student1.showRegisteredCourses();
}
else {
break;
}
}
}
}
@@ -10,15 +10,25 @@ 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 course : courses) {
if (course.getCourseCode().equals(code)) {
return course;
}
}
return null;
}
public void showAllCourses() {
// TODO: Print all courses in the system
for (Course course : courses) {
System.out.println(course + ", " + course.getCourseCode() + ", " + "remaining capacity: " + course.getCapacity());
}
}
}
+20 -1
View File
@@ -23,17 +23,36 @@ 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
return false;
if (c.getCapacity() > 0) {
registeredCourses.add(c);
c.reduceCapacity();
return true;
}
else {
return false;
}
// NOT SURE about else statement cause I delete the outer 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
for (Course course: registeredCourses) {
if (c.equals(course.getCourseCode())) {
registeredCourses.remove(c);
course.increaseCapacity();
return true;
}
}
return false;
}
public void showRegisteredCourses() {
// TODO: Print all registered courses
for (Course course: registeredCourses) {
System.out.println(course);
}
}
}