// SortingDemoExceptionsTest3.java
//
// Demonstrates selectionSort and isSorted methods
// defined in class SortingDemoExceptions, which
// uses exceptions to detect inappropriate parameter
// values and an assertion to detect incorrect
// postconditions for selectionSort.
//
// Tests the selectionSort method using input from
// a text file.
//
// To compile this program in Java 1.4, type:
//    javac -source 1.4 SortingDemoExceptionsTest2.java
// (Compile it normally in Java 1.5 or later.)
//
// To run this program with assertions enabled, type:
//    java -ea SortingDemoExceptionsTest2
// (Otherwise, assertions are disabled.)

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

/**
 * This program sorts the numbers in a text file
 * containing short integers, one number per line,
 * and then outputs the sorted numbers to a
 * text area in a message dialog box.
 *
 * @author Dorothy L. Nixon
 */
public class SortingDemoExceptionsTest3
{
   /**
    * Main method.
    *
    * @param args command-line arguments.
    *             Just one command-line argument
    *             is needed for the input file.
    */
   public static void main(String[] args)
   {
      final int MAX_NUMBER_OF_NUMBERS = 40;

      if ( args.length == 0 )
      {
         System.out.println("This program sorts short integers");
         System.out.println("from the file specified by a");
         System.out.println("command-line argument.");
         System.exit(0);
      } // if

      final String inputFileName = args[0];

      short[] numbersArray = new short[MAX_NUMBER_OF_NUMBERS];

      // Read numbers into numbersArray from input file:
      int subArrayLength = inputFromFile(inputFileName, numbersArray);

      // Sort the numbers in ascending order:
      SortingDemoExceptions.selectionSort(numbersArray,
                                          subArrayLength);

      // Diaplay dialog box with text area containing
      // numbers in numbersArray:
      display(numbersArray, subArrayLength);

      // Close the program, including any threads
      // spawned by the dialog box:
      System.exit(0);
   }  // method main

   /**
    * Reads short integers from the specified
    * file to the specified array.  Returns the
    * number of numbers that have been read,
    * if successful.
    *
    * If the number of numbers in the file
    * exceeds the length of the array, then a 
    * user-friendly error message is printed
    * to the console, and the program is
    * terminated with error code 1.
    *
    * @param filename name of file containing
    *                 short integers, one per line.
    *                 The number of numbers in the
    *                 file is not known in advance.
    * @param numbers array into which numbers in 
    *                 the file are read.
    *
    * @return length of subarray of <code>numbers</code>
    *         containing actual numbers read from the file.
    */
   private static int inputFromFile(String filename,
                                    short[] numbers)
   {
      TextFileInput in = new TextFileInput(filename);

      // Read numbers into array:
      int lengthFilled = 0;
      String line = in.readLine();   // read first line in file
      while ( lengthFilled < numbers.length && line != null )
      {
         numbers[lengthFilled] = Short.parseShort(line);
         line = in.readLine();       // read next line in file
         lengthFilled++;
      } // while

      // Check to see if all the numbers in the file were read.
      // If not, then the array wasn't big enough to hold them all.
      // In that case, print an error message and quit.
      if ( line != null )            // i.e. if end-of-file not reached
      {
         System.out.println("File contains too many numbers.");
         System.out.println("This program can process only "
                             + numbers.length + " numbers.");
         System.exit(1);
      }  // if

      // Release file for re-use:
      in.close();

      return lengthFilled;
   }  // method inputFromFile

   /**
    * Displays short integers from the specified
    * subarray to a text area in a message dialog box.
    *
    * @param numbers partially-filled array of short
    *                integers
    * @param lengthFilled length of subarray of
    *                <code>numbers</code> containing
    *                the numbers to be displayed
    */
   private static void display(short[] numbers,
                               int lengthFilled)
   {
      final String lineBreak = System.getProperty("line.separator");

      // Create text area for output:
      JTextArea textArea = new JTextArea();
      textArea.setEditable(false);

      // Print numbers to output text area:
      for ( int i = 0; i < lengthFilled; i++ )
         textArea.append(numbers[i] + lineBreak);

      // Display the output text area via a message dialog box:
      JOptionPane.showMessageDialog(null, textArea);
   }  // method display
}  // class SortingDemoExceptionsTest3