/*This program is a more complete version of the Roster program shown in the Interitance section.
  The main purpose of the latter was to show how polymorphism is used (computeCourseGrade()).

  The purpose of this program is to show one example of how File i/o can be handled gracefully. 
  The program prompts the user for a fileName and reads all of the roster information (including grades) from
  that file.  

  Student file format:
  Student1Name
  Level [U | G]
  test1score
  test2score
  test3score
  Student2Name
  Level [U | G]
  test1score
  test2score
  test3score
  ...
  ===========================================================================================

  Notes: 
   1. This is very similar to how you may implement the file i/o in Assignment 5.  However, this version
      excludes the following (which you need in A5): NumberFormatException and ArrayIndexOutOfBoundsException
      handling.  
   2. This program relies on the fact that NUM_OF_TESTS is exactly three for each student when the file is read.
      In A5, each object may have anywhere from zero to MAX_COMMENTS associated comments, so you will have to do
      something extra to handle that.  

*/

//import needed for PrintWriter, FileReader
import java.io.*;
import java.util.Scanner;

class Roster2
{
    public static int MAXSTUDENTS = 5;
    //helper method to get a student's name

    //Given a fileName, reads the students from that file, returns # of students read.  
    //Precondition: fileName is a valid file name that can be opened for reading
    /*Note: this precondition means that we are not providing code to handle an
      IOException in this method (which *may* throw an IOException), so we have to
      put a "throws" clause in the method signature
    */
    public static int readRoster(String fileName, Student [] roster) throws IOException{

	FileReader reader = new FileReader(fileName);
	Scanner in = new Scanner(reader); 
	int [] tests = new int[Student.NUM_OF_TESTS]; //temp variables for input
	String name, level;
	Integer tempInteger;

	//read line by line
	//this assumes a student file is always in proper format
	int numStudents = 0;  
	while(in.hasNextLine()) {
	    name = in.nextLine();
	    level = in.nextLine();

	    //this is where it gets more interesting
	    //you could just read line by line, and parse each line after you've read
	    //An observation you can make, though, is that Integer has a String constructor:
	    for(int i=0; i<Student.NUM_OF_TESTS; ++i) {
		tempInteger = new Integer(in.nextLine());  //string constructor 
		tests[i] = tempInteger.intValue();
	    }

	    //done reading a student, initialize the student using data above
	    if(level.charAt(0)=='U') {
		roster[numStudents] = new UndergraduateStudent(name);
	    }
	    else {//level == G
		roster[numStudents] = new GraduateStudent(name);
	    }
	    //notice this line already uses polymorphism
	    for(int i=0; i<Student.NUM_OF_TESTS; ++i) {
		roster[numStudents].setNextTestScore(tests[i]);
	    }
	    //add the student
	    numStudents++;
	}
	//close the file
	in.close();
	return numStudents; //make sure you understand why we need to return this variable to main
    }

    //Precondition: fileName is a valid file
    //Note: does not handle possible ArrayIndexOutOfBounds
    //Exercise: add some code to main() that writes the roster to another file.
    public static void writeRoster(String fileName, Student [] roster, int numStudents) throws IOException{
	//open a file for writing--this may throw exceptions (again, would be handled elsewhere)
	PrintWriter out = new PrintWriter(fileName);

	//output in the same format as read
	for(int i=0; i<numStudents; ++i) {
	    out.println(roster[i].getName());
	    if(roster[i] instanceof GraduateStudent) {
		out.println("G");
	    }
	    else 
		out.println("U");
	    
	    for(int j=1; j<=Student.NUM_OF_TESTS; ++j) {
		out.println(roster[i].getTestScore(j));
	    }	    
	}//end file output
	
	out.close(); //crucial for output files!
    }

    
    public static void main(String [] args) {
	
	Student[] theRoster = new Student[MAXSTUDENTS]; //initially, no students
	Scanner stdin = new Scanner(System.in);
	boolean success = false;
	int numStudents = 0;
	//get students (in order) from input arguments
	do {	    
	    try {
		System.out.print("Enter input file name: ");
		String fileName = stdin.nextLine();
		numStudents = readRoster(fileName, theRoster);
		success = true;
	    }
	    catch(IOException ioe) {
		System.out.println("ERROR: incorrect file name");
	    }
	}
	while(!success);


	//Rest of program is identical to previous version
	//compute course grades 
	System.out.println(numStudents);
	for (int i=0; i<numStudents; i++)  {
	    theRoster[i].computeCourseGrade(); //this is polymorphism (why?)
	}

	//try writing the roster to a file here

	//print course grades
	for (int i=0; i<numStudents; i++)  {
	    System.out.println(theRoster[i].getName() + " has grade: " //this is not polymorphism
			       + theRoster[i].getCourseGrade());
	}
    } 
}
