// ExamScores3.java
// Given an input text file ExamScores.txt
// containing exam scores, displays dialog box
// with text area containing curved scores.
//
// Prints an error message if the file contains
// too many numbers to fit into the array.
//
// Note that the String line is declared and
// initialized before the loop which reads the
// input file.  This is necessary so that the
// variable line can be tested later, after that
// loop.

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

/**
 * Program which, given an input text file
 * ExamScores.txt containing exam scores,
 * displays a dialog box with text area
 * containing curved scores.
 *
 * @author D. Nixon
 */
public class ExamScores3  {

   /**
    * Main method.
    *
    * @param args command-line arguments - none needed
    */
   public static void main(String[] args)
   {
      final int MAX_NUMBER_OF_SCORES = 40;
      final int columnWidth = 5;
      final String inputFileName = "ExamScores.txt";
      final String lineBreak = System.getProperty("line.separator");

      TextFileInput in = new TextFileInput(inputFileName);
      JTextArea textArea = new JTextArea();

      // Read scores and determine maximum:
      int[] scores = new int[MAX_NUMBER_OF_SCORES];
      int maximumScore = 0;
      int lengthFilled = 0;
      String line = in.readLine();   // read first line in file
      while ( lengthFilled < scores.length && line != null )
      {
         scores[lengthFilled] = Integer.parseInt(line);
         maximumScore = Math.max(maximumScore, scores[lengthFilled]);
         lengthFilled++;
         line = in.readLine();       // read next line in file
      } // for i

      // 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 "
                             + scores.length + " numbers.");
         return;
      }  // if

      // Determine how many points to curve up all scores:
      int curveUp = 100 - maximumScore;

      // Print original scores and curved scores
      // to output text area:
      for ( int i = 0; i < lengthFilled; i++ )
      {
         // Print original score in a right-justified column:
         textArea.append(rightJustify(scores[i], columnWidth));

         // Calculate curved score;
         int curvedScore = scores[i] + curveUp;

         // Print curved score in a right-justified column;
         textArea.append(rightJustify(curvedScore, columnWidth)
                         + lineBreak);
      } // for i

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

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

   /**
    * Generates a string consisting of a string
    * representation of the specified integer
    * padded with enough leading spaces to 
    * facilitate printing in a column with the
    * specified width.
    *
    * @param numberToPrint number to be padded
    *               with leading spaces
    * @param width column width
    * @return string representation of
    *         <code>numberToPrint</code> padded
    *         with leading spaces
    */
   private static String rightJustify(int numberToPrint,
                                      int width)
   {
      String numberText = Integer.toString(numberToPrint);
      for ( int i = numberText.length(); i < width; i++ )
         numberText = " " + numberText;
      return numberText;
   }  // method rightJustify
}  // class ExamScores3