Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c1226b9cc | ||
|
|
c13e3a8327 | ||
|
|
012c246399 | ||
|
|
67f45fe78d |
@@ -5,7 +5,8 @@ public class Course {
|
||||
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;
|
||||
@@ -23,7 +24,8 @@ public class Course {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
public boolean reduceCapacity() {
|
||||
public boolean reduceCapacity()
|
||||
{
|
||||
if (capacity > 0) {
|
||||
capacity--;
|
||||
return true;
|
||||
|
||||
@@ -1,14 +1,40 @@
|
||||
package CourseRegistrationStarter;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// TODO:
|
||||
public class Main
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// 1. Create at least 3 courses
|
||||
Course c1 = new Course("Advanced Programming", "CS101", 2);
|
||||
Course c2 = new Course("Mathematics", "MATH201", 1);
|
||||
Course c3 = new Course("Foundations of Mathematical Sciences","MATH301", 3);
|
||||
|
||||
// 2. Create 1 student
|
||||
Student s1 = new Student("Romina", "40123456");
|
||||
|
||||
// 3. Create a registration system
|
||||
RegistrationSystem regSys = new RegistrationSystem();
|
||||
|
||||
// 4. Add courses to the system
|
||||
regSys.addCourseToSystem(c1);
|
||||
regSys.addCourseToSystem(c2);
|
||||
regSys.addCourseToSystem(c3);
|
||||
|
||||
regSys.showAllCourses();
|
||||
|
||||
// 5. Register the student in some courses
|
||||
s1.addCourse(c1);
|
||||
s1.addCourse(c2);
|
||||
s1.addCourse(c3);
|
||||
|
||||
s1.showRegisteredCourses();
|
||||
|
||||
// 6. Drop one course
|
||||
s1.dropCourse(c2);
|
||||
|
||||
// 7. Print the final registered course list
|
||||
s1.showRegisteredCourses();
|
||||
|
||||
regSys.showAllCourses();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,53 @@
|
||||
package CourseRegistrationStarter;
|
||||
import java.util.ArrayList;
|
||||
|
||||
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)
|
||||
{
|
||||
// Add the course to the system
|
||||
courses.add(c);
|
||||
System.out.println("Course " + c.getCourseName() + " added to the system."); // Feedback message
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
// Search for a course by course code
|
||||
for (Course c : courses)
|
||||
{
|
||||
if (c.getCourseCode().equals(code))
|
||||
{
|
||||
// Return the matching course if found
|
||||
return c;
|
||||
}
|
||||
}
|
||||
// otherwise return null
|
||||
System.out.println("Course with code " + code + " not found."); // Feedback message
|
||||
return null;
|
||||
}
|
||||
|
||||
public void showAllCourses() {
|
||||
// TODO: Print all courses in the system
|
||||
public void showAllCourses()
|
||||
{
|
||||
// Print all courses in the system
|
||||
System.out.println("\n--- All Courses in the System ---");
|
||||
if (courses.isEmpty())
|
||||
{
|
||||
System.out.println("No courses available in the system.");
|
||||
}
|
||||
else
|
||||
{
|
||||
for (Course c : courses)
|
||||
{
|
||||
System.out.println("Name: " + c.getCourseName() + ", Code: " + c.getCourseCode() + ", Capacity: " + c.getCapacity());
|
||||
}
|
||||
}
|
||||
System.out.println("-------------------------------\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
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) {
|
||||
public Student(String name, String studentId)
|
||||
{
|
||||
this.name = name;
|
||||
this.studentId = studentId;
|
||||
this.registeredCourses = new ArrayList<>();
|
||||
@@ -20,20 +22,55 @@ public class Student {
|
||||
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 addCourse(Course c)
|
||||
{
|
||||
// Register the student in the course if capacity allows
|
||||
if (c.reduceCapacity())
|
||||
{ // Reduce capacity, returns true if successful (capacity > 0)
|
||||
registeredCourses.add(c);
|
||||
System.out.println(name + " successfully registered for course: " + c.getCourseName());
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println(name + " could not register for course " + c.getCourseName() + ". Course is full.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean dropCourse(Course c) {
|
||||
// TODO: Remove the course from the student's list
|
||||
public boolean dropCourse(Course c)
|
||||
{
|
||||
// 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;
|
||||
if (registeredCourses.contains(c))
|
||||
{
|
||||
registeredCourses.remove(c);
|
||||
c.increaseCapacity(); // Restore capacity
|
||||
System.out.println(name + " successfully dropped course: " + c.getCourseName());
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println(name + " is not registered for course: " + c.getCourseName() + ", cannot drop.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void showRegisteredCourses() {
|
||||
// TODO: Print all registered courses
|
||||
public void showRegisteredCourses()
|
||||
{
|
||||
// Print all registered courses
|
||||
System.out.println("\n--- Courses Registered by " + name + " ---");
|
||||
if (registeredCourses.isEmpty())
|
||||
{
|
||||
System.out.println(name + " is not registered for any courses.");
|
||||
}
|
||||
else
|
||||
{
|
||||
for (Course c : registeredCourses)
|
||||
{
|
||||
System.out.println("Name: " + c.getCourseName() + ", Code: " + c.getCourseCode());
|
||||
}
|
||||
}
|
||||
System.out.println("------------------------------------\n");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user