Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Scanner;
  4. import java.util.Comparator;
  5. import java.io.File;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.time.LocalDateTime;
  10.  
  11. /**
  12. * Phonebook extends the Hub
  13. * This will be the phonebook that can add, edit, display, and delete contacts.
  14. */
  15. public class PhoneBook extends Hub
  16. {
  17.  
  18. static Scanner user = new Scanner(System.in);
  19.  
  20. static ArrayList<String> name = new ArrayList<String>();
  21. // You should eliminate the parallel array lists and instead create a class called Contact or Entry
  22. // You can then create/manipulate an array list of Contact objects (private instance variable not a static one) instead
  23.  
  24. public static void PhoneBookMenu() {
  25. /**
  26. * This is the menu we can select options from.
  27. */
  28. String menu = "1. Add a contact.\n2. Edit a contact.\n3. Display contents in alphabetical order.\n4. Terminate.";
  29. int selection = 0;
  30.  
  31. do{
  32. /**
  33. * Looping while selection is not 4
  34. */
  35. System.out.println("Welcome to your phone book! Select an item from the following options: \n" + menu);
  36. selection = Integer.parseInt(user.nextLine().trim()); //Accepting user selection
  37.  
  38. if(selection == 1)
  39. {
  40. while(true)
  41. {
  42. /**
  43. * Input names and numbers to add
  44. */
  45. System.out.print("Please enter your name and phone number or enter Done to stop: ");
  46. name.add(user.nextLine());
  47. if(name.contains("Done"))
  48. {
  49. break;
  50. //name.remove("Done");
  51. //How to remove this?
  52. }
  53. }
  54.  
  55. }
  56. else if(selection == 2)
  57. {
  58. //Edit a contact
  59. System.out.print("Enter the name and phone number of the contact you want to change: ");
  60. String input = user.nextLine();
  61. if(name.contains(input))
  62. /**
  63. * Edits the input by removing current one and redoing it
  64. */
  65. {
  66. int index = name.indexOf(input);
  67. System.out.print("What would you like to change it to? ");
  68. String newInput = user.nextLine().trim();
  69. name.set(index, newInput);
  70. }
  71. }
  72. else if(selection == 3)
  73. {
  74. /**
  75. * Displays contacts in alphabetical order
  76. * Removes the "Done" statements made
  77. */
  78. //Display contacts in Alphabetical order
  79. //Arrays.sort(ArrayList name)
  80. name.remove("Done");
  81. Collections.sort(name);
  82.  
  83. for(int i = 0; i < name.size(); i++){
  84. System.out.println(name.get(i));
  85. }
  86. }
  87. else if(selection < 1 || selection > 4)
  88. {
  89. /**
  90. * Gives an error message
  91. * Then asks for another input
  92. */
  93. System.out.print("Invalid selection please try again: ");
  94. selection = Integer.parseInt(user.nextLine().trim());
  95. }
  96. /**
  97. * A 4 selection terminated this
  98. */
  99. }while(selection != 4);
  100. /**
  101. * Creates a new call log file
  102. */
  103. File callLog = new File("C:\\Users\\Henry\\Desktop\\JavaFiles\\log.txt");
  104.  
  105. //Source : https://stackoverflow.com/questions/9961292/write-to-text-file-without-overwriting-in-java
  106.  
  107. PrintWriter fileOUT;
  108. /**
  109. * Makes a new file
  110. * @catch IOException catches if a file cannot be made
  111. */
  112. try {
  113. fileOUT = new PrintWriter(new FileWriter(callLog, true));
  114. for(String print : name){
  115. fileOUT.println(print);
  116. }
  117. fileOUT.close();
  118.  
  119. } catch (IOException e)
  120. {
  121.  
  122. System.out.println("File does not exist.");
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129. }
  130.  
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement