Advertisement
dzocesrce

[NP] Quiz

Apr 14th, 2025
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.61 KB | None | 0 0
  1. import java.io.PrintStream;
  2. import java.io.PrintWriter;
  3. import java.util.ArrayList;
  4. import java.util.Comparator;
  5. import java.util.IntSummaryStatistics;
  6. import java.util.List;
  7. import java.util.stream.IntStream;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Scanner;
  11.  
  12. public class QuizTest {
  13.     public static void main(String[] args) {
  14.  
  15.         Scanner sc = new Scanner(System.in);
  16.  
  17.         Quiz quiz = new Quiz();
  18.  
  19.         int questions = Integer.parseInt(sc.nextLine());
  20.  
  21.         for (int i=0;i<questions;i++) {
  22.             try {
  23.                 quiz.addQuestion(sc.nextLine());
  24.             } catch (InvalidOperationException e) {
  25.                 System.out.println(e.getMessage());
  26.             }
  27.         }
  28.  
  29.         List<String> answers = new ArrayList<>();
  30.  
  31.         int answersCount =  Integer.parseInt(sc.nextLine());
  32.  
  33.         for (int i=0;i<answersCount;i++) {
  34.             answers.add(sc.nextLine());
  35.         }
  36.  
  37.         int testCase = Integer.parseInt(sc.nextLine());
  38.  
  39.         if (testCase==1) {
  40.             quiz.printQuiz(System.out);
  41.         } else if (testCase==2) {
  42.             try {
  43.                 quiz.answerQuiz(answers, System.out);
  44.             } catch (InvalidOperationException e) {
  45.                 System.out.println(e.getMessage());
  46.             }
  47.         } else {
  48.             System.out.println("Invalid test case");
  49.         }
  50.     }
  51. }
  52.  
  53. class InvalidOperationException extends Exception{
  54.     public InvalidOperationException(String message) {
  55.         super(message);
  56.     }
  57. }
  58.  
  59. abstract class Question implements Comparable<Question> {
  60.     private String name;
  61.     private int points;
  62.     private Type type;
  63.  
  64.     public Question(String name, int points) {
  65.         this.name = name;
  66.         this.points = points;
  67.     }
  68.  
  69.     public String getName() {
  70.         return name;
  71.     }
  72.  
  73.     public int getPoints() {
  74.         return points;
  75.     }
  76.  
  77.     public abstract String getAnswer();
  78.  
  79.     public abstract Type getType();
  80.  
  81. }
  82.  
  83. class MultipleChoiceQuestion extends Question{
  84.     private char answer;
  85.     public MultipleChoiceQuestion(String name, int points, char answer) {
  86.         super(name, points);
  87.         this.answer=answer;
  88.     }
  89.  
  90.     public String getAnswer() {
  91.         return String.valueOf(answer);
  92.     }
  93.  
  94.     @Override
  95.     public Type getType() {
  96.         return Type.MC;
  97.     }
  98.  
  99.     @Override
  100.     public String toString() {
  101.         return String.format("Multiple Choice Question: %s Points %d Answer: %c",super.getName(),super.getPoints(),answer);
  102.     }
  103.  
  104.     @Override
  105.     public int compareTo(Question o) {
  106.         return Integer.compare(super.getPoints(),o.getPoints());
  107.     }
  108. }
  109.  
  110. class TrueFalseQuestion extends Question{
  111.     private boolean answer;
  112.  
  113.     public TrueFalseQuestion(String name, int points,boolean answer) {
  114.         super(name, points);
  115.         this.answer=answer;
  116.     }
  117.  
  118.     public String getAnswer() {
  119.         return String.valueOf(answer);
  120.     }
  121.  
  122.     @Override
  123.     public Type getType() {
  124.         return Type.TF;
  125.     }
  126.  
  127.     @Override
  128.     public String toString() {
  129.         return String.format("True/False Question: %s Points: %d Answer: %s",super.getName(),super.getPoints(),answer);
  130.     }
  131.  
  132.     @Override
  133.     public int compareTo(Question o) {
  134.         return Integer.compare(super.getPoints(),o.getPoints());
  135.     }
  136. }
  137.  
  138. enum Type {
  139.     MC,
  140.     TF
  141. }
  142.  
  143.  
  144. class Quiz {
  145.     List<Question> questions;
  146.  
  147.     public Quiz() {
  148.         this.questions = new ArrayList<>();
  149.     }
  150.  
  151.     public int checkAnswer(char c){
  152.         return "ABCDE".indexOf(c);
  153.     }
  154.  
  155.     public void addQuestion(String s) throws InvalidOperationException {
  156.         String[] parts= s.split(";");
  157.         String name= parts[1];
  158.         int points= Integer.parseInt(parts[2]);
  159.         String answer= parts[3];
  160.         if(parts[0].equals("MC")){
  161.             if(checkAnswer(answer.charAt(0))==-1)
  162.                 throw new InvalidOperationException(answer.charAt(0)+" is not allowed option for this question");
  163.             questions.add(new MultipleChoiceQuestion(name,points,answer.charAt(0)));
  164.         }
  165.         else{
  166.             questions.add(new TrueFalseQuestion(name,points,Boolean.parseBoolean(parts[3])));
  167.         }
  168.     }
  169.  
  170.     public void printQuiz(PrintStream out) {
  171.         PrintWriter printWriter = new PrintWriter(out);
  172.  
  173.         questions.stream().sorted(Comparator.reverseOrder()).forEach(i->printWriter.println(i));
  174.         printWriter.flush();
  175.     }
  176.  
  177.  
  178.  
  179.     public void answerQuiz(List<String> answers, PrintStream out) throws InvalidOperationException {
  180.         PrintWriter printWriter = new PrintWriter(out);
  181.         if(answers.size()!= questions.size())
  182.             throw new InvalidOperationException("Answers and questions must be of same length!");
  183.         double totalPoints=0;
  184.         for(int i=0; i<answers.size();i++){
  185.             if(questions.get(i).getAnswer().equals(answers.get(i))) {
  186.                 printWriter.println(String.format("%d. %.2f", i+1,(double) questions.get(i).getPoints()));
  187.                 totalPoints += questions.get(i).getPoints();
  188.             }
  189.             else{
  190.                 if(questions.get(i).getType().equals(Type.MC)){
  191.                     printWriter.println(String.format("%d. %.2f",i+1,(double) questions.get(i).getPoints()*0.2*(-1)));
  192.                     totalPoints-=questions.get(i).getPoints()*0.2;
  193.                 }
  194.                 else{
  195.                     printWriter.println(String.format("%d. 0.00",i+1));
  196.                 }
  197.  
  198.             }
  199.         }
  200.         printWriter.println(String.format("Total points: %.2f",totalPoints));
  201.         printWriter.flush();
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement