Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. package com.danilyanich;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. public class Main {
  7.  
  8. static class Task {
  9. public String question;
  10. public List<String> answers;
  11. public int correct;
  12.  
  13. public Task() {
  14. this.answers = new LinkedList<>();
  15. }
  16.  
  17. @Override
  18. public String toString() {
  19. StringBuilder sb = new StringBuilder("question: ").append(question).append("?\n");
  20. int count = 1;
  21. for (String answer : answers) {
  22. sb.append(count).append(": ").append(answer).append(".\n");
  23. }
  24. return sb.toString();
  25. }
  26. }
  27.  
  28. static class User {
  29. public String surname;
  30. public String name;
  31. public int course;
  32. public int group;
  33. }
  34.  
  35. static class Test {
  36. private List<Task> tasks;
  37. private User user;
  38.  
  39. int size;
  40. int correct = 0;
  41.  
  42. public Test(List<Task> tasks, User user) {
  43. this.tasks = tasks;
  44. this.user = user;
  45. size = tasks.size();
  46. }
  47.  
  48. public boolean hasNextTask() {
  49. return !tasks.isEmpty();
  50. }
  51.  
  52. public String nextTask() {
  53. Task task = tasks.get(0);
  54. tasks.remove(0);
  55. return task.toString();
  56. }
  57.  
  58. public void setAnswer(int i) {
  59. if (tasks.get(0).correct == i)
  60. correct++;
  61. }
  62.  
  63. public double getStatistics() {
  64. return correct / (double) size;
  65. }
  66. }
  67.  
  68. public static File test = new File("test.txt");
  69.  
  70. public static void main(String[] args) {
  71. try {
  72. List<Task> tasks = parseTest(test);
  73. System.out.println("user(surname, name, course, group): ");
  74. User user = parseUser(System.in);
  75. Test test = new Test(tasks, user);
  76. Scanner scanner = new Scanner(System.in);
  77. while (test.hasNextTask()) {
  78. System.out.println(test.nextTask());
  79. System.out.println("answer: ");
  80. test.setAnswer(scanner.nextInt());
  81. }
  82. System.out.println("stat: " + test.getStatistics());
  83. } catch (Exception e) {
  84. e.printStackTrace();
  85. }
  86. }
  87.  
  88. private static User parseUser(InputStream in) throws Exception {
  89. Scanner scanner = new Scanner(in);
  90. User user = new User();
  91. try {
  92. user.surname = scanner.next();
  93. user.name = scanner.next();
  94. user.course = scanner.nextInt();
  95. user.group = scanner.nextInt();
  96. } catch (NoSuchElementException e) {
  97. throw new Exception("wrong format");
  98. }
  99. return user;
  100. }
  101.  
  102. private static List<Task> parseTest(File test) throws Exception {
  103. List<Task> tasks = new LinkedList<>();
  104. Scanner input = new Scanner(test);
  105. try {
  106. while (input.hasNext()) {
  107. Task task = new Task();
  108. task.question = input.nextLine();
  109. while (input.hasNextLine() && !input.hasNextInt())
  110. task.answers.add(input.nextLine());
  111. task.correct = input.nextInt();
  112. }
  113. } catch (NoSuchElementException e) {
  114. throw new Exception("wrong file format");
  115. }
  116. return tasks;
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement