I need to add Person p to the contacts ArrayList unless it contains the same surname and first name.
In that case I need to return an error message "could not add person". Here is my current code:
public class AddressBook {
private ArrayList<Person> contacts;
public AddressBook(){
this.contacts = new ArrayList<Person>();
}
public void addPerson(Person p) {
for (int i = 0; i < contacts.size(); i++) {
if(contacts.get(i).getfirstName().equals(p.firstName)){
System.out.printf("could not add person");
}
}
this.contacts.add(p);
}
I am having trouble figuring out how I should compare the strings about to be added via the addPerson()
method with the existing strings contained within the Person object already contained within the list.