user3075117
user3075117

Reputation: 49

Add method to add an object to the array

Here's example:

 public class Course {
        private String name;
        private Student[] students;
        private int capacity=40;
        private int numberOfStudents;

        public Course(String name){
            this.name=name;
        }

        public Course(String name, int capacity){
            this.name= name;
            this.capacity= capacity;
        }

        public int getNumberOfStudents(){
            return numberOfStudents;
        }

        public String getCourseName(){
            return name;
        }

        public Student[] getStudents(){
            return students;
        }

        public boolean addStudents(Student newStudent){
            if(numberOfStudents < capacity){    
                students[numberOfStudents++] = newStudent;
                return true;
            }
            return false;
        }
    }

I'm trying to add a new student to the Student[] students array. I wrote the code above. In the Student class, every student has a unique id.

The problem is that while I am adding newStudent, I want check if newStudent already exists in the class. To do that I should use id property of students because every student has its own unique id. How can I add it to do if statement?

Upvotes: 0

Views: 4384

Answers (2)

Paul Samsotha
Paul Samsotha

Reputation: 208944

You need to use a loop to loop through the students. Something like this. The loop checks if the students exists. If it does, then the method will return false without adding a student.

 public boolean addStudents(Student newStudent){
     for (Student student : students){
         if (student.getID() == newStudent.getId()){
             return false;
         }
     }
     if(numberOfStudents < capacity){    
         students[numberOfStudents++] = newStudent;
         return true;
     }
     return false;
}

Upvotes: 3

Anthony Accioly
Anthony Accioly

Reputation: 22461

If Student correctly overrides equals (and, as a good practice, hashCode) you can do the following:

public boolean addStudents(Student newStudent){
    if(numberOfStudents < capacity && isNew(newStudent)){    
        students[numberOfStudents++] = newStudent;
        return true;
    }
    return false;
}

public boolean isNew(Student student) {
   for (int i = 0; i < numberOfStudents; i++) {
       if (students[i].equals(student)
           return false;   
   }
   return true;
}

Overridden equals method on student:

public boolean equals(Object obj) {
    if (obj == null)
        return false;
    if (obj == this)
        return true;
    if (!(obj instanceof Student))
        return false;

    Student s = (Student) obj;
    return getID() == s.getID() 
        || (getID() != null && getID.equals(s.getID())) // skip if id is a primitive
}

You can also replace the use of acessors (getID() == s.getID()) calls with the use of properties since your Student class will have access to private properties (id == s.id).


If you have a lot of students per class I would follow @David advice and use a HashMap or similar data structure so that you don't need to loop through all of the class students (O(n)) to find out if you are adding a new student.

Upvotes: 1

Related Questions