Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1.  
  2. import bestfriendsarraylist.BestFriend;
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class BestFriendArrayListHelper {
  7.  
  8. // array of bestfriends
  9. BestFriend[] myBFFs;
  10. private int count;// current count of best friends
  11. Scanner keyboard = new Scanner(System.in);
  12. String firstName;
  13. String lastName;
  14. String nickName;
  15. String cellPhone;
  16. String email;
  17.  
  18. public BestFriendArrayListHelper() //instantiates myBFFs and count. Scanner object is created too.
  19. {
  20.  
  21. myBFFs = new BestFriend[10];
  22. count = 0;
  23. keyboard = new Scanner(System.in);
  24. Scanner keyboard = new Scanner(System.in);
  25.  
  26. }
  27.  
  28. public void addAFriend() //adds friend to array
  29. {
  30.  
  31. System.out.println("Enter the First Name of ur friend");
  32.  
  33. firstName = keyboard.nextLine();
  34.  
  35. System.out.println("Enter the Last Name of ur friend");
  36.  
  37. lastName = keyboard.nextLine();
  38.  
  39. System.out.println("Enter the Nick Name of ur friend");
  40.  
  41. nickName = keyboard.nextLine();
  42.  
  43. System.out.println("Enter the Phone no of ur friend");
  44.  
  45. cellPhone = keyboard.nextLine();
  46.  
  47. System.out.println("Enter the email of ur friend");
  48.  
  49. email = keyboard.nextLine();
  50.  
  51. BestFriend bff = new BestFriend(firstName, lastName, nickName, cellPhone, email);
  52.  
  53. if (count == myBFFs.length) { //array doubles if full
  54.  
  55. expandArray();
  56.  
  57. }
  58.  
  59. // adding new best friend to the array, updating count of friends.
  60. myBFFs[count] = bff;
  61. count++;
  62. System.out.println("Friend has been successfully added!");
  63.  
  64. }
  65.  
  66. private void shrinkArray(int index) //method to shrink the array by removing element at given index.
  67. {
  68.  
  69. // shifting remaining elements to the left
  70. for (int i = index; i < count - 1; i++) {
  71.  
  72. myBFFs[i] = myBFFs[i + 1];
  73.  
  74. }
  75.  
  76. // decrementing count
  77. count--;
  78.  
  79. }
  80.  
  81. private void expandArray() //expands erray
  82. {
  83.  
  84. BestFriend newArray[] = new BestFriend[myBFFs.length * 2];
  85. for (int i = 0; i < myBFFs.length; i++) //copies items from old array to new one
  86. {
  87.  
  88. newArray[i] = myBFFs[i];
  89.  
  90. }
  91. myBFFs = newArray;
  92.  
  93. }
  94.  
  95. public void displayFriend() {
  96. System.out.println("\ta. Display a specific friend: Type S ");
  97. System.out.println("\tb. Display all friends: Type all");
  98. String choice = keyboard.nextLine();
  99.  
  100. if (choice.equalsIgnoreCase("all")) {
  101.  
  102. for (int i = 0; i < count; i++) {
  103.  
  104. System.out.println(myBFFs[i]);
  105.  
  106. }
  107.  
  108. } else {
  109.  
  110. int i = searchAFriend();
  111.  
  112. if (i == -1) {
  113. System.out.println("Not Found");
  114. } else {
  115. System.out.println(myBFFs[i]);
  116. }
  117.  
  118. }
  119.  
  120. }
  121.  
  122. public int searchAFriend() {
  123. BestFriend searchObj = new BestFriend(firstName, lastName, nickName);
  124. System.out.println("\nWhat is their first name?");
  125.  
  126. firstName = keyboard.nextLine();
  127.  
  128. System.out.println("\nWhat is their last name?");
  129.  
  130. lastName = keyboard.nextLine();
  131.  
  132. System.out.println("\nWhat is their nick name?");
  133.  
  134. nickName = keyboard.nextLine();
  135.  
  136. for (int i = 0; i < count; i++) {
  137.  
  138. if (myBFFs[i].equals(searchObj)) {
  139. return i;
  140. }
  141. else{
  142. System.out.println("User has not been found!");
  143.  
  144. }
  145. } return -1;
  146. }
  147.  
  148.  
  149.  
  150. public void deleteFriend() //deletes friend from array.
  151. {
  152. String Choice;
  153. int i = searchAFriend();
  154.  
  155. if (i != -1) {
  156.  
  157. System.out.println(myBFFs[i]);
  158.  
  159. System.out.println("Are you sure you want to remove this friend? :");
  160.  
  161. Choice = keyboard.nextLine();
  162.  
  163. if (Choice.equalsIgnoreCase("yes")) {
  164.  
  165. myBFFs[i] = null;
  166. shrinkArray(i);
  167. System.out.println("Friend has been sucessfully removed");
  168.  
  169. }
  170.  
  171. } else {
  172. System.out.println("Sorry, your friend cannot be found");
  173. }
  174.  
  175. }
  176.  
  177. public void changeFriend() {
  178.  
  179.  
  180. int i = searchAFriend();
  181.  
  182. if (i == -1) {
  183. System.out.println("Not Found");
  184. } else {
  185.  
  186. System.out.println("\nWhat is the new first Name of your friend?");
  187.  
  188. firstName = keyboard.nextLine();
  189.  
  190. System.out.println("\nWhat is the new last name of your friend?");
  191.  
  192. lastName = keyboard.nextLine();
  193.  
  194. System.out.println("\nWhat is the new nick name of your friend?");
  195.  
  196. nickName = keyboard.nextLine();
  197.  
  198. System.out.println("\nWhat is the new cell phone of your friend?");
  199.  
  200. cellPhone = keyboard.nextLine();
  201.  
  202. System.out.println("\nWhat is the new email of your friend?");;
  203.  
  204. email = keyboard.nextLine();
  205.  
  206. BestFriend bff = myBFFs[i];
  207.  
  208. bff.setCellPhone(cellPhone);
  209.  
  210. bff.setEmail(email);
  211.  
  212. bff.setFirstName(firstName);
  213.  
  214. bff.setLastName(lastName);
  215.  
  216. bff.setNickName(nickName);
  217.  
  218.  
  219. }
  220.  
  221. }
  222.  
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement