import java.awt.*;
import javax.swing.*;
/** DialogReader accepts user input from a dialog window */
public class DialogReader
{
  /** Constructor DialogReader initializes the input view, a dialog  */
  public DialogReader() { }  // nothing to initialize!

  /** readString reads a string from the input source.
    * @param prompt - the prompt that prompts the user for input
    * @return the string the user types in response */
  public String readString(String prompt)
  { String input = JOptionPane.showInputDialog(prompt); // see Chapter 10
    return input;
  }

  /** readInt reads an integer from the input source
    * @param prompt - the prompt that prompts the user for input
    * @return the integer supplied in response */
  public int readInt(String prompt)
  { int input;
    try { input = new Integer( readString(prompt).trim() ).intValue(); }
    catch (RuntimeException e)
          { System.out.println("Reader error: not an integer. Try again!");
            input = readInt(prompt);   // restart!
          }
    return input;
  }

  /** readDouble reads a double from the input source
    * @param prompt - the prompt that prompts the user for input
    * @return the double supplied in response */
  public double readDouble(String prompt)
  { double input;
    try { input = new Double( readString(prompt).trim() ).doubleValue(); }
    catch (RuntimeException e)
          { System.out.println("Reader error: not a double. Try again!");
            input = readDouble(prompt);  // restart!
          }
    return input;
  }
}
