workshop 2

This commit is contained in:
Arefe Talebi
2026-04-21 23:02:34 +03:30
parent 8ddd529c85
commit 48ebb71955
3 changed files with 65 additions and 11 deletions
+35 -3
View File
@@ -5,6 +5,7 @@ public class Student {
private String name;
private String studentId;
private ArrayList<Course> registeredCourses;
private int maxCourses = 3;
public Student(String name, String studentId) {
this.name = name;
@@ -21,19 +22,50 @@ public class Student {
}
public boolean addCourse(Course c) {
//bonus1:جلوگیری از ثبت نام تکراری
for (Course course : registeredCourses) {
if (course.getCourseCode().equals(c.getCourseCode())) {
return false;
}
}
//bonus2:محدودیت برای تکرار
if (registeredCourses.size() >= maxCourses) {
return false;
}
// TODO: Register the student in the course if capacity allows
// Return true if registration was successful, otherwise false
if (c.getCapacity()>0){
registeredCourses.add(c);
c.reduceCapacity();
return true;
}
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
if (registeredCourses.contains(c)){
registeredCourses.remove(c);
c.increaseCapacity();
return true;
}
return false;
}
public void showRegisteredCourses() {
// TODO: Print all registered courses
System.out.println("Registered courses for"+name+":");
for (Course c :registeredCourses){
System.out.println(c.getCourseName()+"-"+ c.getCourseCode());
}
}
//bonus3
public Course findCourseByName(String name){
for (Course c: registeredCourses){
if (c.getCourseName().equalsIgnoreCase(name)){
return c;
}
}
return null;
}
}