import java.io.*; /** This class is an implementation of PDUserInterface that uses the console to display the menu of command choices. @author Koffman & Wolfgang */ public class PDConsoleUI implements PDUserInterface { /** A reference to the PhoneDirectory object to be processed. Globally available to the command-processing methods. */ private PhoneDirectory theDirectory = null; /** Buffered reader to read from the input console. */ private BufferedReader in = null; // Constructor /** Default constructor. */ public PDConsoleUI() { in = new BufferedReader(new InputStreamReader(System.in)); } // Methods /** Method to display the command choices and process user commands. pre: The directory exists and has been loaded with data. post: The directory is updated based on user commands. @param thePhoneDirectory A reference to the PhoneDirectory to be processed */ public void processCommands(PhoneDirectory thePhoneDirectory) { String[] commands = { "Add/Change Entry", "Look Up Entry", "Remove Entry", "Save Directory", "Exit"}; theDirectory = thePhoneDirectory; int choice; try { do { for (int i = 0; i < commands.length; i++) { System.out.println("Select " + i + ": " + commands[i]); } String line = in.readLine(); if (line != null) choice = Integer.parseInt(line); else choice = commands.length - 1; switch (choice) { case 0: doAddChangeEntry(); break; case 1: doLookupEntry(); break; case 2: doRemoveEntry(); break; case 3: doSave(); break; case 4: doSave(); break; } } while (choice < commands.length - 1); System.exit(0); } catch (IOException ex) { System.err.println ("IO Exception while reading from System.in"); System.exit(1); } } /** Method to add or change an entry. pre: The directory exists and has been loaded with data. post: A new name is added, or the value for the name is changed, modified is set to true. @throws IOException - if an IO error occurs */ private void doAddChangeEntry() throws IOException { // Request the name. System.out.println("Enter name"); String newName = null; newName = in.readLine(); if (newName == null) { return; } // Request the number. System.out.println("Enter number"); String newNumber = null; newNumber = in.readLine(); if (newNumber == null) { return; } // Insert/change name-number. String oldNumber = (theDirectory.addOrChangeEntry(newName, newNumber)); String message = null; if (oldNumber == null) { // New entry. message = newName + " was added to the directory" + "\nNew number: " + newNumber; } else { // Changed entry. message = "Number for " + newName + " was changed" + "\nOld number: " + oldNumber + "\nNew number: " + newNumber; } // Display confirmation message. System.out.println(message); } /** Method to look up an entry. pre: The directory has been loaded with data. post: No changes made to the directory. @throws IOException - If an IO error occurs */ private void doLookupEntry() throws IOException { // Request the name. System.out.println("Enter name"); String theName = null; theName = in.readLine(); if (theName == null) { return; // Dialog was cancelled. } // Look up the name. String theNumber = theDirectory.lookupEntry(theName); String message = null; if (theNumber != null) { // Name was found. message = "The number for " + theName + " is " + theNumber; } else { // Name was not found. message = theName + " is not listed in the directory"; } // Display the result. System.out.println(message); } /** Method to remove an entry. pre: The directory has been loaded with data. post: The requested name is removed, modifed is set to true. @throws IOException - If there is an IO Error */ private void doRemoveEntry() throws IOException { /**** BEGIN EXERCISE ****/ // Request the name. System.out.println("Enter name"); String theName = null; theName = in.readLine(); if (theName == null) { return; // Dialog was cancelled. } // remove the name. String theNumber = theDirectory.lookupEntry(theName); String message = null; if (theNumber != null) { // Name was found. theDirectory.removeEntry(theName); message = "" + theName + " has been removed."; } else { // Name was not found. message = theName + " is not listed in the directory"; } // Display the result. System.out.println(message); /**** END EXERCISE ****/ } /** Method to save the directory to the data file. pre: The directory has been loaded with data. post: The current contents of the directory have been saved to the data file. */ private void doSave() { theDirectory.save(); } }