Advertisement
jakemalis

Untitled

Mar 31st, 2020
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class AddressBook
  4. {
  5. private ArrayList<Contact> list;
  6. public AddressBook() { list = new ArrayList<Contact>(); }
  7.  
  8. public void addContact(String first, String last, String email)
  9. {
  10. String newContact = (last+first).toUpperCase();
  11. boolean contactInserted = false;
  12. if (list.size() == 0) {
  13. list.add(new Contact(first, last, email));
  14. contactInserted = true;
  15. return;
  16. }
  17. for(int index = 0; index < list.size(); index++)
  18. {
  19. if ((checkExisting(first, last)) == false) {
  20. Contact person = list.get(index);
  21. String listContact = (person.getLast() + person.getFirst()).toUpperCase();
  22. if((newContact.compareTo(listContact)) < 0)
  23. {
  24. list.add(index, (new Contact(first, last, email)));
  25. contactInserted = true;
  26. break;
  27. }
  28. }
  29. else if ((checkExisting(first, last)) == true) { contactInserted = true; }
  30. }
  31. if (contactInserted == false) { list.add(new Contact(first, last, email)); }
  32. }
  33.  
  34. private boolean checkExisting(String first, String last)
  35. {
  36. boolean contactExists = false;
  37. String newContact = (last+first).toUpperCase();
  38. if (list.size() == 0) { return contactExists; }
  39. for(Contact contact : list)
  40. {
  41. String listContact = (contact.getLast() + contact.getFirst()).toUpperCase();
  42. if(newContact.equals(listContact)) {
  43. contactExists = true;
  44. }
  45. }
  46. return contactExists;
  47. }
  48.  
  49. public void removeContact(String first, String last)
  50. {
  51. String newContact = (last+first).toUpperCase();
  52. for(int i = 0; i < list.size(); i++)
  53. {
  54. Contact person = list.get(i);
  55. String listContact = (person.getLast() + person.getFirst()).toUpperCase();
  56. if(newContact.equals(listContact))
  57. {
  58. list.remove(person);
  59. }
  60. }
  61. }
  62.  
  63. public void mergeBooks(AddressBook otherBook)
  64. {
  65. for(int i = 0; i < otherBook.list.size(); i++)
  66. {
  67. String first = otherBook.list.get(i).getFirst();
  68. String last = otherBook.list.get(i).getLast();
  69. String email = otherBook.list.get(i).email();
  70. addContact(first, last, email);
  71. }
  72. }
  73.  
  74. public String toString()
  75. {
  76. String temp = "";
  77. for(Contact contact : list) { temp += contact.getFirst() + " " + contact.getLast() + " " + contact.email() + "\n"; }
  78. return temp;
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement