1 Commits
Author SHA1 Message Date
Soroush 3eea4f7372 Workshop-2 2026-04-21 13:19:38 +03:30
3 changed files with 155 additions and 18 deletions
+86 -8
View File
@@ -1,14 +1,92 @@
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:
// 1. Create at least 3 courses Course c1 = new Course("Advanced Programming", "AP1404", 2);
// 2. Create 1 student Course c2 = new Course("Data Structures", "DS2201", 1);
// 3. Create a registration system Course c3 = new Course("Database Systems", "DB3301", 3);
// 4. Add courses to the system Course c4 = new Course("Basics of Programming", "BP4041", 0);
// 5. Register the student in some courses
// 6. Drop one course RegistrationSystem system = new RegistrationSystem();
// 7. Print the final registered course list system.addCourseToSystem(c1);
system.addCourseToSystem(c2);
system.addCourseToSystem(c3);
system.addCourseToSystem(c3);
system.addCourseToSystem(c4);
Student student = new Student("Soroush", "404222000");
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\n===== 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. Search course by name");
System.out.println("6. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine();
if (choice == 1) {
system.showAllCourses();
}
else if (choice == 2) {
System.out.print("Enter course code to register: ");
String code = scanner.nextLine();
Course course = system.findCourseByCode(code);
if (course == null) {
System.out.print("Course not found.");
} else {
student.addCourse(course);
}
}
else if (choice == 3) {
System.out.print("Enter course code to drop: ");
String code = scanner.nextLine();
Course course = system.findCourseByCode(code);
if (course == null) {
System.out.print("Course not found.");
} else {
student.dropCourse(course);
}
}
else if (choice == 4) {
student.showRegisteredCourses();
}
else if (choice == 5) {
System.out.print("Enter course name to search: ");
String name = scanner.nextLine();
Course course = system.findCourseByName(name);
if (course == null) {
System.out.println("Course not found.");
} else {
//boolean isRegistered = false;
//for (int i = 0; i < student.registeredCourses.size(); i++) {
// if (student.registeredCourses.get(i).getCourseCode().equals(course.getCourseCode())) {
// isRegistered = true;
// break;
// }
//}
System.out.println("Course found: " + course.getCourseName() + " (" + course.getCourseCode() + ") - Remaining capacity: " + course.getCapacity());
//System.out.println("Status: " + (isRegistered ? "Registered" : "Not Registered"));
}
}
else if (choice == 6) {
System.out.println("Exiting...");
break;
}
else {
System.out.println("Invalid choice.");
}
}
scanner.close();
} }
} }
@@ -8,17 +8,45 @@ public class RegistrationSystem {
courses = new ArrayList<>(); courses = new ArrayList<>();
} }
// Adds the course to the system
public void addCourseToSystem(Course c) { public void addCourseToSystem(Course c) {
// TODO: Add the course to the system for (int i = 0; i < courses.size(); i++) {
if (courses.get(i).getCourseCode().equals(c.getCourseCode())) {
return;
}
}
courses.add(c);
} }
// Searches for a course by course code
public Course findCourseByCode(String code) { public Course findCourseByCode(String code) {
// TODO: Search for a course by course code for (int i = 0; i < courses.size(); i++) {
// Return the matching course if found, otherwise return null if (courses.get(i).getCourseCode().equals(code)) {
return courses.get(i);
}
}
return null; return null;
} }
// Searches for a course by course name
public Course findCourseByName(String name) {
for (int i = 0; i < courses.size(); i++) {
if (courses.get(i).getCourseName().equals(name)) {
return courses.get(i);
}
}
return null;
}
// Prints all courses in the system
public void showAllCourses() { public void showAllCourses() {
// TODO: Print all courses in the system if (courses.size() == 0) {
System.out.println("No courses found.");
} else {
for (int i = 0; i < courses.size(); i++) {
Course c = courses.get(i);
System.out.println(c.getCourseName() + " (" + c.getCourseCode() + ") - Remaining capacity: " + c.getCapacity());
}
}
} }
} }
+37 -6
View File
@@ -20,20 +20,51 @@ public class Student {
return studentId; return studentId;
} }
// Registers the student in the course if capacity allows
public boolean addCourse(Course c) { public boolean addCourse(Course c) {
// TODO: Register the student in the course if capacity allows if (registeredCourses.size() >= 2) {
// Return true if registration was successful, otherwise false System.out.println("You've reached the maximum limit of 2 courses.");
return false;
}
for (int i = 0; i < registeredCourses.size(); i++) {
if (registeredCourses.get(i).getCourseCode().equals(c.getCourseCode())) {
System.out.println("You're already registered in this course.");
return false;
}
}
if (c.getCapacity() > 0) {
registeredCourses.add(c);
c.reduceCapacity();
System.out.println("Course registered successfully.");
return true;
}
System.out.println("Course capacity is full.");
return false; return false;
} }
// Removes the course from the student's list
public boolean dropCourse(Course c) { public boolean dropCourse(Course c) {
// TODO: Remove the course from the student's list for (int i = 0; i < registeredCourses.size(); i++) {
// Restore the course capacity if removal was successful if (registeredCourses.get(i).getCourseCode().equals(c.getCourseCode())) {
// Return true if the course was dropped, otherwise false registeredCourses.remove(i);
c.increaseCapacity();
System.out.println("Course dropped successfully.");
return true;
}
}
System.out.println("You're already not registered in this course.");
return false; return false;
} }
// Prints all registered courses
public void showRegisteredCourses() { public void showRegisteredCourses() {
// TODO: Print all registered courses if (registeredCourses.size() == 0) {
System.out.println("No courses registered yet.");
} else {
for (int i = 0; i < registeredCourses.size(); i++) {
Course c = registeredCourses.get(i);
System.out.println(c.getCourseName() + " (" + c.getCourseCode() + ") - Remaining capacity: " + c.getCapacity());
}
}
} }
} }