Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.Scanner;
- public class QuestionsHandler {
- private ArrayList<Question> questions;
- private Comparator comparator;
- public QuestionsHandler() {
- this.questions = new ArrayList<Question>();
- this.comparator = null;
- }
- public void setComparator(Comparator comparator) {
- this.comparator = comparator;
- }
- public Comparator getComparator() {
- return comparator;
- }
- public void setQuestions(ArrayList<Question> questions){
- this.questions = questions;
- }
- public ArrayList<Question> getQuestions() {
- return questions;
- }
- public void loadQuestions(char[] password, String finput, boolean skipDuplicates) throws Exception {
- if (skipDuplicates && (comparator == null)) throw (new NullPointerException());
- //
- BufferedInputStream fin = new BufferedInputStream(new FileInputStream(finput));
- byte[] ciphertext = new byte[fin.available()];
- fin.read(ciphertext);
- fin.close();
- byte[] cleartext = DataCipher.decryptData(ciphertext, password);
- QuestionBuilder builder = new QuestionBuilder();
- Scanner scanner = new Scanner(new String(cleartext));
- while (scanner.hasNextLine()) {
- String[] data = scanner.nextLine().split(";;");
- ArrayList<Answer> answers = new ArrayList<Answer>();
- builder.setContent(data[0]);
- builder.setSubject(data[1]);
- builder.setCategory(data[2]);
- builder.setDifficultyLevel(Integer.parseInt(data[3]));
- builder.setMultipleChoice(Boolean.parseBoolean(data[4]));
- String temp = null;
- for (int i = 0; i < Integer.parseInt(data[5]); i++) {
- temp = data[(5 + 1) + i];
- if (temp.startsWith("%%")) {
- answers.add(new Answer(temp.substring(2), Boolean.TRUE, Boolean.FALSE));
- } else {
- answers.add(new Answer(temp, Boolean.FALSE, Boolean.FALSE));
- }
- }
- builder.setAnswers(answers);
- builder.setReference(data[data.length - 2]);
- builder.setComment(data[data.length - 1]);
- Question q1 = builder.build();
- if (skipDuplicates) {
- boolean flag = false;
- for (Question q2: questions) {
- if (comparator.compare(q1, q2) == 0) {
- flag = true;
- break;
- }
- }
- if (!flag) questions.add(q1);
- } else {
- questions.add(q1);
- }
- }
- }
- public void saveQuestions(char[] password, String foutput) throws Exception {
- BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(foutput));
- StringBuilder sb = new StringBuilder();
- for (Question q: questions) {
- sb.append(q.getContent()).append(";;");
- sb.append(q.getSubject()).append(";;");
- sb.append(q.getCategory()).append(";;");
- sb.append(String.valueOf(q.getDifficultyLevel())).append(";;");
- sb.append(String.valueOf(q.isMultipleChoice())).append(";;");
- sb.append(String.valueOf(q.getAnswers().size())).append(";;");
- for(Answer answer: q.getAnswers()) {
- if(answer.isCorrect()) {
- sb.append("%%").append(String.format("%s;;", answer.getContent()));
- } else {
- sb.append(String.format("%s;;", answer.getContent()));
- }
- }
- sb.append(q.getReference()).append(";;");
- sb.append(q.getComment()).append(";;");
- sb.append("\n");
- }
- fout.write(DataCipher.encrypData((new String(sb)).getBytes(), password));
- fout.flush();
- fout.close();
- }
- public void removeDuplicates() {
- if (comparator == null) throw (new NullPointerException());
- //
- ArrayList<Question> temp = new ArrayList<Question>();
- for (Question q1: questions) {
- boolean flag = false;
- for (Question q2: temp) {
- if (comparator.compare(q1, q2) == 0) {
- flag = true;
- break;
- }
- }
- if (!flag) temp.add(q1);
- }
- this.questions = temp;
- }
- public void addQuestion(Question q) {
- questions.add(q);
- }
- public void removeQuestion(Question q1) {
- if (comparator == null) throw (new NullPointerException());
- //
- ArrayList<Question> temp = new ArrayList<Question>();
- for (Question q2: questions) {
- if (comparator.compare(q1, q2) != 0) temp.add(q2);
- }
- this.questions = temp;
- }
- }
Add Comment
Please, Sign In to add comment