001package edu.pdx.cs.joy.grader;
002
003import edu.pdx.cs.joy.grader.gradebook.*;
004
005import java.io.File;
006import java.io.PrintStream;
007
008/**
009 * This program subtracts some number of points off of each student's
010 * grade for a given project.  It is used when I forget to take the
011 * POA into account when grading.
012 */
013public class AdjustProjectGrade {
014  private static PrintStream err = System.err;
015  
016  /**
017   * Prints usage information about this program
018   */
019  private static void usage(String s) {
020    err.println("\n** " + s + "\n");
021    err.println("usage: java AdjustProjectGrade xmlFile proj points");
022    err.println("  xmlFile  XML file containing the grade book");
023    err.println("  proj     The project from which to deduct points");
024    err.println("  points   The number of points to deduct");
025    err.println("");
026    err.println("This program deducts a number of points off of " +
027                "each students grade for a given project.");
028    err.println("");
029    System.exit(1);
030  }
031
032  public static void main(String[] args) throws Throwable {
033    String fileName = null;
034    String proj = null;
035    String pointsString = null;
036
037    for (String arg : args) {
038      if (fileName == null) {
039        fileName = arg;
040
041      } else if (proj == null) {
042        proj = arg;
043
044      } else if (pointsString == null) {
045        pointsString = arg;
046
047      } else {
048        usage("Spurious command line: " + arg);
049      }
050    }
051
052    if (fileName == null) {
053      usage("Missing file name");
054      return;
055    }
056
057    if (proj == null) {
058      usage("Missing project");
059    }
060
061    if (pointsString == null) {
062      usage("Missing points");
063    }
064
065    double points;
066    try {
067      points = Double.parseDouble(pointsString);
068
069    } catch (NumberFormatException ex) {
070      usage("Invalid points: " + pointsString);
071      return;
072    }
073
074    File file = new File(fileName);
075    if (!file.exists()) {
076      usage("File \"" + file + "\" does not exist");
077    }
078
079    XmlGradeBookParser parser = new XmlGradeBookParser(file);
080    GradeBook book = parser.parse();
081    
082    Assignment assign = book.getAssignment(proj);
083    if (assign == null) {
084      usage("No such assignment: " + proj);
085      return;
086    }
087
088    if (assign.getType() != Assignment.AssignmentType.PROJECT) {
089      usage("Assignment \"" + proj + "\" is not a project");
090    }
091
092    adjustGradeForEachStudent(book, proj, points);
093
094    XmlDumper dumper = new XmlDumper(file);
095    dumper.dump(book);
096  }
097
098  private static void adjustGradeForEachStudent(GradeBook book, String projectName, double adjustment) {
099    book.forEachStudent((Student student) -> {
100      Grade grade = student.getGrade(projectName);
101      if (grade != null) {
102        grade.setScore(grade.getScore() - adjustment);
103      }
104    });
105  }
106
107}