Advertisement
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)
- {
- String newContact = (last+first).toUpperCase();
- boolean contactInserted = false;
- if (list.size() == 0) {
- list.add(new Contact(first, last, email));
- contactInserted = true;
- return;
- }
- for(int index = 0; index < list.size(); index++)
- {
- if ((checkExisting(first, last)) == false) {
- Contact person = list.get(index);
- String listContact = (person.getLast() + person.getFirst()).toUpperCase();
- if((newContact.compareTo(listContact)) < 0)
- {
- list.add(index, (new Contact(first, last, email)));
- contactInserted = true;
- break;
- }
- }
- else if ((checkExisting(first, last)) == true) { contactInserted = true; }
- }
- if (contactInserted == false) { list.add(new Contact(first, last, email)); }
- }
- private boolean checkExisting(String first, String last)
- {
- boolean contactExists = false;
- String newContact = (last+first).toUpperCase();
- if (list.size() == 0) { return contactExists; }
- for(Contact contact : list)
- {
- String listContact = (contact.getLast() + contact.getFirst()).toUpperCase();
- if(newContact.equals(listContact)) {
- contactExists = true;
- }
- }
- return contactExists;
- }
- public void removeContact(String first, String last)
- {
- String newContact = (last+first).toUpperCase();
- for(int i = 0; i < list.size(); i++)
- {
- Contact person = list.get(i);
- String listContact = (person.getLast() + person.getFirst()).toUpperCase();
- if(newContact.equals(listContact))
- {
- list.remove(person);
- }
- }
- }
- 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
Advertisement