Source code for ConsoleReader.java

package PhoneApp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import PhoneApp.Data.*;

/**
 * This class provides the ability to read input from
 * a console. Users must enter commands line-by-line,
 * as the input is only triggered by hitting enter.
 *
 * @version $Date: 2001/06/25 $
 * @author  Steve Franklin
 * @since   jdk 1.3.1
 */

class ConsoleReader {

 /**
  * Default constructor has no behavior.
  */
  public ConsoleReader() {

  }

 /**
  * Send a prompt to the users, typically indicating the
  * type of information that the software is expecting.
  */
  public void sendPrompt(String prompt) {
    sendText(prompt + "\n");
  }

 /**
  * Send text to the user, and flush the output.
  */
  public void sendText(String text) {
    System.out.print(text);
    System.out.flush();
  }
  
 /**
  * Retrieve input from the user after providing a field-level
  * prompt.
  */
  public String getInput(String prompt) {
    System.out.print(prompt + ": ");
    System.out.flush();
    String userEntry = readInput();
    return userEntry;
  }
  
  /**
   * Read a number from the console
   */
  public int getUserNumber(String title, String prompt) {
    if ( title.length() > 0 ) {
      sendPrompt(title);
    }
    String optionText = getInput(prompt);
    int option = -1;
    try {
      option = Integer.parseInt(optionText);
    } catch (Exception e) {
      System.out.println("Unrecognized option.");
    }
    return option;
  }
  
 /**
  * Read input from the console using the BufferedReader. Trigger
  * input based on the user pressing enter. This function reads one
  * line at a time.
  */
  private String readInput() {
    BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in));
    String userEntry = "";
    try { userEntry=consoleInput.readLine(); } catch (java.io.IOException e) { userEntry = ""; }
    return userEntry;
  }
  
}