
/**
 * Title:        Chapter 19, "Java Data Structures"
 * Description:  Examples
 * Copyright:    Copyright (c) 2000
 * Company:      Armstrong Atlantic State University
 * @author Y. Daniel Liang
 * @version 1.0
 */

// AssignGradeUsingVector.java: Assign grade
package chapter19;

import java.util.*;
import chapter2.MyInput;

public class AssignGradeUsingVector
{
  // Main method
  public static void main(String[] args)
  {
    Vector scoreVector = new Vector(); // Vector to hold scores
    double best = 0; // The best score
    char grade; // The grade

    // Read scores and find the best score
    System.out.println("Please enter scores. " +
      "A negative score terminates input.");
    do
    {
      System.out.print("Please enter a new score: ");
      double score = MyInput.readDouble();

      if (score < 0) break;

      // Add the score into the vector
      scoreVector.addElement(new Double(score));

      // Find the best score
      if (score > best)
        best = score;
    } while (true);

    System.out.println("There are total " + scoreVector.size() +
      " students ");

    // Assign and display grades
    for (int i=0; i<scoreVector.size(); i++)
    {
      // Retrieve an element from the vector
      Double doubleObject = (Double)(scoreVector.elementAt(i));

      // Get the score
      double score = doubleObject.doubleValue();

      if (score >= best - 10)
        grade = 'A';
      else if (score >= best - 20)
        grade = 'B';
      else if (score >= best - 30)
        grade = 'C';
      else if (score >= best - 40)
        grade = 'D';
      else
        grade = 'F';

      System.out.println("Student " + i + " score is " + score +
        " and grade is " + grade);
    }
  }
}