/**
   * Loops through all currently loaded Students and matches Anon Codes If matched then replaces
   * anon codes with Student Name
   *
   * @param assessments - ArrayList of currently loaded Assessments
   * @param students - ArrayList of all students
   */
  public void deAnnonymise(ArrayList<Assessment> assessments, ArrayList<Student> students) {
    for (Assessment a : assessments) {
      for (Result t : a.getResults()) {
        String candKey = t.getCandKey();
        // Checks if candKey is actually student number
        // If candKey is student Number, then is coursework file
        if (candKey.substring(candKey.length() - 2, candKey.length() - 1).equals("/")) {
          // Removes the end /1 or /2 after student number
          candKey = candKey.substring(0, candKey.length() - 2);
          candKey = candKey.replaceAll("#", "");

          for (Student s : students) {
            // Finds student with matching student numbers
            if (candKey.equals(s.getStudentNumber() + "")) {
              t.setCandKey(s.getStudentNumber() + "");
              s.addMarks(t.getModuleCode() + " " + t.getAssessment(), t.getMark());
            }
          }
        } else {
          // If candKey is anon code
          for (Student s : students) {
            candKey = candKey.replaceAll("#", "");
            if (candKey.equals(s.getAMC() + "")) {
              // Finds student with matching anonymous marking code replaces it with student number
              t.setCandKey(s.getStudentNumber() + "");
              t.setName(s.getName());
              s.addMarks(t.getModuleCode() + " " + t.getAssessment(), t.getMark());
            }
          }
        }
      }
    }
  }
  /**
   * Generates a JTable using results from Assessment data
   *
   * @param assessment - Assessment object
   */
  public void makeTable(Assessment assessment) {
    DefaultTableModel model =
        new DefaultTableModel() {
          @Override
          public boolean isCellEditable(int row, int column) {
            // Disables all cells from being editable
            return false;
          }
        };

    // Assigns column headings
    boolean isAnonymised = !assessment.getResults().get(0).getCandKey().contains("#");
    if (isAnonymised) {
      model.addColumn("Name");
      model.addColumn("Student Number");
    } else {
      model.addColumn("Anon Codes");
    }

    model.addColumn("#Ass");
    model.addColumn("Module Code");
    model.addColumn("Mark");
    model.addColumn("Grade");

    table = new JTable(model);
    table.setFont(new Font("Calibri", Font.BOLD, 14));

    // Sets column header look
    JTableHeader header = table.getTableHeader();
    header.setFont(new Font("Calibri", Font.BOLD, 16));
    header.setBackground(Color.black);
    header.setForeground(Color.WHITE);

    // Sets cell selection to single so only one cell is selected
    table.setCellSelectionEnabled(true);
    System.out.println("Making JTable");
    // Fetches first assessment and adds results data to model
    for (Result r : assessment.getResults()) {
      String name = r.getName();
      if (r.getName().equals("")) {
        name = r.getCandKey();
      }
      if (isAnonymised) {
        model.addRow(
            new Object[] {
              name, r.getCandKey(), r.getAssessment(), r.getModuleCode(), r.getMark(), r.getGrade()
            });
      } else {
        model.addRow(
            new Object[] {
              r.getCandKey(), r.getAssessment(), r.getModuleCode(), r.getMark(), r.getGrade()
            });
      }
    }

    table.setPreferredScrollableViewportSize(new Dimension(200, 300));
    table.setFillsViewportHeight(true);
    table.setShowGrid(false);
  }
  /**
   * Reads data from CSV file and adds to Assessment ArrayList
   *
   * @param bf - BufferedReader containing CSV file from file
   * @param assessments - ArrayList of currently loaded assessments
   * @throws IOException - Throws IOException if can't read file
   */
  public void readExamData(BufferedReader bf, ArrayList<Assessment> assessments)
      throws IOException {
    // Finds corresponding column indexes
    // Reads first line to get column headings
    String line = bf.readLine();
    String[] linesplit = line.split(",");
    int yearCol = 0, nameCol = 0, moduleCol = 0, assCol = 0, candCol = 0, markCol = 0, gradeCol = 0;
    for (int i = 0; i < linesplit.length; i++) {
      if (linesplit[i].equals("\"#Module\"") || linesplit[i].equals("#Module")) {
        moduleCol = i;
      } else if (linesplit[i].equals("\"#Ass#\"") || linesplit[i].equals("#Ass#")) {
        assCol = i;
      } else if (linesplit[i].equals("\"#Cand Key\"") || linesplit[i].equals("#Cand Key")) {
        candCol = i;
      } else if (linesplit[i].equals("\"Mark\"") || linesplit[i].equals("Mark")) {
        markCol = i;
      } else if (linesplit[i].equals("\"Grade\"") || linesplit[i].equals("Grade")) {
        gradeCol = i;
      } else if (linesplit[i].equals("\"Year\"") || linesplit[i].equals("Year")) {
        yearCol = i;
      } else if (linesplit[i].equals("\"Name\"") || linesplit[i].equals("Name")) {
        nameCol = i;
      }
    }

    // Adds records to assessments
    while ((line = bf.readLine()) != null) {
      linesplit = line.split(",");
      Result temp =
          new Result(
              linesplit[yearCol].replaceAll("\"", ""),
              linesplit[moduleCol].replaceAll("\"", ""),
              linesplit[assCol].replaceAll("\"", ""),
              linesplit[candCol].replaceAll("\"", ""),
              linesplit[nameCol].replaceAll("\"", ""),
              Integer.parseInt(linesplit[markCol]),
              linesplit[gradeCol]);
      // First checks if Assessment array is empty
      if (assessments.isEmpty()) {
        Assessment t1 = new Assessment();
        t1.addResult(temp);
        assessments.add(t1);
        // Checks if there is already an assessment object with same assessment number
        // If not make new assessment object then adds record
      } else if (!checkAllAss(temp.getAssessment(), assessments)) {
        Assessment t1 = new Assessment();
        t1.addResult(temp);
        assessments.add(t1);
      } else {
        // Since there is existing assessment object finds it, and adds record
        for (int i = 0; i < assessments.size(); i++) {
          if (assessments.get(i).getResults().get(0).getAssessment().equals(temp.getAssessment())) {
            assessments.get(i).addResult(temp);
          }
        }
      }
    }
  }
 /**
  * Loops through all Assessment Objects within Assessment ArrayList If finds existing one, that
  * matches string returns true
  *
  * @param assCode - Assessment code to check
  * @param assessments - ArrayList of assessments
  * @return true - If have existing assessment
  */
 public boolean checkAllAss(String assCode, ArrayList<Assessment> assessments) {
   for (Assessment t : assessments) {
     if (t.getResults().get(0).getAssessment().equals(assCode)) {
       // If does have assessment already
       return true;
     }
   }
   return false;
 }