/****************************************
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
*****************************************/

// imports for basic I/O from the console
import java.io.*;
import java.util.*;

// class specification
public class AverageB {
  public static void main(String[] args) {

    // Initialize
    int counter = 1, total = 0, average;
    char grade;                           // Better data representation
    final int NUMBER_OF_GRADES = 3;

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

    // processing phase
    // While it's not the end of data
    while ( counter <= NUMBER_OF_GRADES)
    {
      inputOK = true;                      // Assume the input is valid

      // Prompt for a grade
      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 grade
      if ( grade == 'A' )  // start of if else structure
      {
        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);
      }

    }  // End of processing loop

    // termination phase
    // Preform the wrap-up processing
    average = total / NUMBER_OF_GRADES;      // integer division
    double daverage = (double) total / NUMBER_OF_GRADES;  // floating point division

    // Output any final results
    System.out.println( "Class average is " + average );
    System.out.println( "Decimal average is " + daverage );

  } // end main
} // end class