Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.BufferedReader;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.stream.Collectors;
- import java.util.AbstractMap;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- class FishyQuizDetection extends Exception {
- public FishyQuizDetection(String message) {
- super(message);
- }
- }
- class Quiz {
- public static double calculatePoints(List<String> correctAnswers, List<String> givenAnswers) throws FishyQuizDetection {
- if(correctAnswers.size()!=givenAnswers.size())
- throw new FishyQuizDetection("A quiz must have same number of correct and selected answers");
- double totalPoints=0;
- for(int i=0;i<correctAnswers.size();i++){
- if(correctAnswers.get(i).equals(givenAnswers.get(i)))
- totalPoints++;
- else
- totalPoints-=0.25;
- }
- return totalPoints;
- }
- public static Map.Entry<String,Double> create(String string) {
- String[] parts = string.split(";");
- String id = parts[0];
- List<String> correctAnswers = Arrays.stream(parts[1].trim().split(",")).collect(Collectors.toList());
- List<String> givenAnswers = Arrays.stream(parts[2].trim().split(",")).collect(Collectors.toList());
- try {
- return new AbstractMap.SimpleEntry<>(id, calculatePoints(correctAnswers,givenAnswers));
- } catch (FishyQuizDetection e) {
- System.out.println(e.getMessage());
- return new AbstractMap.SimpleEntry<>("AUF", 0.0);
- }
- }
- }
- class QuizProcessor {
- Map<String ,Double> answers;
- public QuizProcessor() {
- answers= new LinkedHashMap<>();
- }
- public static Map<String, Double> processAnswers(InputStream in) {
- BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(in));
- Map<String ,Double> answers= bufferedReader.lines().map(Quiz::create).filter(i->!i.getKey().equals("AUF")).collect(Collectors.toMap(
- entry->entry.getKey(),
- entry->entry.getValue(),
- (a,b)->a,
- LinkedHashMap::new
- ));
- return answers;
- }
- }
- public class QuizProcessorTest {
- public static void main(String[] args) {
- QuizProcessor.processAnswers(System.in).forEach((k, v) -> System.out.printf("%s -> %.2f%n", k, v));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement