/****************************************
Describe the program
 This application reads in a series of letter grades and calculates a
 numerical average based on ranking:
 A is worth 4 points
 B is worth 3 points
 C is worth 2 points
 D is worth 1 point
 F is worth 0 points

NOTE: This program is a modification of Figure 2.7 to use ConsoleInput
      for input, a defined constant to control the number of grades
      that are entered, and a boolean variable for validation. In addition,
      the calculation for the final grade average is done in floating point for
      a more accurate decimal calculation
*****************************************/

// import java.io.*;

public class AverageC {
  public static void main( String args[] )
  {
    int counter, total, average;
    char grade;
    final int NUMBER_OF_GRADES = 3;

    boolean inputOK;                      // Used for QA (Quality Assurance)


    // initialization phase
    total = 0;
    counter = 1;

    // processing phase
    while ( counter <= NUMBER_OF_GRADES ) {
      inputOK = true;                      // Assume the input is valid

      // Prompt for a grade
      //System.out.print( "Enter letter grade: " );
      //grade = System.in.read();
      System.out.print( "Enter letter grade: " );
      grade = ConsoleInput.readLineChar();  // A kinder, gentler method

      // Output the grade that was entered
      System.out.println("You entered: " + grade);

      // Process the grade
      if ( grade == 'A' )
         total = total + 4;
      else if ( grade == 'B' )
         total = total + 3;
      else if ( grade == 'C' )
         total = total + 2;
      else if ( grade == 'D' )
         total = total + 1;

      // Check for 'F' for complete validation
      else if ( grade == 'F' )
      {
        total = total + 0;
      }
      else
      {
        inputOK = false;            // input is not 'A' 'B' 'C' 'D' or 'F'
      }  // end of if else structure

      // increment counter only if the input is valid
      if (inputOK) {
        counter++;
      }
      else  // invalid input -- print an error message
      {
        System.out.println("Valid grades are A, B, C, D, or F. The input has been" +
                           " ingored");
        System.out.println("grade: " + grade + " inputOk: " + inputOK);
      }


      /**************************************************
      //System.in.skip( 2 );    // skip the carriage return newline characters
      grade = System.in.read();
      System.out.print("End of the line -  first 'char': " + grade);
      grade = System.in.read();
      System.out.println(" second 'char': " + grade);
      *****************************/

      //counter = counter + 1;
    }

    // termination phase
    average = total / NUMBER_OF_GRADES;      // integer division
    double daverage = (float) total / NUMBER_OF_GRADES;  // floating point division

    System.out.println( "Class average is " + average );
    System.out.println( "Decimal average is " + daverage );
  }
}
