Build a simple PhoneBook program.

PhoneBook.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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("This is " + reverseLookup(phoneNumber) + "'s number.");
  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. String nameForAGivenNumber = "no match";
  134. for(String name: phonebook.keySet()){
  135. Entry entry = phonebook.get(name);
  136. String phoneNumber = entry.getPhoneNumber();
  137. if (phoneNumber.equals(number)){
  138. nameForAGivenNumber = name;
  139. }
  140. }
  141. return nameForAGivenNumber;
  142. }
  143. //Deletion
  144. public void deleteRecordOrNumberPrompt(){
  145. boolean error = false;
  146. do{
  147. if(phonebook.size()<=0){
  148. System.out.println("\n**No records to delete.**\n");
  149. break;
  150. }else{
  151. System.out.println("\nRemove an entire (record) or remove a phone (number)? Enter (exit) to go back.");
  152. String userInput = getUserInputNormalized();
  153. if(userInput.equals("record")){
  154. removeRecord();
  155. }else if(userInput.equals("number")){
  156. removeNumber();
  157. }else if(userInput.equals("exit")){
  158. break;
  159. }else{
  160. System.out.println("\n**Command not recognized.**");
  161. error = true;
  162. }
  163. }
  164. }while(error);
  165. }
  166. public void removeRecord(){
  167. String recordToRemove = getNameFromUser();
  168. if(hasEntry(recordToRemove)){
  169. confirmRemoveEntry(recordToRemove);
  170. }else{
  171. System.out.println("\n**Name not found.**\n");
  172. }
  173. }
  174. public void confirmRemoveEntry(String name){
  175. boolean continueWithDelete = true;
  176. while(continueWithDelete){
  177. System.out.println("\n" + displayRecord(name) + "\nContinue with deleting this listing? (y/n)");
  178. String userInput = getUserInputNormalized();
  179. if(userInput.equals("n")||userInput.equals("no")){
  180. System.out.println("\n**Delete cancelled.**\n");
  181. continueWithDelete = false;
  182. }else if(userInput.equals("y")||userInput.equals("yes")){
  183. removeEntry(name);
  184. continueWithDelete = false;
  185. }else{
  186. System.out.println("\n**Command not recognized.**");
  187. }
  188. }
  189. }
  190. public void removeEntry(String name){
  191. phonebook.remove(name);
  192. System.out.println("\n**" + name + " deleted.**\n");
  193. }
  194. public void removeNumber(){
  195. String name = getNameFromUser();
  196. if(hasEntry(name) && getEntry(name).getNumberOfRecords()>1){
  197. deleteWhereMultipleEntries(name);
  198. }else if(hasEntry(name) && getEntry(name).getNumberOfRecords()==1){
  199. deleteWhereSingleEntry(name);
  200. }else{
  201. System.out.println("\n**Name not found.**\n");
  202. }
  203. }
  204. public void deleteWhereMultipleEntries(String name){
  205. System.out.println("\nPhone numbers listed for: " + displayRecord(name));
  206. String phoneNumber = getNumberFromUser();
  207. getEntry(name).removePhoneNumber(phoneNumber);
  208. System.out.println("\n**Deleted " + phoneNumber + ".**\n**Record updated to: " + displayRecord(name) + ".**");
  209. continueMessage();
  210. }
  211. public void deleteWhereSingleEntry(String name){
  212. System.out.println("\nThere is only one number listed for " + displayRecord(name) + ".");
  213. boolean continueToPrompt = true;
  214. while(continueToPrompt){
  215. System.out.println("Continue with deleting this record? (y/n)");
  216. String userInput = getUserInputNormalized();
  217. if(userInput.equals("n")||userInput.equals("no")){
  218. System.out.println("\n**Delete cancelled.**\n");
  219. continueToPrompt = false;
  220. }else if(userInput.equals("y")||userInput.equals("yes")){
  221. removeEntry(name);
  222. continueToPrompt = false;
  223. }else{
  224. System.out.println("\n**Command not recognized.**\n");
  225. }
  226. }
  227. }
  228. //Display
  229. public String displayPhonebook(){
  230. StringBuilder stringToDisplay = new StringBuilder();
  231. stringToDisplay.append("\n**Listings:**\n");
  232. for(String name: phonebook.keySet()){
  233. Entry entry = phonebook.get(name);
  234. String phoneNumber = entry.getPhoneNumber();
  235. stringToDisplay.append(name + ": " + phoneNumber + "\n");
  236. }
  237. return stringToDisplay.toString();
  238. }
  239. public String displayRecord(String name){
  240. StringBuilder stringToDisplay = new StringBuilder();
  241. String phoneNumber = phonebook.get(name).getPhoneNumber();
  242. stringToDisplay.append(name + ": " + phoneNumber);
  243. return stringToDisplay.toString();
  244. }
  245. //Messaging
  246. public void continueMessage(){
  247. System.out.println("Press enter to continue.");
  248. scanner.nextLine();
  249. }
  250. public void recordUpdatedMessage(String name, String phoneNumber){
  251. System.out.println("\n**" + phoneNumber + " added to " + name + "**\n");
  252. }
  253. }