Untitled
unknown
plain_text
3 years ago
2.5 kB
10
Indexable
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ExamGrader {
static class ExamAnswer {
public int question;
public String answer;
}
static class Exam {
public String id;
public String name;
public String type;
public List<ExamAnswer> answers;
}
static class Student {
public String id;
public String name;
public String surname;
public Exam exam;
}
static class ExamResult {
public Student student;
public int score;
}
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Usage: java ExamGrader exam_folder answer_key");
System.exit(1);
}
String examFolder = args[0];
String answerKeyFile = args[1];
ObjectMapper objectMapper = new ObjectMapper();
// Read answer key
Exam answerKey = null;
try {
answerKey = objectMapper.readValue(new File(answerKeyFile), Exam.class);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
// Read student answers
List<Student> students = new ArrayList<>();
File folder = new File(examFolder);
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile()) {
try {
Student student = objectMapper.readValue(file, Student.class);
students.add(student);
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Grade exams
List<ExamResult> results = new ArrayList<>();
for (Student student : students) {
ExamResult result = new ExamResult();
result.student = student;
int score = 0;
for (int i = 0; i < student.exam.answers.size(); i++) {
ExamAnswer studentAnswer = student.exam.answers.get(i);
ExamAnswer correctAnswer = answerKey.answers.get(i);
if (studentAnswer.answer.equals(correctAnswer.answer)) {
Editor is loading...