Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. public class PhoneBook
  2. {
  3. Node<Phone> first;
  4. Node<Phone> last;
  5.  
  6. public PhoneBook() // no costumer
  7. {
  8. first = null;
  9. last = null ;
  10. }
  11.  
  12. public PhoneBook(Phone a) // Bank with only one consumer
  13. {
  14. Node <Phone> n = new Node<Phone> (a);
  15. first = n;
  16. last = first;
  17. }
  18.  
  19. public void addContact(String name, String num)
  20. {
  21. Phone a = new Phone (name, num);
  22. Node<Phone> abc = new Node <Phone> (a);
  23. Node<Phone> p = first;
  24.  
  25. //if there is something in list add to the last
  26. if (p != null)
  27. {
  28. Node <Phone> last = first;
  29. while (last.getNext() != null)
  30. {
  31. last = last.getNext();
  32. }
  33. last.setNext(abc);
  34. }
  35. //if there isnt something in list add to the beginning
  36. else
  37. {
  38. abc.setNext(first);
  39. first = abc;
  40. }
  41. }
  42.  
  43. public void removeContact(Phone a)
  44. {
  45. Node<Phone> p = first;
  46. Node<Phone> save = first;
  47. int i = 0;
  48.  
  49. while (!(p.getValue().getName().equals(a.getName())))
  50. {
  51. p = p.getNext();
  52. }
  53.  
  54. p = first;
  55.  
  56. if(i==0)
  57. {
  58. first = p.getNext();
  59.  
  60. while(p != null)
  61. {
  62. p = p.getNext();
  63. }
  64. }
  65. else
  66. {
  67. while(save.getNext() != p)
  68. {
  69. save = save.getNext();
  70. }
  71. p = null;
  72. save.setNext(p);
  73. }
  74. }
  75.  
  76. public String [] getAllContactsNames()
  77. {
  78. Node<Phone> p = first;
  79. int i = 0;
  80.  
  81. while (p != null)
  82. {
  83. i++;
  84. p = p.getNext();
  85. }
  86.  
  87. String [] s = new String [i];
  88. p = first;
  89.  
  90. for(int j = 0; j < i; j++)
  91. {
  92. s[j] = p.getValue().getName();
  93. }
  94.  
  95. return s;
  96. }
  97.  
  98. void setPhone (String name, String phone)
  99. {
  100. Node <Phone> p = first;
  101.  
  102. while (!(p.getValue().getName().equals(name)))
  103. {
  104. p = p.getNext();
  105. }
  106. p.getValue().setNum(phone);
  107. }
  108.  
  109. public String toString()
  110. {
  111. String s = "";
  112. Node<Phone> p = first;
  113.  
  114. while (p!=null)
  115. {
  116. s += "\n" + p.getValue().toString();
  117. p = p.getNext();
  118. }
  119.  
  120. return s;
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement