Advertisement
Parasect

list2

Oct 20th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. package phonebook;
  2.  
  3. /**
  4. * Document : PhoneBook Created on : 20/10/2014, 13:00:57 Author : Admin
  5. */
  6. public class PhoneBook
  7. {
  8. private Contact[] list;
  9. private int count;
  10. private static final int MAX_CONTACT = 40;
  11.  
  12. public PhoneBook()
  13. {
  14. count = 0;
  15. list = new Contact[MAX_CONTACT];
  16. }
  17.  
  18. public void addContact(String name, String phone)
  19. {
  20. int i = 0;
  21. while(i < count && !name.equals(list[i].getN()))
  22. {
  23. i++;
  24. }
  25. if(i < count)
  26. {
  27. list[i].setP(phone);
  28. }
  29. else
  30. {
  31. if(count == list.length)
  32. {
  33. Contact[] tmp = new Contact[MAX_CONTACT];
  34. for(int j = 0; j < list.length; j++)
  35. {
  36. tmp[j] = list[j];
  37. }
  38. list = tmp;
  39. list[count] = new Contact(name, phone);
  40. count++;
  41. }
  42.  
  43. }
  44. }
  45.  
  46. public void delContact(String name)
  47. {
  48. for(int i = 0; i < count; i++)
  49. {
  50. if(list[i].getN().equals(name))
  51. {
  52.  
  53. for(int j = i; j < count - 1; j++)
  54. {
  55. Contact temp = list[j];
  56. list[j] = list[j + 1];
  57. list[j + 1] = temp;
  58. }
  59. list[count - 1] = null;
  60. count--;
  61. }
  62. }
  63. }
  64.  
  65. public String getPhone(String name)
  66. {
  67. for(int i = 0; i < count; i++)
  68. {
  69. if(list[i].equals(name))
  70. {
  71. return list[i].getP();
  72. }
  73. }
  74. return null;
  75. }
  76.  
  77. public String[] getAllContactsNames()
  78. {
  79. String[] names = new String[count];
  80. for(int i = 0; i < count; i++)
  81. {
  82. names[i] = list[i].getN();
  83. }
  84. return names;
  85. }
  86.  
  87. @Override
  88. public String toString()
  89. {
  90.  
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement