Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7.  
  8. public class topFive extends PhoneBook
  9. {
  10. /**
  11. * Logs all the numbers, contacts, favorite 5, etc
  12. * @throws IOException In case no file is found for the favorite 5
  13. */
  14. public static void FavoriteFive() throws IOException
  15. {
  16. ArrayList<String> favContact = new ArrayList<String>();
  17. ArrayList<String> favNames = new ArrayList<String>();
  18. ArrayList<String> favNums = new ArrayList<String>();
  19. ArrayList<String> favEmail = new ArrayList<String>();
  20.  
  21. final int fWidth = 3524;
  22. final int fHeight = 2440;
  23.  
  24. FavoriteContactFrame contact4 = new FavoriteContactFrame("C:\\Users\\sword\\OneDrive\\Pictures\\Screenshots\\DatBoi.jpg", fWidth, fHeight, "Dat Boi", "(777) 777-7777"
  25. + "", "datboi@csulb.edu", "");
  26. FavoriteContactFrame contact5 = new FavoriteContactFrame("C:\\Users\\sword\\OneDrive\\Pictures\\Screenshots\\spongebobicon.jpg", fWidth, fHeight, "Spongebob", "(562) 584-5488"
  27. + "", "spongebobsquarepants@csulb.edu", "");
  28. FavoriteContactFrame contact6 = new FavoriteContactFrame("C:\\Users\\sword\\OneDrive\\Pictures\\Screenshots\\squidwardmeme.jpg", fWidth, fHeight, "Squidward", "(562) 714-8792"
  29. + "", "squidwardtentacles@ucrb.edu", "");
  30.  
  31. favContact.add("Dat Boi 777-777-7777 datboi@csulb.edu");
  32. favContact.add("Spongebob 562-584-5488 spongebobsquarepants@csulb.edu");
  33. favContact.add("Squidward 562-714-8792 squidwardtentacles@ucrb.edu");
  34.  
  35. String menu = "1. Add a contact.\n2. Remove a contact.\n3. Rearrange a contact.\n4. Display the contacts.";
  36. int selection = 0;
  37.  
  38. do
  39. {
  40. System.out.println("Welcome to your favorite contacts! Please select one of the following options: \n" + menu);
  41. selection = Integer.parseInt(user.nextLine().trim()); //Accepting user selection
  42. /**
  43. * If selection = 1, adds a favorite contact
  44. */
  45. if(selection == 1)
  46. {
  47. while(true)
  48. {
  49. System.out.print("Please enter your name, phone number and email or enter Done to stop: ");
  50. favContact.add(user.nextLine());
  51. if(favContact.contains("Done"))
  52. {
  53. break;
  54. }
  55. }
  56.  
  57. }
  58. //------------------------------------------------------------------------------------------------------------------------
  59. else if(selection == 2)
  60. /**
  61. * Removes the selected contact via name
  62. * Else, declare that the contact does not exist
  63. */
  64. {
  65. //Remove the contact
  66. //Dat Boi 777-777-7777 datboi@csulb.edu
  67. //Squidward 562-714-8792 squidwardtentacles@ucrb.edu
  68. System.out.print("Enter the name of the contact you want to remove: ");
  69. String remove = user.nextLine();
  70. if(favContact.contains(remove))
  71. {
  72. int removeIndex = favContact.indexOf(remove);
  73. favContact.remove(removeIndex);
  74. for(String delete : favContact)
  75. {
  76. System.out.println(delete);
  77. }
  78. }
  79. else
  80. {
  81. System.out.println("This contact does not exist.");
  82. }
  83. }
  84. //------------------------------------------------------------------------------------------------------------------------
  85. else if(selection == 3)
  86. /**
  87. * Rearrange the order of the contacts
  88. */
  89. {
  90. //Rearrange a contact
  91. //Spongebob 562-584-5488 spongebobsquarepants@csulb.edu
  92. System.out.print("Enter the name of the contact you want to rearrange: ");
  93. String firstSwap = user.nextLine();
  94. if(favContact.contains(firstSwap))
  95. {
  96. int index = favContact.indexOf(firstSwap);
  97. System.out.print("Enter the name of the contact you want to switch it with: ");
  98. String secondSwap = user.nextLine();
  99. if(favContact.contains(secondSwap))
  100. {
  101. int indexTwo = favContact.indexOf(secondSwap);
  102. Collections.swap(favContact, index, indexTwo);
  103. for(String output : favContact)
  104. {
  105. System.out.println(output);
  106. }
  107. }
  108. }
  109. }
  110. //------------------------------------------------------------------------------------------------------------------------
  111. else if(selection == 4)
  112. {
  113. while(true)
  114. {
  115. System.out.print("Enter the name of your favorite contact or Done to stop: ");
  116. favNames.add(user.nextLine());
  117. if(favNames.contains("Done"))
  118. {
  119. FavoriteContactFrame contact2 = new FavoriteContactFrame("C:\\Users\\sword\\OneDrive\\Pictures\\Screenshots\\Airalin Wallpaper.png", fWidth, fHeight, favNames.get(0), favNums.get(0)
  120. + "", favEmail.get(0), "");
  121. contact2.displayContactFrame();
  122.  
  123. FavoriteContactFrame contact3 = new FavoriteContactFrame("C:\\Users\\sword\\OneDrive\\Pictures\\Screenshots\\Knife_Spooky.png", fWidth, fHeight, favNames.get(1), favNums.get(1)
  124. + "", favEmail.get(1), "");
  125. contact3.displayContactFrame();
  126. contact4.displayContactFrame();
  127. contact5.displayContactFrame();
  128. contact6.displayContactFrame();
  129. break;
  130. }
  131. System.out.print("Now enter their number: ");
  132. favNums.add(user.nextLine());
  133.  
  134. System.out.print("Finally enter their email: ");
  135. favEmail.add(user.nextLine());
  136. }
  137. }
  138. //------------------------------------------------------------------------------------------------------------------------
  139. else if(selection < 1 || selection > 5)
  140. /**
  141. * A catch in case an invalid selection is made
  142. */
  143. {
  144. System.out.print("Invalid selection please try again: ");
  145. selection = Integer.parseInt(user.nextLine().trim());
  146. }
  147. /**
  148. * Program is gone once it hits 5
  149. */
  150. }while(selection != 5);
  151. /**
  152. * Saves data once this is done
  153. */
  154. File callLog = new File("C:\\Users\\Henry\\Desktop\\JavaFiles\\favoriteFive.txt");
  155.  
  156. //Source : https://stackoverflow.com/questions/9961292/write-to-text-file-without-overwriting-in-java
  157. PrintWriter fileOUT = new PrintWriter(new FileWriter(callLog, true));
  158. for(String print : name){
  159. fileOUT.println(print);
  160. }
  161.  
  162. fileOUT.close();
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement