123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- import java.util.TreeMap;
- import java.util.Map;
- import java.util.ArrayList;
- import java.util.Scanner;
-
- /**
- * Created by Rachelle
- */
- public class PhoneBook {
- Map<String,Entry> phonebook = new TreeMap<String,Entry>();
- 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("This is " + reverseLookup(phoneNumber) + "'s number.");
- }
- 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){
- String nameForAGivenNumber = "no match";
- for(String name: phonebook.keySet()){
- Entry entry = phonebook.get(name);
- String phoneNumber = entry.getPhoneNumber();
- if (phoneNumber.equals(number)){
- nameForAGivenNumber = name;
- }
- }
- return nameForAGivenNumber;
- }
-
- //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");
- }
- }
|