implement triangle method
This commit is contained in:
@@ -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