jakemalis

Untitled

Mar 29th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. import java.util.ArrayList;
  2. public class AddressBook
  3. {
  4. private ArrayList<Contact> list;
  5. public AddressBook() { list = new ArrayList<Contact>(); }
  6.  
  7.  
  8. public void addContact(String first, String last, String email)
  9. {
  10. Contact newContact = new Contact(first,last,email);
  11. for( Contact contact : list)
  12. {
  13. if(((newContact.getFirst()) != (contact.getFirst())) || ((newContact.getLast()) != (contact.getLast())) || ((newContact.email()) != (contact.email())))
  14. {
  15. if((newContact.getLast()).compareTo((contact.getLast())) < 0)
  16. {
  17. list.add(newContact);
  18. }
  19. }
  20. }
  21. }
  22.  
  23. public void removeContact(String first, String last)
  24. {
  25. for(Contact contact : list)
  26. {
  27. if(((contact.getLast()).equals(last)) && ((contact.getFirst()).equals(first)))
  28. {
  29. list.remove(contact);
  30. }
  31. }
  32. }
  33. public void mergeBooks(AddressBook otherBook)
  34. {
  35. for(int i = 0; i < otherBook.list.size(); i++)
  36. {
  37. String first = otherBook.list.get(i).getFirst();
  38. String last = otherBook.list.get(i).getLast();
  39. String email = otherBook.list.get(i).email();
  40. addContact(first, last, email);
  41. }
  42. }
  43. public String toString()
  44. {
  45. String temp = "";
  46. for(Contact contact : list)
  47. {
  48. temp += contact.getFirst() + " , " + contact.getLast() + " , " + contact.email() + "\n";
  49. }
  50. return temp;
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment