its gonna seem complicated but I want to make a function that removes an object from the ArrayList of an object that is in another ArrayList let me simplify it, there is a class with objects (students) and every student has an ArrayList (studentCourses). I want to make a function that removes a course and when the course is removed it is also removed from the student courses list
public class Student extends User{
ArrayList<Course> StudentcoursesTaken=new ArrayList<>();
public static int studentsNO; //var. to help in assigning an id to every student
protected int NoOfCoursesTaken=StudentcoursesTaken.size();
private int studentId;
private String studentName;
protected static ArrayList<Student> studentList = new ArrayList<>();
that's the course class:
public class Course {
private int courseId;
private String courseName;
private String courseDescription;
private int maxStudents;
private Lecturer tutor; //aggergation relationship
protected ArrayList<Student> course_students = new ArrayList<>();
protected static ArrayList<Course> courseList = new ArrayList<>();
and that's the function that registers the student in the course:
public static void master_register_courses_student(){
if (Course.courseList.isEmpty()){
System.out.println("There are no courses currently to assign a student to\n Try to add a new course");
}else if((Course.courseList.size())>=1) {
System.out.println("choose the course");
Course.displayCourses();
int r=in.nextInt();
if ((Course.courseList.get(r-1)).course_students.size()==(Course.courseList.get(r-1)).getMaxStudents()){
System.out.println("The course is complete \n a new student cannot be added");
}else{
if(Student.studentList.isEmpty()){
System.out.println("There is no student currently to assign a course to\n Try to add a new student");
}else{
System.out.println("choose the student you want to add to the course");
Student.displayStudent();
int n=in.nextInt();
(Course.courseList.get(r-1)).course_students.add(Student.studentList.get(n-1));
(Student.studentList.get(n-1)).StudentcoursesTaken.add(Course.courseList.get(r-1));
}
}
}
}
and that's the remove function:
public static void removeobject(){
System.out.println("Choose what you want to remove.");
System.out.println("1.Courses\t2.Students\t3.Tutors");
int i=in.nextInt();
switch(i){
case 1:
if(Course.courseList.isEmpty()){
System.out.println("Course list is empty\nTry to add new courses");
}else{
System.out.println("Which course would you like to remove?");
Course.displayCourses();
int c=in.nextInt();
if(c>Course.courseList.size()){
System.out.println("invalid choice");
}else{
Course.courseList.remove((c-1));
for(int f=0;f<Student.studentList.size();f++){
(Student.studentList.get(f)).StudentcoursesTaken.removeIf(p->p.getCourseName().equals((Course.courseList.get(c-1)).getCourseName()));
}
System.out.println("A course removed successfully");
}}
break;
the remove function doesn't give me any errors in code but when the user chooses the course to remove it gives me build failure
I'm sorry it seems complicated but I hope anyone has a solution for it. and thanks