-1

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

2 Answers2

0

At which criteria you delete a course? In general you can search through the arraylist with a for loop:

ArrayList<Course> StudentcoursesTaken=new ArrayList<>();

public void removeObject(Course course){
Course needstobedeleted;
for(int i=0;i<StudentcoursesTaken.size();i++){

if(StudentcoursesTaken.get(i).getCourseName()==course.getCourseName()){
needstobedeleted = StudentcoursesTaken.get(i);
//this is other way
String tobedeleted = StudentcoursesTaken.get(i).getCourseName();
StudentcoursesTaken.remove(i);
}

you could save the object--> needstobedeleted

for(int j=0;j<Student.studentlist.size();j++){
if(Student.studentlist.get(j).getCourseName()==needstobedeleted.getCourseName()||tobedeleted){
Student.studentlist.remove(j);
}

}

Did I understand your question right? Please tell me But if i did this could help

0

Here is how I would do it. I am presuming you would have a List<Student> and a List<Course> accessible. You may have to modify this to fit your needs. I would also recommend you remove the Course list from the Course class and and the Student list from the Student class. You can still make them accessible as instance fields (even as static fields) but it really makes no sense to me to have them where they are.

  • first check to see if the course is being taught
  • if so, clear the students taking that course (no longer available)
  • also remove the course from the Student taking the course
public static boolean removeCourse(List<Student> students, List<Course> courses, Course course) {
    if (courses.contains(course)) {
        course.course_students.clear();  // course dropped so remove all students 
        students.removeIf(student->student.getCourse().equals(course));
    }
}
  • You will need to ensure equals is overriden correctly based on your criteria for equality. (you may want to override hashCode too for completeness.
  • your classes should have getters and setters implemented.
WJS
  • 36,363
  • 4
  • 24
  • 39
  • I think I am gonna assign the course in studentcoursetaken as a string to be easier and I don't get why should I remove the course and student list they contain all the new students and courses created could you tell how it's possible "still make them accessible as instance fields (even as static fields)" sorry if I didn't get it I'm still a kinda beginner and thanks I appreciate it – Ahmedzaghloul27 Jun 05 '22 at 20:12
  • Typically a class has attributes of its type. So a Student class might have the name of the student, the age, the home address, the GPA, and a list of courses that the student is taking. But why would a student carry around a list of other Students? Even if that list is static and shared by all the other students? I am not saying you can't make it work. It just doesn't make any sense (at least to me). For example, one might have a School class. The class would contain the name, address, list of students, list of courses offered, list of the teachers,etc. – WJS Jun 05 '22 at 20:51
  • So, you're telling me to make a list type string not a course or student that save a list of students or courses as a string with out showing all the information of students or course? Did i get or not? If not then the student list or course list there main purpose is to give a list of students and a list of courses in the school thats it all @WJS – Ahmedzaghloul27 Jun 06 '22 at 01:08