Advertisement
Parasect

שדגכשדכשדכ

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