// ListTestUIConsole.java
//
// Uses, in this diretory:
//     ConsoleInput.java
//     ListTestUI.java
//
// To compile under Java 1.4 (and avoid warnings under 1.5), type:
//    javac -source 1.4 ListTestUIConsole.java

import java.util.Hashtable;

public class ListTestUIConsole implements ListTestUI
{
   private static String[] options
                       = new String[] {"N", "A", "I", "J",
                                       "R", "E", "H", "B"};
   private static Hashtable table;

   static {
      // Number of letter options should match
      // number of longer string options:
      assert ( options.length == ListTestUI.options.length )
             : "options.length=" + options.length
                    + " and ListTestUI.options.length="
                    + ListTestUI.options.length
                    + " should be equal.";

      // Pair up letter options with longer string options:
      table = new Hashtable();
      for (int i = 0; i < options.length; i++)
         table.put(options[i], ListTestUI.options[i]);
   }  // static initialization block

   public String askWhichMethod()
   {
      System.out.println("Select a method to test.");
      System.out.println();
      for ( int i = 0; i < options.length; i++ )
         System.out.println("   " + options[i] + " - "
                                + table.get(options[i]));
      System.out.println();

      String answer = ConsoleInput.askSelection(
                            "Enter your selection:",
                            options);

      return (String) table.get(answer.toUpperCase());
   }  // method

   public int askIndex()
   {
      return Integer.parseInt(
                      ConsoleInput.ask(
                            "Enter an index:"));
   }  // method

   public short askData()
   {
      return Short.parseShort(
                      ConsoleInput.ask(
                           "Enter a data item:"));
   }  // method

   public boolean askYesNo(String question, String abbrevQuestion)
   {
      System.out.println(question);
      return ConsoleInput.askYesNo(abbrevQuestion);
   }  // method askYesNo


   public void displayErrorMessage(RuntimeException re)
   {
      re.printStackTrace();
   }  // method

   public void displayAnnouncementMessage(String message,
                                          String title)
   {
      System.out.println(message);
   }  // method displayAnnoucementMessage

   public void displayResults(String[] text)
   {
      System.out.println();
      for ( int i = 0; i < text.length; i++ )
         System.out.println(text[i]);
      System.out.println();
   }  // method displayResults

   public void runTestsRepeatedly()
   {
      do  {   // Test both lists repeatedly
         ListTest.runTests();
      } while ( askYesNo("Test the two lists again (and possibly"
                                + " create/destroy them first)?",
                         "Test again?") );
   }  // method runTestsRepeatedly
} // class ListTestUIConsole