001package edu.pdx.cs410J.grader;
002
003import edu.pdx.cs410J.ParserException;
004import edu.pdx.cs410J.grader.gradebook.Grade;
005import edu.pdx.cs410J.grader.gradebook.GradeBook;
006import edu.pdx.cs410J.grader.gradebook.XmlDumper;
007import edu.pdx.cs410J.grader.gradebook.XmlGradeBookParser;
008
009import java.io.File;
010import java.io.FileNotFoundException;
011import java.io.IOException;
012import java.io.PrintStream;
013
014/**
015 * This is a little program that combines the grades for the midterm
016 * quiz, the dream job, and the resume into one grade.  This way
017 * people who missed this quiz are not penalized unnecessarily.
018 */
019public class FixMidterm {
020
021  public static void main(String[] args) {
022    PrintStream out = System.out;
023    PrintStream err = System.err;
024
025    if (args.length < 1) {
026      err.println("** usage: java FixMidterm gradebook");
027      System.exit(1);
028    }
029
030    String xmlFile = args[0];
031
032    GradeBook book;
033
034    File file = new File(xmlFile);
035    if (file.exists()) {
036      // Parse a grade book from the XML file
037      try {
038        XmlGradeBookParser parser = new XmlGradeBookParser(file);
039        book = parser.parse();
040
041      } catch (FileNotFoundException ex) {
042        err.println("** Could not find file: " + ex.getMessage());
043        System.exit(1);
044        return;
045
046      } catch (IOException ex) {
047        err.println("** IOException during parsing: " + ex.getMessage());
048        System.exit(1);
049        return;
050
051      } catch (ParserException ex) {
052        err.println("** Error during parsing: " + ex.getMessage());
053        System.exit(1);
054        return;
055      }
056
057    } else {
058      err.println("** File " + file + " does not exist");
059      System.exit(1);
060      return;
061    }
062
063    book.forEachStudent(student -> {
064      out.print("Fixing " + student.getId() + ": ");
065      out.flush();
066
067      double total = 0.0;
068
069      Grade quiz3 = student.getGrade("quiz3");
070      if (quiz3 != null) {
071        total += quiz3.getScore();
072      } else {
073        quiz3 = new Grade("quiz3", 0.0);
074      }
075
076      out.print("midterm = " + quiz3.getScore() + ", ");
077      out.flush();
078
079      Grade job = student.getGrade("job");
080      if (job != null) {
081        total += job.getScore();
082        quiz3.addNote("Dream job score: " + job.getScore());
083        out.print("dream job = " + job.getScore() + ", ");
084
085      } else {
086        quiz3.addNote("Missing dream job");
087      }
088
089      Grade resume = student.getGrade("resume");
090      if (resume != null) {
091        total += resume.getScore();
092        quiz3.addNote("Resume score: " + resume.getScore());
093        out.print("resume = " + resume.getScore() + ", ");
094        out.flush();
095
096      } else {
097        quiz3.addNote("Missing resume");
098      }
099
100      quiz3.setScore(total);
101      student.setGrade(quiz3.getAssignmentName(), quiz3);
102      out.println("total = " + quiz3.getScore());
103    });
104
105    // Write the grade book to the XML file
106    try {
107      XmlDumper dumper = new XmlDumper(file);
108      dumper.dump(book);
109
110    } catch (IOException ex) {
111      err.println("** Error while writing XML file: " + ex);
112      System.exit(1);
113    }    
114  }
115
116}