001package edu.pdx.cs.joy.grader;
002
003import edu.pdx.cs.joy.grader.gradebook.*;
004
005import java.io.File;
006import java.io.PrintStream;
007import java.util.Iterator;
008import java.util.SortedSet;
009import java.util.TreeSet;
010
011/**
012 * This program sorts the scores for a given assignment and dumps them
013 * to standard out.
014 */
015public class DumpScores {
016
017  /**
018   * Inner class that represents a score/Student tuple
019   */
020  private static class Tuple implements Comparable {
021    private double score;
022    private Student student;
023
024    Tuple(Student student, double score) {
025      this.student = student;
026      this.score = score;
027    }
028
029    public int compareTo(Object o) {
030      Tuple other = (Tuple) o;
031      if (this.score == other.score) {
032        return this.student.getId().compareTo(other.student.getId());
033
034      } else if (this.score > other.score) {
035        return -1;
036
037      } else if (this.score < other.score) {
038        return 1;
039
040      } else {
041//          assert this.score == other.score;
042        return 0;
043      }
044    }
045
046    public String toString() {
047      return this.student + ": " + this.score;
048    }
049    
050  }
051
052  //////////////////////  Main Program  ///////////////////////
053
054  private static PrintStream out = System.out;
055  private static PrintStream err = System.err;
056
057  /**
058   * Prints usage information about this program
059   */
060  private static void usage(String s) {
061    err.println("\n** " + s + "\n");
062    err.println("usage: java DumpScores xmlFile assignment");
063    err.println("");
064    err.println("Dumps the grades for a given assignment to " +
065                "standard out");
066    err.println("");
067    System.exit(1);
068  }
069
070  public static void main(String[] args) throws Throwable {
071    String xmlFileName = null;
072    String assignmentName = null;
073
074    for (int i = 0; i < args.length; i++) {
075      if (xmlFileName == null) {
076        xmlFileName = args[i];
077
078      } else if (assignmentName == null) {
079        assignmentName = args[i];
080
081      } else {
082        usage("Extraneous command line: " + args[i]);
083      }
084    }
085
086    if (xmlFileName == null) {
087      usage("Missing XML file");
088
089    } else if (assignmentName == null) {
090      usage("Missing assignment");
091    }
092
093    File xmlFile = new File(xmlFileName);
094    if (!xmlFile.exists()) {
095      usage("File \"" + xmlFileName + "\" does not exist");
096    }
097
098    GradeBook book = (new XmlGradeBookParser(xmlFile)).parse();
099    Assignment assign = book.getAssignment(assignmentName);
100    if (assign == null) {
101      usage("No such assignment \"" + assignmentName + "\"");
102    }
103
104    // Maps score to the student
105    SortedSet<Tuple> scores = new TreeSet<Tuple>();
106
107    book.forEachStudent(student -> {
108      Grade grade = student.getGrade(assign.getName());
109      if (grade != null) {
110        double score = grade.getScore();
111        scores.add(new Tuple(student, score));
112      }
113    });
114
115    out.println("\nGrades for " + assign + ": " +
116                assign.getDescription());
117
118    Iterator iter = scores.iterator();
119    while (iter.hasNext()) {
120      out.println("  " + iter.next());
121    }
122
123    out.println("");
124  }
125
126}