Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class AddressBook
- {
- private ArrayList<Contact> list;
- public AddressBook() { list = new ArrayList<Contact>(); }
- public void addContact(String first, String last, String email)
- {
- Contact newContact = new Contact(first,last,email);
- for( Contact contact : list)
- {
- if(((newContact.getFirst()) != (contact.getFirst())) || ((newContact.getLast()) != (contact.getLast())) || ((newContact.email()) != (contact.email())))
- {
- if((newContact.getLast()).compareTo((contact.getLast())) < 0)
- {
- list.add(newContact);
- }
- }
- }
- }
- public void removeContact(String first, String last)
- {
- for(Contact contact : list)
- {
- if(((contact.getLast()).equals(last)) && ((contact.getFirst()).equals(first)))
- {
- list.remove(contact);
- }
- }
- }
- public void mergeBooks(AddressBook otherBook)
- {
- for(int i = 0; i < otherBook.list.size(); i++)
- {
- String first = otherBook.list.get(i).getFirst();
- String last = otherBook.list.get(i).getLast();
- String email = otherBook.list.get(i).email();
- addContact(first, last, email);
- }
- }
- public String toString()
- {
- String temp = "";
- for(Contact contact : list)
- {
- temp += contact.getFirst() + " , " + contact.getLast() + " , " + contact.email() + "\n";
- }
- return temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment