/**
 * TestGUI.java -- this is an application/main class that uses Java wrapper
 * class JOptionPane from the javax.swing package.  It also uses some java.io
 * package to read from console and write to console.
 * It has been created to illustrate the two different interfaces: console vs.
 * GUI.  Please run and understand the following claerly.
 *
 * CSCE 155 Fall 2005
 * @author Leen-Kiat Soh
 * @version 1.0
 */

import javax.swing.*;
import java.io.*;

class TestGUI   {

   /**
    * This method prompts the user for an integer (age) using the JOptionPane
    * GUI.  It also echos the response by using the JOptionPane GUI.
    */

   public static void promptForInput_GUI()  {

      // graphical user interface
      String str = JOptionPane.showInputDialog(null, "Enter age:");
      int age = Integer.parseInt(str);

      // this is to confirm what the user has typed (echoing)
      JOptionPane.showMessageDialog(null, "You entered: " + age);

   }  // end promptForInput_GUI

   /**
    * This method prompts the user for an integer (age) through console
    * interface.  It also echos the response by printing out a string to the
    * console.
    */

   public static void promptForInput_Console()  {

      String userInput = "";
      int age = 0;

      // this is to obtain an input
      System.out.println("Enter age:");
      BufferedReader stdin 
         = new BufferedReader(new InputStreamReader(System.in), 1);
      try {
         userInput = stdin.readLine();
         age = Integer.parseInt(userInput);   // convert to integer
      }  catch (IOException ex)  {
         System.out.println(ex);
         }

      // now print to the screen
      System.out.println("You entered: " + age);

   }  // end promptForInput_Console

   /**
    * The main method of TestGUI, allows the class to be invoked with one
    * argument.  It calls either promptForInput_GUI or promptForInput_Console.
    * @params String[] args expects an integer
    */

   public static void main (String[] args)  {

      // first check to see whether the program is invoked with the correct
      // number of arguments.
      
      if (args.length != 1)  {
	 System.out.println("USAGE: java TestGUI <choice>");
         System.out.println("<choice> either 0 or 1");
	 System.exit(0);
      }

      // then determine what types of interface to use to prompt for an input
      // if choice == 0, GUI; if choice == 1, Console; otherwise, do nothing
      
      int choice = Integer.parseInt(args[0]);
      if (choice == 0) 
	 promptForInput_GUI();          // GUI
      else if (choice == 1) 
	 promptForInput_Console();      // use Console

   }  // end main method

}  // end TestGUI Class

