Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
27a22c95f5 |
@@ -1,37 +1,45 @@
|
||||
package CourseRegistrationStarter;
|
||||
|
||||
public class Course {
|
||||
public class Course
|
||||
{
|
||||
private String courseName;
|
||||
private String courseCode;
|
||||
private int capacity;
|
||||
|
||||
public Course(String courseName, String courseCode, int capacity) {
|
||||
public Course(String courseName, String courseCode, int capacity)
|
||||
{
|
||||
this.courseName = courseName;
|
||||
this.courseCode = courseCode;
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public String getCourseName() {
|
||||
public String getCourseName()
|
||||
{
|
||||
return courseName;
|
||||
}
|
||||
|
||||
public String getCourseCode() {
|
||||
public String getCourseCode()
|
||||
{
|
||||
return courseCode;
|
||||
}
|
||||
|
||||
public int getCapacity() {
|
||||
public int getCapacity()
|
||||
{
|
||||
return capacity;
|
||||
}
|
||||
|
||||
public boolean reduceCapacity() {
|
||||
if (capacity > 0) {
|
||||
public boolean reduceCapacity()
|
||||
{
|
||||
if (capacity > 0)
|
||||
{
|
||||
capacity--;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void increaseCapacity() {
|
||||
public void increaseCapacity()
|
||||
{
|
||||
capacity++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,70 @@
|
||||
package CourseRegistrationStarter;
|
||||
|
||||
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
|
||||
public class Main
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// 1. Creating courses
|
||||
Course ap = new Course("Advanced Programming", "AP1404", 2);
|
||||
Course ds = new Course("Data Structures", "DS2201", 1);
|
||||
Course db = new Course("Database Systems", "DB3301", 3);
|
||||
Course algo = new Course("Algorithms", "CS401", 2); // Another course for testing limits
|
||||
|
||||
// 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;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
|
||||
public class RegistrationSystem {
|
||||
public class RegistrationSystem
|
||||
{
|
||||
private ArrayList<Course> courses;
|
||||
|
||||
public RegistrationSystem() {
|
||||
public RegistrationSystem()
|
||||
{
|
||||
courses = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addCourseToSystem(Course c) {
|
||||
// TODO: Add the course to the system
|
||||
public void addCourseToSystem(Course c)
|
||||
{
|
||||
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) {
|
||||
// TODO: Search for a course by course code
|
||||
// Return the matching course if found, otherwise return null
|
||||
public Course findCourseByCode(String code)
|
||||
{
|
||||
for (Course c : courses)
|
||||
{
|
||||
if (c.getCourseCode().equals(code))
|
||||
return c;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void showAllCourses() {
|
||||
// TODO: Print all courses in the system
|
||||
public Course findCourseByName(String name)
|
||||
{
|
||||
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;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Student {
|
||||
public class Student
|
||||
{
|
||||
private String name;
|
||||
private String studentId;
|
||||
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.studentId = studentId;
|
||||
this.registeredCourses = new ArrayList<>();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getStudentId() {
|
||||
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
|
||||
public boolean addCourse(Course c)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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(Course c)
|
||||
{
|
||||
if (registeredCourses.remove(c))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public void showRegisteredCourses() {
|
||||
// TODO: Print all registered courses
|
||||
public void showRegisteredCourses()
|
||||
{
|
||||
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