3 Commits
Author SHA1 Message Date
Razia bb9cc82763 Merge pull request 'wh2' (#2) from main into develop
Reviewed-on: Razia/WS-02-intro-to-oop#2
2026-07-15 14:59:41 +00:00
2025mohseni 8332230000 WH2 2026-07-15 18:11:56 +03:30
Meraj 3cb0489679 Merge pull request 'complete all TODO sections' (#1) from develop into main
Reviewed-on: #1
2026-04-26 23:21:02 +00:00
4 changed files with 102 additions and 156 deletions
+8 -16
View File
@@ -1,27 +1,19 @@
package CourseRegistrationStarter; package CourseRegistrationStarter;
public class Course { public class Course {
private String courseName; private String name;
private String courseCode; private String code;
private int capacity; private int capacity;
public Course(String courseName, String courseCode, int capacity) { public Course(String name, String code, int capacity) {
this.courseName = courseName; this.name = name;
this.courseCode = courseCode; this.code = code;
this.capacity = capacity; this.capacity = capacity;
} }
public String getCourseName() { public String getName() { return name; }
return courseName; public String getCode() { return code; }
} public int getCapacity() { return capacity; }
public String getCourseCode() {
return courseCode;
}
public int getCapacity() {
return capacity;
}
public boolean reduceCapacity() { public boolean reduceCapacity() {
if (capacity > 0) { if (capacity > 0) {
+47 -61
View File
@@ -4,72 +4,58 @@ import java.util.Scanner;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Course course1 = new Course("َAP", "1414", 2); Scanner scanner = new Scanner(System.in);
Course course2 = new Course("DS", "2201", 1); RegistrationSystem system = new RegistrationSystem();
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) system.addCourseToSystem(new Course("Advanced Programming", "CS101", 30));
{ system.addCourseToSystem(new Course("Data Structures", "CS102", 25));
student1.addCourse(courseToAdd); system.addCourseToSystem(new Course("Database Systems", "CS103", 20));
}
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)
{ System.out.print("Enter Student Name: ");
student1.dropCourse(courseToDrop); String sName = scanner.nextLine();
System.out.print("Enter Student ID: ");
String sId = scanner.nextLine();
Student student = new Student(sName, sId);
boolean running = true;
while (running) {
System.out.println("\n1. View All Courses\n2. Add Course\n3. Drop Course\n4. My Courses\n5. Exit");
System.out.print("Choice: ");
String choice = scanner.nextLine();
switch (choice) {
case "1":
system.showAllCourses();
break;
case "2":
System.out.print("Enter course code to add: ");
String addCode = scanner.nextLine();
Course toAdd = system.findCourseByCode(addCode);
if (toAdd != null) {
if (student.addCourse(toAdd)) System.out.println("Successfully added.");
} else {
System.out.println("Course not found!");
} }
} break;
else if (chosen == 3) case "3":
{ System.out.print("Enter course code to drop: ");
System.out.println("\n--- Registered Courses ---"); String dropCode = scanner.nextLine();
student1.showRegisteredCourses(); if (student.dropCourse(dropCode)) System.out.println("Successfully dropped.");
} else System.out.println("You are not registered in this course.");
else if (chosen == 4) break;
{ case "4":
System.out.println("\n--- All Available Courses ---"); student.showRegisteredCourses();
registrationSystem.showAllCourses(); break;
} case "5":
else if (chosen == 5) running = false;
{ System.out.println("Exiting...");
isChosen = true; break;
} default:
else System.out.println("Invalid option.");
{
System.out.println("Invalid! Please enter a number between 1 and 5.");
} }
} }
scanner.close();
} }
} }
@@ -1,43 +1,29 @@
package CourseRegistrationStarter; package CourseRegistrationStarter;
import java.util.ArrayList; import java.util.ArrayList;
public class RegistrationSystem { public class RegistrationSystem {
private ArrayList<Course> courses; private ArrayList<Course> allCourses;
public RegistrationSystem() { public RegistrationSystem() {
courses = new ArrayList<>(); this.allCourses = new ArrayList<>();
} }
public void addCourseToSystem(Course c) { public void addCourseToSystem(Course course) {
courses.add(c); allCourses.add(course);
} }
public Course findCourseByCode(String code) { public Course findCourseByCode(String code) {
for (Course a : courses) for (Course c : allCourses) {
{ if (c.getCode().equalsIgnoreCase(code)) return c;
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() {
for (Course c : courses) System.out.println("\n--- Available Courses in System ---");
{ for (Course c : allCourses) {
System.out.println(c.getCourseName() + " - " + c.getCourseCode() + " - capacity: " + c.getCapacity()); System.out.println("Name: " + c.getName() + " | Code: " + c.getCode() + " | Capacity: " + c.getCapacity());
} }
} }
} }
+31 -49
View File
@@ -1,79 +1,61 @@
package CourseRegistrationStarter; package CourseRegistrationStarter;
import java.util.ArrayList; import java.util.ArrayList;
public class Student { public class Student {
private String name; private String name;
private String studentId; private String id;
private ArrayList<Course> registeredCourses; private ArrayList<Course> registeredCourses;
private final int MAX_COURSES = 5;
public Student(String name, String studentId) { public Student(String name, String id) {
this.name = name; this.name = name;
this.studentId = studentId; this.id = id;
this.registeredCourses = new ArrayList<>(); this.registeredCourses = new ArrayList<>();
} }
public String getName() { public boolean addCourse(Course course) {
return name;
}
public String getStudentId() { if (registeredCourses.size() >= MAX_COURSES) {
return studentId; System.out.println("Error: Maximum course limit reached (5).");
}
public boolean addCourse(Course c) {
if (registeredCourses.size() == 5)
{
System.out.println("Maximum course limit (5) reached.");
return false; return false;
} }
for (Course a : registeredCourses)
{ for (Course c : registeredCourses) {
if (a.getCourseCode().equals(c.getCourseCode())) if (c.getCode().equals(course.getCode())) {
{ System.out.println("Error: Already registered in " + course.getName());
System.out.println("Error: you are already registered for this course!");
return false; return false;
} }
} }
if (c.getCapacity() > 0)
{ if (course.reduceCapacity()) {
registeredCourses.add(c); registeredCourses.add(course);
c.reduceCapacity();
System.out.println("Registration was successful.");
return true; return true;
} } else {
System.out.println("Error: No capacity left in " + course.getName());
return false; return false;
} }
}
public boolean dropCourse(Course c) { public boolean dropCourse(String courseCode) {
Course courseToRempove = null; for (Course c : registeredCourses) {
for (Course a : registeredCourses) if (c.getCode().equals(courseCode)) {
{
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(); c.increaseCapacity();
registeredCourses.remove(c);
return true; return true;
} }
}
return false;
}
public void showRegisteredCourses() { public void showRegisteredCourses() {
if (registeredCourses.isEmpty()) System.out.println("\n--- Registered Courses for " + name + " (" + id + ") ---");
{ if (registeredCourses.isEmpty()) {
System.out.println("No courses registered."); System.out.println("No courses registered yet.");
return; } else {
for (Course c : registeredCourses) {
System.out.println("- " + c.getName() + " [" + c.getCode() + "]");
} }
for(Course a : registeredCourses)
{
System.out.println(a.getCourseName() + " - " + a.getCourseCode());
} }
} }
} }