Build a simple PhoneBook program.

PhoneBook.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import java.util.TreeMap;
  2. import java.util.Map;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5. /**
  6. * Created by Rachelle
  7. */
  8. public class PhoneBook {
  9. Map<String,Entry> phonebook = new TreeMap<String,Entry>();
  10. Scanner scanner = new Scanner(System.in);
  11. public PhoneBook(){
  12. }
  13. public void main(){
  14. System.out.println("Welcome!");
  15. boolean runningProgram = true;
  16. while(runningProgram){
  17. System.out.println("\nSelect an option from the menu.\n"
  18. + "(Add): to add a new entry or to an existing entry\n(Lookup): to lookup a number\n"
  19. + "(Reverse): to perform a reverse lookup\n(Display) to print phonebook\n"
  20. + "(Delete): to remove a record or a phone number\n(Exit): to exit the phonebook");
  21. switch(getUserInputNormalized()){
  22. case "add":
  23. addEntry(getNameFromUser(),getNumberFromUser());
  24. break;
  25. case "lookup":
  26. String name = getNameFromUser();
  27. if(hasEntry(name)){
  28. System.out.println("\n" + name + ": " + lookupNumber(name));
  29. }else{
  30. System.out.println("\n**Name not found**");
  31. }
  32. continueMessage();
  33. break;
  34. case "reverse":
  35. String phoneNumber = getNumberFromUser();
  36. if(reverseLookup(phoneNumber).equals("no match")){
  37. System.out.println("\n**Number not found.**");
  38. }else{
  39. System.out.println("\n**" + phoneNumber + " listed to:**\n" + reverseLookup(phoneNumber));
  40. }
  41. continueMessage();
  42. break;
  43. case "display":
  44. if(phonebook.isEmpty()){
  45. System.out.println("\n**Nothing to display. Please add an entry first.**");
  46. }else{
  47. System.out.println(displayPhonebook());
  48. }
  49. continueMessage();
  50. break;
  51. case "delete":
  52. deleteRecordOrNumberPrompt();
  53. break;
  54. case "exit": runningProgram = false;
  55. break;
  56. default: System.out.println("\n**Command not found.**\n");
  57. break;
  58. }
  59. }
  60. }
  61. //Input
  62. public String getUserInputNormalized(){
  63. Scanner scanner = new Scanner(System.in);
  64. return scanner.nextLine().toLowerCase().trim();
  65. }
  66. public String getNumberFromUser(){
  67. System.out.println("Enter phone number.");
  68. return scanner.nextLine();
  69. }
  70. public String getNameFromUser(){
  71. System.out.println("Enter name.");
  72. return scanner.nextLine();
  73. }
  74. //Additions
  75. public void addEntry(String name, String phoneNumber){
  76. if(hasEntry(name)){
  77. duplicateEntry(name, phoneNumber);
  78. }else{
  79. Entry newEntry = new Entry(phoneNumber);
  80. phonebook.put(name,newEntry);
  81. recordUpdatedMessage(name, phoneNumber);
  82. }
  83. }
  84. public boolean hasEntry(String name){
  85. return phonebook.containsKey(name);
  86. }
  87. public void duplicateEntry(String name, String phoneNumber){
  88. boolean duplicateError = false;
  89. do{
  90. System.out.println("\nDuplicate name found. Add to existing entry? (y/n). Enter (exit) to go back.");
  91. String userInput = getUserInputNormalized();
  92. if(userInput.equals("n")||userInput.equals("no")){
  93. addEntry(getNameFromUser(),phoneNumber);
  94. }else if(userInput.equals("y")||userInput.equals("yes")){
  95. addAdditionalNumbers(name, phoneNumber);
  96. }else if(userInput.equals("exit")){
  97. break;
  98. }else{
  99. System.out.println("\n**Command not recognized.**\n");
  100. duplicateError = true;
  101. }
  102. }while(duplicateError);
  103. }
  104. public void addAdditionalNumbers(String name, String phoneNumber){
  105. boolean duplicateNumber = false;
  106. do{
  107. if(getEntry(name).entryArrayContainsNumber(phoneNumber)){
  108. System.out.println("\nThis phone number is already listed for " + name + ". Enter a new number or enter (exit) to go back.");
  109. System.out.println("Record found: " + displayRecord(name));
  110. String userInput = getUserInputNormalized();
  111. if(getEntry(name).entryArrayContainsNumber(userInput)){
  112. duplicateNumber = true;
  113. }else if(!userInput.equals("exit")){
  114. getEntry(name).addPhoneNumber(userInput);
  115. recordUpdatedMessage(name,userInput);
  116. duplicateNumber = false;
  117. }
  118. }else{
  119. getEntry(name).addPhoneNumber(phoneNumber);
  120. recordUpdatedMessage(name, phoneNumber);
  121. }
  122. }while(duplicateNumber);
  123. }
  124. public Entry getEntry(String name){
  125. return phonebook.get(name);
  126. }
  127. //Lookups
  128. public String lookupNumber(String name){
  129. Entry entryToLookUp = phonebook.get(name);
  130. return entryToLookUp.getPhoneNumber();
  131. }
  132. public String reverseLookup(String number){
  133. StringBuilder personsWithGivenNumber = new StringBuilder();
  134. for(String name: phonebook.keySet()){
  135. Entry entry = phonebook.get(name);
  136. if (entry.hasPhoneNumber(number)){
  137. personsWithGivenNumber.append(name + "\n");
  138. }
  139. }
  140. return personsWithGivenNumber.toString();
  141. }
  142. //Deletion
  143. public void deleteRecordOrNumberPrompt(){
  144. boolean error = false;
  145. do{
  146. if(phonebook.size()<=0){
  147. System.out.println("\n**No records to delete.**\n");
  148. break;
  149. }else{
  150. System.out.println("\nRemove an entire (record) or remove a phone (number)? Enter (exit) to go back.");
  151. String userInput = getUserInputNormalized();
  152. if(userInput.equals("record")){
  153. removeRecord();
  154. }else if(userInput.equals("number")){
  155. removeNumber();
  156. }else if(userInput.equals("exit")){
  157. break;
  158. }else{
  159. System.out.println("\n**Command not recognized.**");
  160. error = true;
  161. }
  162. }
  163. }while(error);
  164. }
  165. public void removeRecord(){
  166. String recordToRemove = getNameFromUser();
  167. if(hasEntry(recordToRemove)){
  168. confirmRemoveEntry(recordToRemove);
  169. }else{
  170. System.out.println("\n**Name not found.**\n");
  171. }
  172. }
  173. public void confirmRemoveEntry(String name){
  174. boolean continueWithDelete = true;
  175. while(continueWithDelete){
  176. System.out.println("\n" + displayRecord(name) + "\nContinue with deleting this listing? (y/n)");
  177. String userInput = getUserInputNormalized();
  178. if(userInput.equals("n")||userInput.equals("no")){
  179. System.out.println("\n**Delete cancelled.**\n");
  180. continueWithDelete = false;
  181. }else if(userInput.equals("y")||userInput.equals("yes")){
  182. removeEntry(name);
  183. continueWithDelete = false;
  184. }else{
  185. System.out.println("\n**Command not recognized.**");
  186. }
  187. }
  188. }
  189. public void removeEntry(String name){
  190. phonebook.remove(name);
  191. System.out.println("\n**" + name + " deleted.**\n");
  192. }
  193. public void removeNumber(){
  194. String name = getNameFromUser();
  195. if(hasEntry(name) && getEntry(name).getNumberOfRecords()>1){
  196. deleteWhereMultipleEntries(name);
  197. }else if(hasEntry(name) && getEntry(name).getNumberOfRecords()==1){
  198. deleteWhereSingleEntry(name);
  199. }else{
  200. System.out.println("\n**Name not found.**\n");
  201. }
  202. }
  203. public void deleteWhereMultipleEntries(String name){
  204. System.out.println("\nPhone numbers listed for: " + displayRecord(name));
  205. String phoneNumber = getNumberFromUser();
  206. getEntry(name).removePhoneNumber(phoneNumber);
  207. System.out.println("\n**Deleted " + phoneNumber + ".**\n**Record updated to: " + displayRecord(name) + ".**");
  208. continueMessage();
  209. }
  210. public void deleteWhereSingleEntry(String name){
  211. System.out.println("\nThere is only one number listed for " + displayRecord(name) + ".");
  212. boolean continueToPrompt = true;
  213. while(continueToPrompt){
  214. System.out.println("Continue with deleting this record? (y/n)");
  215. String userInput = getUserInputNormalized();
  216. if(userInput.equals("n")||userInput.equals("no")){
  217. System.out.println("\n**Delete cancelled.**\n");
  218. continueToPrompt = false;
  219. }else if(userInput.equals("y")||userInput.equals("yes")){
  220. removeEntry(name);
  221. continueToPrompt = false;
  222. }else{
  223. System.out.println("\n**Command not recognized.**\n");
  224. }
  225. }
  226. }
  227. //Display
  228. public String displayPhonebook(){
  229. StringBuilder stringToDisplay = new StringBuilder();
  230. stringToDisplay.append("\n**Listings:**\n");
  231. for(String name: phonebook.keySet()){
  232. Entry entry = phonebook.get(name);
  233. String phoneNumber = entry.getPhoneNumber();
  234. stringToDisplay.append(name + ": " + phoneNumber + "\n");
  235. }
  236. return stringToDisplay.toString();
  237. }
  238. public String displayRecord(String name){
  239. StringBuilder stringToDisplay = new StringBuilder();
  240. String phoneNumber = phonebook.get(name).getPhoneNumber();
  241. stringToDisplay.append(name + ": " + phoneNumber);
  242. return stringToDisplay.toString();
  243. }
  244. //Messaging
  245. public void continueMessage(){
  246. System.out.println("Press enter to continue.");
  247. scanner.nextLine();
  248. }
  249. public void recordUpdatedMessage(String name, String phoneNumber){
  250. System.out.println("\n**" + phoneNumber + " added to " + name + "**\n");
  251. }
  252. }