if you want me to compile it into a runnable lmk, idk if you even want this thing anymore
if you do use it i will give you 1 warning, you can overwrite just about any file with this i think so be careful what you go saving it as lol
i probably coulda added a save as new feature instead of just copying the listener again but tbh i did not expect to spend so long playing around with this thing
shit... for that matter i should have saved all the student summaries individually in their own files and made you a teachers summary with class stats
Code
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Created by user on 3/19/2016.
*/
public class JSPrequest extends JPanel implements ActionListener {
static File keyInFile;
static File studentInFile;
static File summaryFile;
JButton openKeyButton, openStudentInButton, saveSummaryButton;
static JTextArea log;
JFileChooser fc;
static int numOfSections=0;
public JSPrequest(){
super(new BorderLayout());
log = new JTextArea(5,20);
log.setMargin(new Insets(5,5,5,5));
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);
fc = new JFileChooser();
openKeyButton = new JButton("Key File");
openKeyButton.addActionListener(this);
openStudentInButton = new JButton("Student Answers");
openStudentInButton.addActionListener(this);
saveSummaryButton = new JButton("Save it");
saveSummaryButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(openKeyButton);
buttonPanel.add(openStudentInButton);
buttonPanel.add(saveSummaryButton);
add(buttonPanel, BorderLayout.PAGE_START);
add(logScrollPane, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == openKeyButton) {
int returnVal = fc.showOpenDialog(JSPrequest.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
keyInFile = fc.getSelectedFile();
log.append("Answer Key SET: " + keyInFile.getName() + "\n");
} else {
log.append("Open command cancelled by user.\n");
}
log.setCaretPosition(log.getDocument().getLength());
} else if (e.getSource() == openStudentInButton) {
int returnVal = fc.showSaveDialog(JSPrequest.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
studentInFile = fc.getSelectedFile();
log.append("Student Answers SET: " + studentInFile.getName() + "\n");
} else {
log.append("Open command cancelled by user.\n");
}
log.setCaretPosition(log.getDocument().getLength());
}else if (e.getSource() == saveSummaryButton) {
int returnVal = fc.showSaveDialog(JSPrequest.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
summaryFile = fc.getSelectedFile();
log.append("Saved: " + summaryFile.getName() + ".\n");
summarizeIt();
} else {
log.append("Save command cancelled by user.\n");
}
log.setCaretPosition(log.getDocument().getLength());
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("TestGrader");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JSPrequest());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
createAndShowGUI();
}
public static ArrayList<ArrayList<String>> getCorrectAnswers(File keyInFile){
ArrayList<ArrayList<String>> answers = new ArrayList<>();
try {
Scanner input = new Scanner(keyInFile);
int n = 0;
while (input.hasNextLine()) {
answers.add(new ArrayList<>());
String answer = input.nextLine();
if (answer.equals("new_section")) {
n++;
numOfSections++;
} else {
answers.get(n).add(answer);
}
}
} catch (FileNotFoundException e) {
log.append("no file found");
}
return answers;
}
public static void summarizeIt(){
ArrayList<ArrayList<String>> answers = getCorrectAnswers(keyInFile);
ArrayList<ArrayList<ArrayList<String>>> studentAnswers = new ArrayList<>();
ArrayList<String> studentNames = new ArrayList<>();
try {
Scanner input = new Scanner(studentInFile);
int numOfStudents = 0;
while (input.hasNextLine()) {
String name = input.nextLine();
studentNames.add(name);
studentAnswers.add(new ArrayList<>());
for (int i = 0; i < answers.size(); i++) {
studentAnswers.get(numOfStudents).add(new ArrayList<>());
for (int j = 0; j < answers.get(i).size(); j++) {
String answer = input.nextLine();
studentAnswers.get(numOfStudents).get(i).add(answer);
}
}
numOfStudents++;
}
} catch (FileNotFoundException e) {
log.append("no file found");
}
try {
PrintWriter printer = new PrintWriter(summaryFile);
printer.println("\nSUMMARY");
printer.println();
int rightAnswers = 0;
int totalQuestions = 0;
int rightAnswersSection = 0;
int totalQuestionsSection = 0;
for (int i = 0; i < studentNames.size(); i++) {
printer.printf("%s\'s answers: ", studentNames.get(i));
for (int j = 0; j < studentAnswers.get(i).size(); j++) {
for (int k = 0; k < studentAnswers.get(i).get(j).size(); k++) {
printer.print(studentAnswers.get(i).get(j).get(k));
}
}
printer.println();
printer.print("Correct Answers: ");
for (int j = 0; j < answers.size(); j++) {
for (int k = 0; k < answers.get(j).size(); k++) {
printer.print(answers.get(j).get(k));
}
}
printer.println();
for (int j = 0; j <= numOfSections; j++) {
printer.println();
printer.printf("Section %d: ", j+1);
printer.println();
for (int k = 0; k < answers.get(j).size(); k++) {
if (studentAnswers.get(i).get(j).get(k).equalsIgnoreCase(answers.get(j).get(k))) {
rightAnswers++;
rightAnswersSection++;
}
totalQuestions++;
totalQuestionsSection++;
}
printer.printf("\nScore: %d/%d", rightAnswersSection, totalQuestionsSection);
printer.println();
printer.printf("Percentage: %5.2f%s", (double) rightAnswersSection * 100 / (double) totalQuestionsSection, "%");
printer.println();
printer.println("Answer Analysis");
for (int k = 0; k < answers.get(j).size(); k++) {
if (!studentAnswers.get(i).get(j).get(k).equalsIgnoreCase(answers.get(j).get(k))) {
printer.printf("#%d: %s marked: %s. Correct Answer: %s.", k + 1, studentNames.get(i), studentAnswers.get(i).get(j).get(k), answers.get(j).get(k));
printer.println();
}
}
rightAnswersSection=0;
totalQuestionsSection=0;
printer.println();
}
printer.println("Overall: ");
printer.printf("Number of Sections: %d", numOfSections+1);
printer.println();
printer.printf("Total Score: %d/%d", rightAnswers, totalQuestions);
printer.println();
printer.printf("SAT score: %5.2f/%d", rightAnswers - (totalQuestions - rightAnswers) / 4.0, totalQuestions);
printer.println();
printer.println();
printer.println("-------------------------------------------------------");
printer.println();
totalQuestions = 0;
rightAnswers = 0;
}
printer.close();
} catch (FileNotFoundException e) {
log.append("no file found");
}
}
}
This post was edited by Ideophobe on Mar 20 2016 04:34am