Merge pull request 'implemented all methods' (#1) from develop into main

Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2026-04-29 17:58:57 +00:00
4 changed files with 151 additions and 11 deletions
+18
View File
@@ -4,6 +4,8 @@ public class Course {
private String courseName; private String courseName;
private String courseCode; private String courseCode;
private int capacity; private int capacity;
private boolean canDrop = false;
public Course(String courseName, String courseCode, int capacity) { public Course(String courseName, String courseCode, int capacity) {
this.courseName = courseName; this.courseName = courseName;
@@ -23,6 +25,15 @@ public class Course {
return capacity; return capacity;
} }
public boolean isDropped() {
return canDrop;
}
public void setCanDrop(boolean canDrop) {
this.canDrop = canDrop;
}
public boolean reduceCapacity() { public boolean reduceCapacity() {
if (capacity > 0) { if (capacity > 0) {
capacity--; capacity--;
@@ -34,4 +45,11 @@ public class Course {
public void increaseCapacity() { public void increaseCapacity() {
capacity++; capacity++;
} }
@Override
public String toString() {
return courseName + " (" + courseCode + ") " + "remaining capacity : " + capacity;
} }
}
+98
View File
@@ -1,5 +1,9 @@
package CourseRegistrationStarter; package CourseRegistrationStarter;
import javax.lang.model.element.NestingKind;
import java.util.Objects;
import java.util.Scanner;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// TODO: // TODO:
@@ -10,5 +14,99 @@ public class Main {
// 5. Register the student in some courses // 5. Register the student in some courses
// 6. Drop one course // 6. Drop one course
// 7. Print the final registered course list // 7. Print the final registered course list
Course course1 = new Course("Advanced Programming", "AP1404", 2);
Course course2 = new Course("Data Structures", "DS140401", 3);
Course course3 = new Course("Database System", "DS140402", 1);
Course course4 = new Course("Math2", "M1404", 4);
Student myStudent = new Student("mohammadReza", "Reza404222");
RegistrationSystem mySystem = new RegistrationSystem();
mySystem.addCourseToSystem(course1);
mySystem.addCourseToSystem(course2);
mySystem.addCourseToSystem(course3);
mySystem.addCourseToSystem(course4);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("==== Course Registration 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.println("choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine();
if (choice == 1) {
System.out.println("All courses on System: ");
mySystem.showAllCourses();
} else if (choice == 2) {
System.out.println("Enter course code to register: ");
String inputCode = scanner.nextLine();
Course selected = mySystem.findCourseByCode(inputCode);
if (selected == null) {
System.out.println("this code in invalid");
} else if (selected.isDropped()) {
System.out.println("you previously dropped this course and you can register again");
}
else if (myStudent.addCourse(selected) && selected.reduceCapacity()) {
System.out.println("course " + selected.getCourseName() + " registered successfully");
} else {
System.out.println("this course in full");
}
} else if (choice == 3) {
System.out.println("Enter course code to Drop it: ");
String inputCode = scanner.nextLine();
Course found = mySystem.findCourseByCode(inputCode);
if (found == null) {
System.out.println("this course has not registered yet");
} else {
if (myStudent.dropCourse(found)) {
found.reduceCapacity();
found.increaseCapacity();
System.out.println("course dropped successfully");
} else {
System.out.println("you are not registered in this course");
}
}
} else if (choice == 4) {
myStudent.showRegisteredCourses();
} else if (choice == 5) {
System.out.println("exiting...");
break;
}
}
scanner.close();
} }
} }
@@ -9,16 +9,23 @@ 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
// Return the matching course if found, otherwise return null for (Course c : courses) {
if (c.getCourseCode().equals(code))
return c;
}
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);
}
} }
} }
+23 -6
View File
@@ -21,19 +21,36 @@ public class Student {
} }
public boolean addCourse(Course c) { public boolean addCourse(Course c) {
// TODO: Register the student in the course if capacity allows
// Return true if registration was successful, otherwise false if (c.getCapacity() > 0) {
registeredCourses.add(c);
return true;
}
else
return false; return false;
} }
public boolean dropCourse(Course c) { public boolean dropCourse(Course c) {
// TODO: Remove the course from the student's list for (Course a : registeredCourses) {
// Restore the course capacity if removal was successful if(a.getCourseCode().equals(c.getCourseCode())) {
// Return true if the course was dropped, otherwise false registeredCourses.remove(c);
c.setCanDrop(true);
return true;
}
}
return false; return false;
} }
public void showRegisteredCourses() { public void showRegisteredCourses() {
// TODO: Print all registered courses
if (registeredCourses.isEmpty()) {
System.out.println("you have not registered in any courses yet");
return;
}
System.out.println("your registered courses: ");
for (Course c : registeredCourses) {
System.out.println(c);
}
} }
} }