I have a Contact class which has name, email and address. I store them inside an ArrayList. Right now I have three different methods that are used for letting the user search a contact by name, email or address. Each one does exactly the same as the others except for the comparison statement, one has name.equals(contact.getName()), another email.equals(contact.getEmail()) and the last one has address.equals(contact.getAddress()). I am aware of the DRY principle and I think it can be applied here, is there a way to avoid repeating myself in this case?
public void searchName(String name)
{
for(int i = 0; i < contacts.size(); i++)
{
Contact contact = contacts.get(i);
if(name.equals(contact.getName())
{
printContactInfo(contact);
}
}
}
The other two methods are exactly the same but instead of using name they use email or address.