3 Commits
Author SHA1 Message Date
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: ArefeRoosta/WS-02-intro-to-oop#1
2026-04-26 23:21:02 +00:00
ArefeRoosta c0c283e0c5 complete all TODO sections 2026-04-20 20:48:20 +03:30
4 changed files with 118 additions and 52 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) {
+55 -8
View File
@@ -1,14 +1,61 @@
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: Scanner scanner = new Scanner(System.in);
// 1. Create at least 3 courses RegistrationSystem system = new RegistrationSystem();
// 2. Create 1 student
// 3. Create a registration system
// 4. Add courses to the system system.addCourseToSystem(new Course("Advanced Programming", "CS101", 30));
// 5. Register the student in some courses system.addCourseToSystem(new Course("Data Structures", "CS102", 25));
// 6. Drop one course system.addCourseToSystem(new Course("Database Systems", "CS103", 20));
// 7. Print the final registered course list
System.out.print("Enter Student Name: ");
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;
case "3":
System.out.print("Enter course code to drop: ");
String dropCode = scanner.nextLine();
if (student.dropCourse(dropCode)) System.out.println("Successfully dropped.");
else System.out.println("You are not registered in this course.");
break;
case "4":
student.showRegisteredCourses();
break;
case "5":
running = false;
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid option.");
}
}
scanner.close();
} }
} }
@@ -1,24 +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) {
// TODO: Add the course to the system allCourses.add(course);
} }
public Course findCourseByCode(String code) { public Course findCourseByCode(String code) {
// TODO: Search for a course by course code for (Course c : allCourses) {
// Return the matching course if found, otherwise return null if (c.getCode().equalsIgnoreCase(code)) return c;
}
return null; return null;
} }
public void showAllCourses() { public void showAllCourses() {
// TODO: Print all courses in the system System.out.println("\n--- Available Courses in System ---");
for (Course c : allCourses) {
System.out.println("Name: " + c.getName() + " | Code: " + c.getCode() + " | Capacity: " + c.getCapacity());
}
} }
} }
+43 -21
View File
@@ -1,39 +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;
if (registeredCourses.size() >= MAX_COURSES) {
System.out.println("Error: Maximum course limit reached (5).");
return false;
}
for (Course c : registeredCourses) {
if (c.getCode().equals(course.getCode())) {
System.out.println("Error: Already registered in " + course.getName());
return false;
}
}
if (course.reduceCapacity()) {
registeredCourses.add(course);
return true;
} else {
System.out.println("Error: No capacity left in " + course.getName());
return false;
}
} }
public String getStudentId() { public boolean dropCourse(String courseCode) {
return studentId; for (Course c : registeredCourses) {
} if (c.getCode().equals(courseCode)) {
c.increaseCapacity();
public boolean addCourse(Course c) { registeredCourses.remove(c);
// TODO: Register the student in the course if capacity allows return true;
// Return true if registration was successful, otherwise false }
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; return false;
} }
public void showRegisteredCourses() { public void showRegisteredCourses() {
// TODO: Print all registered courses System.out.println("\n--- Registered Courses for " + name + " (" + id + ") ---");
if (registeredCourses.isEmpty()) {
System.out.println("No courses registered yet.");
} else {
for (Course c : registeredCourses) {
System.out.println("- " + c.getName() + " [" + c.getCode() + "]");
}
}
} }
} }