Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c13e3a8327 | ||
|
|
012c246399 | ||
|
|
67f45fe78d |
@@ -5,7 +5,8 @@ public class Course {
|
|||||||
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;
|
||||||
@@ -23,7 +24,8 @@ public class Course {
|
|||||||
return capacity;
|
return capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean reduceCapacity() {
|
public boolean reduceCapacity()
|
||||||
|
{
|
||||||
if (capacity > 0) {
|
if (capacity > 0) {
|
||||||
capacity--;
|
capacity--;
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -1,14 +1,40 @@
|
|||||||
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
|
// 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
|
// 2. Create 1 student
|
||||||
|
Student s1 = new Student("Romina", "40123456");
|
||||||
|
|
||||||
// 3. Create a registration system
|
// 3. Create a registration system
|
||||||
|
RegistrationSystem regSys = new RegistrationSystem();
|
||||||
|
|
||||||
// 4. Add courses to the system
|
// 4. Add courses to the system
|
||||||
|
regSys.addCourseToSystem(c1);
|
||||||
|
regSys.addCourseToSystem(c2);
|
||||||
|
regSys.addCourseToSystem(c3);
|
||||||
|
|
||||||
|
regSys.showAllCourses();
|
||||||
|
|
||||||
// 5. Register the student in some courses
|
// 5. Register the student in some courses
|
||||||
|
s1.addCourse(c1);
|
||||||
|
s1.addCourse(c2);
|
||||||
|
s1.addCourse(c3);
|
||||||
|
|
||||||
|
s1.showRegisteredCourses();
|
||||||
|
|
||||||
// 6. Drop one course
|
// 6. Drop one course
|
||||||
|
s1.dropCourse(c2);
|
||||||
|
|
||||||
// 7. Print the final registered course list
|
// 7. Print the final registered course list
|
||||||
|
s1.showRegisteredCourses();
|
||||||
|
|
||||||
|
regSys.showAllCourses();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,24 +1,53 @@
|
|||||||
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> 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
|
{
|
||||||
|
// 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) {
|
public Course findCourseByCode(String code)
|
||||||
// TODO: Search for a course by course code
|
{
|
||||||
// Return the matching course if found, otherwise return null
|
// 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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showAllCourses() {
|
public void showAllCourses()
|
||||||
// TODO: Print all courses in the system
|
{
|
||||||
|
// 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;
|
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) {
|
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<>();
|
||||||
@@ -20,20 +22,55 @@ public class Student {
|
|||||||
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
|
// Register the student in the course if capacity allows
|
||||||
return false;
|
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) {
|
public boolean dropCourse(Course c)
|
||||||
// TODO: Remove the course from the student's list
|
{
|
||||||
|
// Remove the course from the student's list
|
||||||
// Restore the course capacity if removal was successful
|
// Restore the course capacity if removal was successful
|
||||||
// Return true if the course was dropped, otherwise false
|
if (registeredCourses.contains(c))
|
||||||
return false;
|
{
|
||||||
|
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() {
|
public void showRegisteredCourses()
|
||||||
// TODO: Print all registered courses
|
{
|
||||||
|
// 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