import java.io.*;

public class reprtcrd
{ 

  public static void main(String[] args)
  {   

    String RightAnswers = GetRightAnswers();
    char[] RightAnswersArray = MakeRightAnswerArray(RightAnswers);
    
    String[] StudentsAnswerArray = MakeStudentsAnswerArray();
    String[] StudentIDArray = MakeStudentIDArray();

    int[] StudentGradeArray = MakeStudentGradeArray(RightAnswersArray, StudentsAnswerArray);

    int best = GetBestScore(StudentGradeArray);

    String[] StudentsScoreArray = MakeStudentsScoreArray(best, StudentGradeArray);

    PrintTable(StudentIDArray,StudentsScoreArray,StudentGradeArray);

  }

  public static String GetRightAnswers()
  {
    String CorrectAnswers = new String();    

    try
    {
      InputStreamReader inStream = new InputStreamReader(System.in);
      BufferedReader inConsole = new BufferedReader(inStream);

      System.out.print("\nEnter Correct Answers (10) T or F: ");

      while(CorrectAnswers.length() < 10)
      {  
        
        CorrectAnswers += inConsole.readLine();
        
        if (CorrectAnswers.length() < 10)
        {
          int AnswersLeft = 10 - CorrectAnswers.length();
          System.out.print("Please Enter " + AnswersLeft + " More Answers: ");
        }

        if (CorrectAnswers.length() > 10)
        {  
          System.out.print("You have entered too many answers. Please only");
          System.out.println(" enter 10.");
          CorrectAnswers = "";
          System.out.print("\nEnter Correct Answers (10) T or F: ");
        }

      }

      inConsole.close();
    }

    catch (IOException ex)
    {
      System.out.println(ex.getMessage());
      ex.printStackTrace();
    } 

    return CorrectAnswers;

  }

  public static char[] MakeRightAnswerArray(String RightAnswers)
  {
    char[] RightAnswersArray = new char[10];    

    for (int x = 0; x < RightAnswers.length() - 1; x++)
    {
      if(RightAnswers.charAt(x) == 'F' || RightAnswers.charAt(x) == 'T')
        RightAnswersArray[x] = RightAnswers.charAt(x);

      else
      {  
        System.out.print("Answer: " + RightAnswers.charAt(x) + " at location ");
        System.out.print("RightAnswersArray[" + x + "] is invalid. \n(Grades ");
        System.out.println("could be flawed.)");
      }

    } 

    return RightAnswersArray;

  } 

  public static String[] MakeStudentsAnswerArray()
  {
    
    String[] StudentsAnswerArray = new String[14];

    try
    {
      
      FileReader inStream = new FileReader("bubsheet.txt");
      BufferedReader inFile = new BufferedReader(inStream);

      String StudentAnswers = inFile.readLine();

      for (int x = 0; x <=13; x++)
      {
        String StudentAnswerSubString = StudentAnswers.substring(6,17);
        StudentsAnswerArray[x] = StudentAnswerSubString;
        StudentAnswers = inFile.readLine();
      }

    }
    
    catch (IOException ex)
    {
      System.out.println(ex.getMessage());
      ex.printStackTrace();
    } 

    return StudentsAnswerArray;

  }

  public static String[] MakeStudentIDArray()
  {
    String[] StudentIDArray = new String[14];

    try
    {
      
      FileReader inStream = new FileReader("bubsheet.txt");
      BufferedReader inFile = new BufferedReader(inStream);

      String StudentID = inFile.readLine();

      for (int x = 0; x <= 13; x++)
      {
        String StudentIDSubString = StudentID.substring(0,4);
        StudentIDArray[x] = StudentIDSubString;
        StudentID = inFile.readLine();
      }

    }
    
    catch (IOException ex)
    {
      System.out.println(ex.getMessage());
      ex.printStackTrace();
    } 

    return StudentIDArray;

  }

  public static int[] MakeStudentGradeArray(char[] RightAnswersArray, String[] StudentsAnswerArray)
  {
    
    int[] StudentGradeArray = new int[14];
    
    int grade = 0;    

    for (int x = 0; x < 14; x++)
    {
      grade = 0;      

      String StudentAnswerString = StudentsAnswerArray[x];
      
      char[] StudentAnswerArray = MakeStudentAnswerArray(StudentAnswerString);

      for (int z = 0; z < 10; z++)
      {
        if (RightAnswersArray[z] == StudentAnswerArray[z])
          grade++;
      }
    
      StudentGradeArray[x] = grade;

      //System.out.println(grade);

    }

    return StudentGradeArray;

  }

  public static char[] MakeStudentAnswerArray(String StudentAnswerString)
  {
    char[] StudentAnswerArray = new char[10];    

    for (int x = 0; x < 10; x++)
    {
      StudentAnswerArray[x] = StudentAnswerString.charAt(x);
    } 

    return StudentAnswerArray;

  }

  public static int GetBestScore(int[] StudentGradeArray)
  {
    int best = 0;
    
    for (int x = 0; x < 14; x++)
    {
      if (StudentGradeArray[x] > best)
        best = StudentGradeArray[x];
    }
   
    return best;

  }

  public static String[] MakeStudentsScoreArray(int best, int[] StudentGradeArray)
  {
    String[] StudentsScoreArray = new String[14];    

    for (int x = 0; x < 14; x++)
    {
      int score = best - StudentGradeArray[x]; 
     
      switch (score)
      {
        case 0:
        case 1:
          StudentsScoreArray[x] = "A";
          break;

        case 2:
        case 3:
          StudentsScoreArray[x] = "C";
          break;

        default:
          StudentsScoreArray[x] = "F";
      }
       
    }
  
    return StudentsScoreArray; 
 
  }
        
  public static void PrintTable(String[] StudentIDArray,String[]                                                                 StudentsScoreArray,int[] StudentGradeArray)
  {
    System.out.println();

    System.out.println("Student ID    Score    Grade\n");

    for (int x = 0; x < 14; x++)
    {  
      System.out.print("   " + StudentIDArray[x] + "         ");
      System.out.print(StudentsScoreArray[x] + "       ");
      
      if(StudentGradeArray[x] < 10)
        System.out.print(" ");

      System.out.println(StudentGradeArray[x]);    

    }
  }

}