4 Commits
4 changed files with 118 additions and 52 deletions
+8 -16
View File
@@ -1,27 +1,19 @@
package CourseRegistrationStarter;
public class Course {
private String courseName;
private String courseCode;
private String name;
private String code;
private int capacity;
public Course(String courseName, String courseCode, int capacity) {
this.courseName = courseName;
this.courseCode = courseCode;
public Course(String name, String code, int capacity) {
this.name = name;
this.code = code;
this.capacity = capacity;
}
public String getCourseName() {
return courseName;
}
public String getCourseCode() {
return courseCode;
}
public int getCapacity() {
return capacity;
}
public String getName() { return name; }
public String getCode() { return code; }
public int getCapacity() { return capacity; }
public boolean reduceCapacity() {
if (capacity > 0) {
+55 -8
View File
@@ -1,14 +1,61 @@
package CourseRegistrationStarter;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO:
// 1. Create at least 3 courses
// 2. Create 1 student
// 3. Create a registration system
// 4. Add courses to the system
// 5. Register the student in some courses
// 6. Drop one course
// 7. Print the final registered course list
Scanner scanner = new Scanner(System.in);
RegistrationSystem system = new RegistrationSystem();
system.addCourseToSystem(new Course("Advanced Programming", "CS101", 30));
system.addCourseToSystem(new Course("Data Structures", "CS102", 25));
system.addCourseToSystem(new Course("Database Systems", "CS103", 20));
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;
import java.util.ArrayList;
public class RegistrationSystem {
private ArrayList<Course> courses;
private ArrayList<Course> allCourses;
public RegistrationSystem() {
courses = new ArrayList<>();
this.allCourses = new ArrayList<>();
}
public void addCourseToSystem(Course c) {
// TODO: Add the course to the system
public void addCourseToSystem(Course course) {
allCourses.add(course);
}
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 : allCourses) {
if (c.getCode().equalsIgnoreCase(code)) return c;
}
return null;
}
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;
import java.util.ArrayList;
public class Student {
private String name;
private String studentId;
private String id;
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.studentId = studentId;
this.id = id;
this.registeredCourses = new ArrayList<>();
}
public String getName() {
return name;
public boolean addCourse(Course course) {
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() {
return studentId;
}
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;
}
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
public boolean dropCourse(String courseCode) {
for (Course c : registeredCourses) {
if (c.getCode().equals(courseCode)) {
c.increaseCapacity();
registeredCourses.remove(c);
return true;
}
}
return false;
}
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() + "]");
}
}
}
}
}