import java.util.TreeMap; import java.util.Map; import java.util.ArrayList; import java.util.Scanner; /** * Created by Rachelle */ public class PhoneBook { Map phonebook = new TreeMap(); Scanner scanner = new Scanner(System.in); public PhoneBook(){ } public void main(){ System.out.println("Welcome!"); boolean runningProgram = true; while(runningProgram){ System.out.println("\nSelect an option from the menu.\n" + "(Add): to add a new entry or to an existing entry\n(Lookup): to lookup a number\n" + "(Reverse): to perform a reverse lookup\n(Display) to print phonebook\n" + "(Delete): to remove a record or a phone number\n(Exit): to exit the phonebook"); switch(getUserInputNormalized()){ case "add": addEntry(getNameFromUser(),getNumberFromUser()); break; case "lookup": String name = getNameFromUser(); if(hasEntry(name)){ System.out.println("\n" + name + ": " + lookupNumber(name)); }else{ System.out.println("\n**Name not found**"); } continueMessage(); break; case "reverse": String phoneNumber = getNumberFromUser(); if(reverseLookup(phoneNumber).equals("no match")){ System.out.println("\n**Number not found.**"); }else{ System.out.println("\n**" + phoneNumber + " listed to:**\n" + reverseLookup(phoneNumber)); } continueMessage(); break; case "display": if(phonebook.isEmpty()){ System.out.println("\n**Nothing to display. Please add an entry first.**"); }else{ System.out.println(displayPhonebook()); } continueMessage(); break; case "delete": deleteRecordOrNumberPrompt(); break; case "exit": runningProgram = false; break; default: System.out.println("\n**Command not found.**\n"); break; } } } //Input public String getUserInputNormalized(){ Scanner scanner = new Scanner(System.in); return scanner.nextLine().toLowerCase().trim(); } public String getNumberFromUser(){ System.out.println("Enter phone number."); return scanner.nextLine(); } public String getNameFromUser(){ System.out.println("Enter name."); return scanner.nextLine(); } //Additions public void addEntry(String name, String phoneNumber){ if(hasEntry(name)){ duplicateEntry(name, phoneNumber); }else{ Entry newEntry = new Entry(phoneNumber); phonebook.put(name,newEntry); recordUpdatedMessage(name, phoneNumber); } } public boolean hasEntry(String name){ return phonebook.containsKey(name); } public void duplicateEntry(String name, String phoneNumber){ boolean duplicateError = false; do{ System.out.println("\nDuplicate name found. Add to existing entry? (y/n). Enter (exit) to go back."); String userInput = getUserInputNormalized(); if(userInput.equals("n")||userInput.equals("no")){ addEntry(getNameFromUser(),phoneNumber); }else if(userInput.equals("y")||userInput.equals("yes")){ addAdditionalNumbers(name, phoneNumber); }else if(userInput.equals("exit")){ break; }else{ System.out.println("\n**Command not recognized.**\n"); duplicateError = true; } }while(duplicateError); } public void addAdditionalNumbers(String name, String phoneNumber){ boolean duplicateNumber = false; do{ if(getEntry(name).entryArrayContainsNumber(phoneNumber)){ System.out.println("\nThis phone number is already listed for " + name + ". Enter a new number or enter (exit) to go back."); System.out.println("Record found: " + displayRecord(name)); String userInput = getUserInputNormalized(); if(getEntry(name).entryArrayContainsNumber(userInput)){ duplicateNumber = true; }else if(!userInput.equals("exit")){ getEntry(name).addPhoneNumber(userInput); recordUpdatedMessage(name,userInput); duplicateNumber = false; } }else{ getEntry(name).addPhoneNumber(phoneNumber); recordUpdatedMessage(name, phoneNumber); } }while(duplicateNumber); } public Entry getEntry(String name){ return phonebook.get(name); } //Lookups public String lookupNumber(String name){ Entry entryToLookUp = phonebook.get(name); return entryToLookUp.getPhoneNumber(); } public String reverseLookup(String number){ StringBuilder personsWithGivenNumber = new StringBuilder(); for(String name: phonebook.keySet()){ Entry entry = phonebook.get(name); if (entry.hasPhoneNumber(number)){ personsWithGivenNumber.append(name + "\n"); } } return personsWithGivenNumber.toString(); } //Deletion public void deleteRecordOrNumberPrompt(){ boolean error = false; do{ if(phonebook.size()<=0){ System.out.println("\n**No records to delete.**\n"); break; }else{ System.out.println("\nRemove an entire (record) or remove a phone (number)? Enter (exit) to go back."); String userInput = getUserInputNormalized(); if(userInput.equals("record")){ removeRecord(); }else if(userInput.equals("number")){ removeNumber(); }else if(userInput.equals("exit")){ break; }else{ System.out.println("\n**Command not recognized.**"); error = true; } } }while(error); } public void removeRecord(){ String recordToRemove = getNameFromUser(); if(hasEntry(recordToRemove)){ confirmRemoveEntry(recordToRemove); }else{ System.out.println("\n**Name not found.**\n"); } } public void confirmRemoveEntry(String name){ boolean continueWithDelete = true; while(continueWithDelete){ System.out.println("\n" + displayRecord(name) + "\nContinue with deleting this listing? (y/n)"); String userInput = getUserInputNormalized(); if(userInput.equals("n")||userInput.equals("no")){ System.out.println("\n**Delete cancelled.**\n"); continueWithDelete = false; }else if(userInput.equals("y")||userInput.equals("yes")){ removeEntry(name); continueWithDelete = false; }else{ System.out.println("\n**Command not recognized.**"); } } } public void removeEntry(String name){ phonebook.remove(name); System.out.println("\n**" + name + " deleted.**\n"); } public void removeNumber(){ String name = getNameFromUser(); if(hasEntry(name) && getEntry(name).getNumberOfRecords()>1){ deleteWhereMultipleEntries(name); }else if(hasEntry(name) && getEntry(name).getNumberOfRecords()==1){ deleteWhereSingleEntry(name); }else{ System.out.println("\n**Name not found.**\n"); } } public void deleteWhereMultipleEntries(String name){ System.out.println("\nPhone numbers listed for: " + displayRecord(name)); String phoneNumber = getNumberFromUser(); getEntry(name).removePhoneNumber(phoneNumber); System.out.println("\n**Deleted " + phoneNumber + ".**\n**Record updated to: " + displayRecord(name) + ".**"); continueMessage(); } public void deleteWhereSingleEntry(String name){ System.out.println("\nThere is only one number listed for " + displayRecord(name) + "."); boolean continueToPrompt = true; while(continueToPrompt){ System.out.println("Continue with deleting this record? (y/n)"); String userInput = getUserInputNormalized(); if(userInput.equals("n")||userInput.equals("no")){ System.out.println("\n**Delete cancelled.**\n"); continueToPrompt = false; }else if(userInput.equals("y")||userInput.equals("yes")){ removeEntry(name); continueToPrompt = false; }else{ System.out.println("\n**Command not recognized.**\n"); } } } //Display public String displayPhonebook(){ StringBuilder stringToDisplay = new StringBuilder(); stringToDisplay.append("\n**Listings:**\n"); for(String name: phonebook.keySet()){ Entry entry = phonebook.get(name); String phoneNumber = entry.getPhoneNumber(); stringToDisplay.append(name + ": " + phoneNumber + "\n"); } return stringToDisplay.toString(); } public String displayRecord(String name){ StringBuilder stringToDisplay = new StringBuilder(); String phoneNumber = phonebook.get(name).getPhoneNumber(); stringToDisplay.append(name + ": " + phoneNumber); return stringToDisplay.toString(); } //Messaging public void continueMessage(){ System.out.println("Press enter to continue."); scanner.nextLine(); } public void recordUpdatedMessage(String name, String phoneNumber){ System.out.println("\n**" + phoneNumber + " added to " + name + "**\n"); } }