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