001package edu.pdx.cs410J.grader;
002
003import edu.pdx.cs410J.grader.gradebook.Grade;
004import edu.pdx.cs410J.grader.gradebook.GradeBook;
005import edu.pdx.cs410J.grader.gradebook.Student;
006import edu.pdx.cs410J.grader.gradebook.XmlDumper;
007
008import java.io.*;
009import java.util.*;
010
011/**
012 * Quicky program that parses a text file containing student's grades.
013 */
014public class ParseTextFile {
015  private static PrintWriter err = new PrintWriter(System.err, true);
016
017  /**
018   * Prints usage information for this program
019   */
020  private static void usage() {
021    err.println("\njava ParseTextFile textFile courseName xmlFile");
022    err.println("\n");
023    System.exit(1);
024  }
025
026  public static void main(String[] args) {
027    if (args.length < 3) {
028      err.println("** Missing command line arguments");
029      usage();
030    }
031    
032    String textFile = args[0];
033    String courseName = args[1];
034    String xmlFile = args[2];
035
036    // Make a grade book
037    GradeBook book = new GradeBook(courseName);
038
039    // Parse the text file
040    BufferedReader in = null;
041    try {
042      in = new BufferedReader(new FileReader(textFile));
043
044      List<String> assignments = new ArrayList<String>();
045
046      // First line of file contains names of assignments
047      String line = in.readLine();
048      StringTokenizer st = new StringTokenizer(line);
049      st.nextToken();    // Skip "id"
050      while (st.hasMoreTokens()) {
051        String assignment = st.nextToken();
052        assignments.add(assignment);
053      }
054
055      line = in.readLine();
056      while (line != null) {
057        st = new StringTokenizer(line);
058        String studentName = st.nextToken();
059        Student student =
060          book.getStudent(studentName).orElseThrow(() -> new IllegalStateException("No student with id " + studentName));
061
062        for (int i = 0; st.hasMoreTokens(); i++) {
063          String s = st.nextToken();
064          String assignment = (String) assignments.get(i);
065          double score;
066
067          if (s.equals("I")) {
068            score = Grade.INCOMPLETE;
069
070          } else {
071            try {
072              score = Double.parseDouble(s);
073            } catch (NumberFormatException ex) {
074              err.println("** Bad number: " + s);
075              System.exit(1);
076              score = -4.2;
077            }
078          }
079
080          Grade grade = new Grade(assignment, score);
081          student.setGrade(assignment, grade);
082        }
083
084        line = in.readLine();
085      }
086    } catch (IOException ex) {
087      ex.printStackTrace();
088      System.exit(1);
089    }
090
091    // Save grade book to XML file
092    try {
093      XmlDumper dumper = new XmlDumper(xmlFile);
094      dumper.dump(book);
095
096    } catch (IOException ex) {
097      err.println("** Error while writing XML file: " + ex);
098      System.exit(1);
099    }
100  }
101}