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