Files
WS-02-intro-to-oop/src/CourseRegistrationStarter/Student.java
T
2026-04-20 23:15:32 +03:30

56 lines
1.7 KiB
Java

package CourseRegistrationStarter;
import java.util.ArrayList;
public class Student {
private String name;
private String studentId;
private ArrayList<Course> registeredCourses;
public Student(String name, String studentId) {
this.name = name;
this.studentId = studentId;
this.registeredCourses = new ArrayList<>();
}
public String getName() {
return name;
}
public String getStudentId() {
return studentId;
}
public boolean addCourse(Course c) {
if(registeredCourses.contains(c))
return false;
if((c.getCapacity() > 0)){
this.registeredCourses.add(c);
c.reduceCapacity();
return true;
}
// TODO: Register the student in the course if capacity allows
// Return true if registration was successful, otherwise false
return false;
}
public boolean dropCourse(Course c) {
for(int i = 0; i < this.registeredCourses.size(); i++){
if (this.registeredCourses.get(i) == c){
this.registeredCourses.remove(c);
c.increaseCapacity();
return true;
}
}
// 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
return false;
}
public void showRegisteredCourses() {
for(int i = 0; i < registeredCourses.size(); i++){
System.out.println(this.registeredCourses.get(i).getCourseName() + ", code: " + this.registeredCourses.get(i).getCourseCode());
}
// TODO: Print all registered courses
}
}