feat: implement required features
This commit is contained in:
@@ -1,37 +1,45 @@
|
|||||||
package CourseRegistrationStarter;
|
package CourseRegistrationStarter;
|
||||||
|
|
||||||
public class Course {
|
public class Course
|
||||||
|
{
|
||||||
private String courseName;
|
private String courseName;
|
||||||
private String courseCode;
|
private String courseCode;
|
||||||
private int capacity;
|
private int capacity;
|
||||||
|
|
||||||
public Course(String courseName, String courseCode, int capacity) {
|
public Course(String courseName, String courseCode, int capacity)
|
||||||
|
{
|
||||||
this.courseName = courseName;
|
this.courseName = courseName;
|
||||||
this.courseCode = courseCode;
|
this.courseCode = courseCode;
|
||||||
this.capacity = capacity;
|
this.capacity = capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCourseName() {
|
public String getCourseName()
|
||||||
|
{
|
||||||
return courseName;
|
return courseName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCourseCode() {
|
public String getCourseCode()
|
||||||
|
{
|
||||||
return courseCode;
|
return courseCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCapacity() {
|
public int getCapacity()
|
||||||
|
{
|
||||||
return capacity;
|
return capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean reduceCapacity() {
|
public boolean reduceCapacity()
|
||||||
if (capacity > 0) {
|
{
|
||||||
|
if (capacity > 0)
|
||||||
|
{
|
||||||
capacity--;
|
capacity--;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void increaseCapacity() {
|
public void increaseCapacity()
|
||||||
|
{
|
||||||
capacity++;
|
capacity++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,70 @@
|
|||||||
package CourseRegistrationStarter;
|
package CourseRegistrationStarter;
|
||||||
|
|
||||||
public class Main {
|
public class Main
|
||||||
public static void main(String[] args) {
|
{
|
||||||
// TODO:
|
public static void main(String[] args)
|
||||||
// 1. Create at least 3 courses
|
{
|
||||||
// 2. Create 1 student
|
// 1. Creating courses
|
||||||
// 3. Create a registration system
|
Course ap = new Course("Advanced Programming", "AP1404", 2);
|
||||||
// 4. Add courses to the system
|
Course ds = new Course("Data Structures", "DS2201", 1);
|
||||||
// 5. Register the student in some courses
|
Course db = new Course("Database Systems", "DB3301", 3);
|
||||||
// 6. Drop one course
|
Course algo = new Course("Algorithms", "CS401", 2); // Another course for testing limits
|
||||||
// 7. Print the final registered course list
|
|
||||||
|
// 2. Creating student
|
||||||
|
Student student = new Student("steve", "1010101010101");
|
||||||
|
|
||||||
|
// 3. Creating a registration system
|
||||||
|
RegistrationSystem registrationSystem = new RegistrationSystem();
|
||||||
|
|
||||||
|
// 4. Adding courses to the system
|
||||||
|
registrationSystem.addCourseToSystem(ap);
|
||||||
|
registrationSystem.addCourseToSystem(ds);
|
||||||
|
registrationSystem.addCourseToSystem(db);
|
||||||
|
registrationSystem.addCourseToSystem(algo);
|
||||||
|
|
||||||
|
// Attempting to add a duplicate course code (should be prevented)
|
||||||
|
registrationSystem.addCourseToSystem(new Course("Advanced Programming 2", "AP1404", 5));
|
||||||
|
|
||||||
|
|
||||||
|
// 5. Registering the student in some courses
|
||||||
|
System.out.println("\n------- Attempting Course Registrations -------");
|
||||||
|
student.addCourse(ap); // Success
|
||||||
|
student.addCourse(ds); // Success
|
||||||
|
student.addCourse(db); // Success
|
||||||
|
|
||||||
|
// Attempting to register for a 4th course (should fail due to the course limit)
|
||||||
|
student.addCourse(algo); // Failure!
|
||||||
|
|
||||||
|
// Attempting to register for an already registered course (should fail)
|
||||||
|
student.addCourse(ap); // Failure!
|
||||||
|
|
||||||
|
// 6. Dropping one course
|
||||||
|
System.out.println("\n------- Attempting to Drop a Course -------");
|
||||||
|
// Attempting to drop a course the student is not registered in
|
||||||
|
student.dropCourse(algo); // Failure!
|
||||||
|
// Successfully drop a course
|
||||||
|
student.dropCourse(ds); // Success
|
||||||
|
|
||||||
|
// 7. Printing final results
|
||||||
|
student.showRegisteredCourses();
|
||||||
|
registrationSystem.showAllCourses();
|
||||||
|
|
||||||
|
// finding courses
|
||||||
|
System.out.println("\n------- Searching for Courses -------");
|
||||||
|
String searchCode = "DB3301";
|
||||||
|
Course foundByCode = registrationSystem.findCourseByCode(searchCode);
|
||||||
|
|
||||||
|
if (foundByCode != null)
|
||||||
|
System.out.println("Found course by code '" + searchCode + "': " + foundByCode.getCourseName());
|
||||||
|
else
|
||||||
|
System.out.println("Course with code '" + searchCode + "' is not found.");
|
||||||
|
|
||||||
|
String searchName = "data structures";
|
||||||
|
Course foundByName = registrationSystem.findCourseByName(searchName);
|
||||||
|
|
||||||
|
if (foundByName != null)
|
||||||
|
System.out.println("Found course by name '" + searchName + "': " + foundByName.getCourseName() + " (" + foundByName.getCourseCode() + ")");
|
||||||
|
else
|
||||||
|
System.out.println("Course with name '" + searchName + "' not found.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,62 @@
|
|||||||
package CourseRegistrationStarter;
|
package CourseRegistrationStarter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class RegistrationSystem {
|
public class RegistrationSystem
|
||||||
|
{
|
||||||
private ArrayList<Course> courses;
|
private ArrayList<Course> courses;
|
||||||
|
|
||||||
public RegistrationSystem() {
|
public RegistrationSystem()
|
||||||
|
{
|
||||||
courses = new ArrayList<>();
|
courses = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addCourseToSystem(Course c) {
|
public void addCourseToSystem(Course c)
|
||||||
// TODO: Add the course to the system
|
{
|
||||||
|
if (findCourseByCode(c.getCourseCode()) == null)
|
||||||
|
{
|
||||||
|
courses.add(c);
|
||||||
|
System.out.println("Course added to system: " + c.getCourseName() + " (" + c.getCourseCode() + ")");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
System.out.println("Error: Course with code " + c.getCourseCode() + " already exists in the system.");
|
||||||
}
|
}
|
||||||
|
|
||||||
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 Course findCourseByName(String name)
|
||||||
// TODO: Print all courses in the system
|
{
|
||||||
|
for (Course c : courses)
|
||||||
|
{
|
||||||
|
// Case-insensitive search
|
||||||
|
if (c.getCourseName().equalsIgnoreCase(name))
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void showAllCourses()
|
||||||
|
{
|
||||||
|
System.out.println("\n------- All Courses in System -------");
|
||||||
|
|
||||||
|
if (courses.isEmpty())
|
||||||
|
{
|
||||||
|
System.out.println("No course is available in the system.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (Course c : courses)
|
||||||
|
{
|
||||||
|
System.out.println("- " + c.getCourseName() + " (" + c.getCourseCode() + ")");
|
||||||
|
System.out.println(" Remaining Capacity: " + Objects.toString(c.getCapacity(), "N/A"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,39 +1,83 @@
|
|||||||
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 studentId;
|
||||||
private ArrayList<Course> registeredCourses;
|
private ArrayList<Course> registeredCourses;
|
||||||
|
|
||||||
public Student(String name, String studentId) {
|
private static final int MAX_COURSES = 3;
|
||||||
|
|
||||||
|
public Student(String name, String studentId)
|
||||||
|
{
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.studentId = studentId;
|
this.studentId = studentId;
|
||||||
this.registeredCourses = new ArrayList<>();
|
this.registeredCourses = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName()
|
||||||
|
{
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getStudentId() {
|
public String getStudentId()
|
||||||
|
{
|
||||||
return studentId;
|
return studentId;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 (registeredCourses.contains(c))
|
||||||
|
{
|
||||||
|
System.out.println("Error: Already registered in " + c.getCourseName() + " (" + c.getCourseCode() + ").");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (registeredCourses.size() >= MAX_COURSES)
|
||||||
|
{
|
||||||
|
System.out.println("Error: Cannot register for " + c.getCourseName() + ". Maximum of " + MAX_COURSES + " courses reached.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check and reduce capacity
|
||||||
|
if (c.reduceCapacity())
|
||||||
|
{
|
||||||
|
registeredCourses.add(c);
|
||||||
|
System.out.println("Successfully registered in " + c.getCourseName() + " (" + c.getCourseCode() + ").");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Error: Failed to register in " + c.getCourseName() + " (" + c.getCourseCode() + ") due to reaching maximum capacity.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean dropCourse(Course c) {
|
public boolean dropCourse(Course c)
|
||||||
// TODO: Remove the course from the student's list
|
{
|
||||||
// Restore the course capacity if removal was successful
|
if (registeredCourses.remove(c))
|
||||||
// Return true if the course was dropped, otherwise false
|
{
|
||||||
|
c.increaseCapacity();
|
||||||
|
System.out.println("Successfully dropped " + c.getCourseName() + " (" + c.getCourseCode() + ").");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Error: Course '" + c.getCourseName() + "' (" + c.getCourseCode() + ") was not found in registered courses.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showRegisteredCourses() {
|
public void showRegisteredCourses()
|
||||||
// TODO: Print all registered courses
|
{
|
||||||
|
System.out.println("\n------- " + name + "'s Registered Courses -------");
|
||||||
|
|
||||||
|
if (registeredCourses.isEmpty())
|
||||||
|
{
|
||||||
|
System.out.println("No course is registered.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Course c : registeredCourses)
|
||||||
|
System.out.println("- " + c.getCourseName() + " (" + c.getCourseCode() + ")");
|
||||||
|
|
||||||
|
System.out.println("Total courses: " + registeredCourses.size() + "/" + MAX_COURSES);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user