Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.PrintStream;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.IntSummaryStatistics;
- import java.util.List;
- import java.util.stream.IntStream;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class QuizTest {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- Quiz quiz = new Quiz();
- int questions = Integer.parseInt(sc.nextLine());
- for (int i=0;i<questions;i++) {
- try {
- quiz.addQuestion(sc.nextLine());
- } catch (InvalidOperationException e) {
- System.out.println(e.getMessage());
- }
- }
- List<String> answers = new ArrayList<>();
- int answersCount = Integer.parseInt(sc.nextLine());
- for (int i=0;i<answersCount;i++) {
- answers.add(sc.nextLine());
- }
- int testCase = Integer.parseInt(sc.nextLine());
- if (testCase==1) {
- quiz.printQuiz(System.out);
- } else if (testCase==2) {
- try {
- quiz.answerQuiz(answers, System.out);
- } catch (InvalidOperationException e) {
- System.out.println(e.getMessage());
- }
- } else {
- System.out.println("Invalid test case");
- }
- }
- }
- class InvalidOperationException extends Exception{
- public InvalidOperationException(String message) {
- super(message);
- }
- }
- abstract class Question implements Comparable<Question> {
- private String name;
- private int points;
- private Type type;
- public Question(String name, int points) {
- this.name = name;
- this.points = points;
- }
- public String getName() {
- return name;
- }
- public int getPoints() {
- return points;
- }
- public abstract String getAnswer();
- public abstract Type getType();
- }
- class MultipleChoiceQuestion extends Question{
- private char answer;
- public MultipleChoiceQuestion(String name, int points, char answer) {
- super(name, points);
- this.answer=answer;
- }
- public String getAnswer() {
- return String.valueOf(answer);
- }
- @Override
- public Type getType() {
- return Type.MC;
- }
- @Override
- public String toString() {
- return String.format("Multiple Choice Question: %s Points %d Answer: %c",super.getName(),super.getPoints(),answer);
- }
- @Override
- public int compareTo(Question o) {
- return Integer.compare(super.getPoints(),o.getPoints());
- }
- }
- class TrueFalseQuestion extends Question{
- private boolean answer;
- public TrueFalseQuestion(String name, int points,boolean answer) {
- super(name, points);
- this.answer=answer;
- }
- public String getAnswer() {
- return String.valueOf(answer);
- }
- @Override
- public Type getType() {
- return Type.TF;
- }
- @Override
- public String toString() {
- return String.format("True/False Question: %s Points: %d Answer: %s",super.getName(),super.getPoints(),answer);
- }
- @Override
- public int compareTo(Question o) {
- return Integer.compare(super.getPoints(),o.getPoints());
- }
- }
- enum Type {
- MC,
- TF
- }
- class Quiz {
- List<Question> questions;
- public Quiz() {
- this.questions = new ArrayList<>();
- }
- public int checkAnswer(char c){
- return "ABCDE".indexOf(c);
- }
- public void addQuestion(String s) throws InvalidOperationException {
- String[] parts= s.split(";");
- String name= parts[1];
- int points= Integer.parseInt(parts[2]);
- String answer= parts[3];
- if(parts[0].equals("MC")){
- if(checkAnswer(answer.charAt(0))==-1)
- throw new InvalidOperationException(answer.charAt(0)+" is not allowed option for this question");
- questions.add(new MultipleChoiceQuestion(name,points,answer.charAt(0)));
- }
- else{
- questions.add(new TrueFalseQuestion(name,points,Boolean.parseBoolean(parts[3])));
- }
- }
- public void printQuiz(PrintStream out) {
- PrintWriter printWriter = new PrintWriter(out);
- questions.stream().sorted(Comparator.reverseOrder()).forEach(i->printWriter.println(i));
- printWriter.flush();
- }
- public void answerQuiz(List<String> answers, PrintStream out) throws InvalidOperationException {
- PrintWriter printWriter = new PrintWriter(out);
- if(answers.size()!= questions.size())
- throw new InvalidOperationException("Answers and questions must be of same length!");
- double totalPoints=0;
- for(int i=0; i<answers.size();i++){
- if(questions.get(i).getAnswer().equals(answers.get(i))) {
- printWriter.println(String.format("%d. %.2f", i+1,(double) questions.get(i).getPoints()));
- totalPoints += questions.get(i).getPoints();
- }
- else{
- if(questions.get(i).getType().equals(Type.MC)){
- printWriter.println(String.format("%d. %.2f",i+1,(double) questions.get(i).getPoints()*0.2*(-1)));
- totalPoints-=questions.get(i).getPoints()*0.2;
- }
- else{
- printWriter.println(String.format("%d. 0.00",i+1));
- }
- }
- }
- printWriter.println(String.format("Total points: %.2f",totalPoints));
- printWriter.flush();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement