Guest User

Untitled

a guest
Jul 15th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.25 KB | None | 0 0
  1.  
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.util.ArrayList;
  7. import java.util.Comparator;
  8. import java.util.Scanner;
  9.  
  10. public class QuestionsHandler {
  11.  
  12.     private ArrayList<Question> questions;
  13.     private Comparator comparator;
  14.    
  15.    
  16.    
  17.     public QuestionsHandler() {
  18.         this.questions = new ArrayList<Question>();
  19.         this.comparator = null;
  20.     }
  21.  
  22.     public void setComparator(Comparator comparator) {
  23.         this.comparator = comparator;
  24.     }
  25.  
  26.     public Comparator getComparator() {
  27.         return comparator;
  28.     }
  29.    
  30.     public void setQuestions(ArrayList<Question> questions){
  31.         this.questions = questions;
  32.     }
  33.  
  34.     public ArrayList<Question> getQuestions() {
  35.         return questions;
  36.     }
  37.    
  38.     public void loadQuestions(char[] password, String finput, boolean skipDuplicates) throws Exception {
  39.         if (skipDuplicates && (comparator == null)) throw (new NullPointerException());
  40.        
  41.         //
  42.        
  43.         BufferedInputStream fin = new BufferedInputStream(new FileInputStream(finput));
  44.         byte[] ciphertext = new byte[fin.available()];
  45.         fin.read(ciphertext);
  46.         fin.close();
  47.  
  48.         byte[] cleartext = DataCipher.decryptData(ciphertext, password);
  49.        
  50.         QuestionBuilder builder = new QuestionBuilder();
  51.         Scanner scanner = new Scanner(new String(cleartext));
  52.         while (scanner.hasNextLine()) {
  53.             String[] data = scanner.nextLine().split(";;");
  54.             ArrayList<Answer> answers = new ArrayList<Answer>();
  55.            
  56.             builder.setContent(data[0]);
  57.             builder.setSubject(data[1]);
  58.             builder.setCategory(data[2]);
  59.             builder.setDifficultyLevel(Integer.parseInt(data[3]));
  60.             builder.setMultipleChoice(Boolean.parseBoolean(data[4]));
  61.            
  62.             String temp = null;
  63.             for (int i = 0; i < Integer.parseInt(data[5]); i++) {
  64.                 temp = data[(5 + 1) + i];
  65.                 if (temp.startsWith("%%")) {
  66.                     answers.add(new Answer(temp.substring(2), Boolean.TRUE, Boolean.FALSE));
  67.                 } else {
  68.                     answers.add(new Answer(temp, Boolean.FALSE, Boolean.FALSE));
  69.                 }
  70.             }
  71.            
  72.             builder.setAnswers(answers);
  73.             builder.setReference(data[data.length - 2]);
  74.             builder.setComment(data[data.length - 1]);
  75.            
  76.             Question q1 = builder.build();
  77.             if (skipDuplicates) {
  78.                 boolean flag = false;
  79.                 for (Question q2: questions) {
  80.                     if (comparator.compare(q1, q2) == 0) {
  81.                         flag = true;
  82.                         break;
  83.                     }
  84.                 }
  85.                 if (!flag) questions.add(q1);
  86.             } else {
  87.                 questions.add(q1);
  88.             }
  89.         }
  90.     }
  91.    
  92.     public void saveQuestions(char[] password, String foutput) throws Exception {
  93.         BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(foutput));
  94.         StringBuilder sb = new StringBuilder();
  95.        
  96.         for (Question q: questions) {
  97.             sb.append(q.getContent()).append(";;");
  98.             sb.append(q.getSubject()).append(";;");
  99.             sb.append(q.getCategory()).append(";;");
  100.             sb.append(String.valueOf(q.getDifficultyLevel())).append(";;");
  101.             sb.append(String.valueOf(q.isMultipleChoice())).append(";;");
  102.             sb.append(String.valueOf(q.getAnswers().size())).append(";;");
  103.            
  104.             for(Answer answer: q.getAnswers()) {
  105.                 if(answer.isCorrect()) {
  106.                     sb.append("%%").append(String.format("%s;;", answer.getContent()));
  107.                 } else {
  108.                     sb.append(String.format("%s;;", answer.getContent()));
  109.                 }
  110.             }
  111.            
  112.             sb.append(q.getReference()).append(";;");
  113.             sb.append(q.getComment()).append(";;");
  114.             sb.append("\n");
  115.         }
  116.  
  117.         fout.write(DataCipher.encrypData((new String(sb)).getBytes(), password));
  118.         fout.flush();
  119.         fout.close();
  120.     }
  121.  
  122.     public void removeDuplicates() {
  123.         if (comparator == null) throw (new NullPointerException());
  124.        
  125.         //
  126.        
  127.         ArrayList<Question> temp = new ArrayList<Question>();
  128.         for (Question q1: questions) {
  129.             boolean flag = false;
  130.             for (Question q2: temp) {
  131.                 if (comparator.compare(q1, q2) == 0) {
  132.                     flag = true;
  133.                     break;
  134.                 }
  135.             }
  136.             if (!flag) temp.add(q1);
  137.         }
  138.         this.questions = temp;
  139.     }
  140.    
  141.     public void addQuestion(Question q) {
  142.         questions.add(q);
  143.     }
  144.    
  145.     public void removeQuestion(Question q1) {
  146.         if (comparator == null) throw (new NullPointerException());
  147.        
  148.         //
  149.        
  150.         ArrayList<Question> temp = new ArrayList<Question>();
  151.         for (Question q2: questions) {
  152.             if (comparator.compare(q1, q2) != 0) temp.add(q2);
  153.         }
  154.         this.questions = temp;
  155.     }
  156.    
  157. }
Add Comment
Please, Sign In to add comment