Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.danilyanich;
- import java.io.*;
- import java.util.*;
- public class Main {
- static class Task {
- public String question;
- public List<String> answers;
- public int correct;
- public Task() {
- this.answers = new LinkedList<>();
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder("question: ").append(question).append("?\n");
- int count = 1;
- for (String answer : answers) {
- sb.append(count).append(": ").append(answer).append(".\n");
- }
- return sb.toString();
- }
- }
- static class User {
- public String surname;
- public String name;
- public int course;
- public int group;
- }
- static class Test {
- private List<Task> tasks;
- private User user;
- int size;
- int correct = 0;
- public Test(List<Task> tasks, User user) {
- this.tasks = tasks;
- this.user = user;
- size = tasks.size();
- }
- public boolean hasNextTask() {
- return !tasks.isEmpty();
- }
- public String nextTask() {
- Task task = tasks.get(0);
- tasks.remove(0);
- return task.toString();
- }
- public void setAnswer(int i) {
- if (tasks.get(0).correct == i)
- correct++;
- }
- public double getStatistics() {
- return correct / (double) size;
- }
- }
- public static File test = new File("test.txt");
- public static void main(String[] args) {
- try {
- List<Task> tasks = parseTest(test);
- System.out.println("user(surname, name, course, group): ");
- User user = parseUser(System.in);
- Test test = new Test(tasks, user);
- Scanner scanner = new Scanner(System.in);
- while (test.hasNextTask()) {
- System.out.println(test.nextTask());
- System.out.println("answer: ");
- test.setAnswer(scanner.nextInt());
- }
- System.out.println("stat: " + test.getStatistics());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private static User parseUser(InputStream in) throws Exception {
- Scanner scanner = new Scanner(in);
- User user = new User();
- try {
- user.surname = scanner.next();
- user.name = scanner.next();
- user.course = scanner.nextInt();
- user.group = scanner.nextInt();
- } catch (NoSuchElementException e) {
- throw new Exception("wrong format");
- }
- return user;
- }
- private static List<Task> parseTest(File test) throws Exception {
- List<Task> tasks = new LinkedList<>();
- Scanner input = new Scanner(test);
- try {
- while (input.hasNext()) {
- Task task = new Task();
- task.question = input.nextLine();
- while (input.hasNextLine() && !input.hasNextInt())
- task.answers.add(input.nextLine());
- task.correct = input.nextInt();
- }
- } catch (NoSuchElementException e) {
- throw new Exception("wrong file format");
- }
- return tasks;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement