-
Notifications
You must be signed in to change notification settings - Fork 21
Lab 1: Quizer #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DudkoAndrei
wants to merge
24
commits into
Rrenkens:main
Choose a base branch
from
DudkoAndrei:quizer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lab 1: Quizer #29
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
18e0254
Add blank base classes and interfaces
DudkoAndrei 83b01fe
Fix some javadocs
DudkoAndrei 0affb72
Make TaskGenerator Task's nested interface
DudkoAndrei 9a57b7d
Implement Quiz methods
DudkoAndrei afca867
Add MathTask interface and AbstractMathTask class
DudkoAndrei 0c5145f
Add TextTask class
DudkoAndrei 32cd8fb
Add PoolTaskGenerator class
DudkoAndrei bbd8241
Add GroupTaskGenerator class
DudkoAndrei da6e27b
Add isDoubleEqual and truncate methods to AbstractMathTask
DudkoAndrei 3beb748
Add compute method to MathTask.Operation
DudkoAndrei 3f7f5a0
Add ExpressionTask class
DudkoAndrei ac5bc55
Remove code duplicates from AbstractMathTask.Generator class
DudkoAndrei cf8324a
Add EquationTask class
DudkoAndrei 8f45f52
Add getRandomDouble method to AbstractMathTask.Generator class
DudkoAndrei 904ebb4
Add exceptions
DudkoAndrei 913ebbe
Make TextTask class public
DudkoAndrei 504603f
Refactor switches in MathTask class
DudkoAndrei f751caa
Fix GroupTaskGenerator.generate and make constructors public
DudkoAndrei a3967d2
Fix some cases with integer division
DudkoAndrei a3d5646
Fix minor problems
DudkoAndrei f69fa09
Add quizzes
DudkoAndrei 822abe3
Refactor AbstractMathTask.Generator.getRandomDouble method
DudkoAndrei f142e48
Fix PoolTaskGenerator constructor
DudkoAndrei 2224cf5
Refactor GroupTaskGenerator.generate method
DudkoAndrei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package by.dudkoandrei.quizer; | ||
|
|
||
| import by.dudkoandrei.quizer.constants.EquationTaskGeneratorConstants; | ||
| import by.dudkoandrei.quizer.constants.ExpressionTaskGeneratorConstants; | ||
| import by.dudkoandrei.quizer.constants.TextGeneratorConstats; | ||
| import by.dudkoandrei.quizer.task_generators.GroupTaskGenerator; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| /** | ||
| * @return тесты в {@link Map}, где ключ - название теста {@link String} значение - сам тест | ||
| * {@link Quiz} | ||
| */ | ||
| static Map<String, Quiz> getQuizMap() { | ||
| Map<String, Quiz> quizMap = new HashMap<>(); | ||
|
|
||
| quizMap.put("Text Quiz", new Quiz(TextGeneratorConstats.generator, 3)); | ||
| quizMap.put("Integer equations", new Quiz(EquationTaskGeneratorConstants.integerGenerator, 10)); | ||
| quizMap.put("Equations", new Quiz(EquationTaskGeneratorConstants.generator, 10)); | ||
| quizMap.put("Integer expressions", | ||
| new Quiz(ExpressionTaskGeneratorConstants.integerGenerator, 10)); | ||
| quizMap.put("Expressions", new Quiz(ExpressionTaskGeneratorConstants.generator, 10)); | ||
| quizMap.put("All tasks", new Quiz( | ||
| new GroupTaskGenerator(TextGeneratorConstats.generator, | ||
| EquationTaskGeneratorConstants.integerGenerator, | ||
| EquationTaskGeneratorConstants.generator, | ||
| ExpressionTaskGeneratorConstants.integerGenerator, | ||
| ExpressionTaskGeneratorConstants.generator), 20)); | ||
|
|
||
| return quizMap; | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| var quizMap = getQuizMap(); | ||
|
|
||
| System.out.println("Available quizzes:"); | ||
|
|
||
| for (var name : quizMap.keySet()) { | ||
| System.out.println(name); | ||
| } | ||
|
|
||
| System.out.println("Select quiz:"); | ||
|
|
||
| String name = scanner.nextLine(); | ||
| while (!quizMap.containsKey(name)) { | ||
| System.out.println("No such quiz. Try again"); | ||
| name = scanner.nextLine(); | ||
| } | ||
|
|
||
| Quiz quiz = quizMap.get(name); | ||
|
|
||
| System.out.println("Quiz: \"" + name + "\" started"); | ||
| while (!quiz.isFinished()) { | ||
| System.out.println(quiz.nextTask().getText()); | ||
|
|
||
| String answer = scanner.nextLine(); | ||
| Result result = quiz.provideAnswer(answer); | ||
|
|
||
| System.out.println(result + "\n"); | ||
| } | ||
|
|
||
| System.out.println("Quiz ended"); | ||
| System.out.println("Your score: " + quiz.getMark() * 10); | ||
| System.out.println("Correct answers: " + quiz.getCorrectAnswerNumber()); | ||
| System.out.println("Wrong answers: " + quiz.getWrongAnswerNumber()); | ||
| System.out.println("Incorrect input: " + quiz.getIncorrectInputNumber()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package by.dudkoandrei.quizer; | ||
|
|
||
| import by.dudkoandrei.quizer.exceptions.QuizFinishedException; | ||
| import by.dudkoandrei.quizer.exceptions.QuizNotFinishedException; | ||
| import by.dudkoandrei.quizer.tasks.Task; | ||
|
|
||
| /** | ||
| * Class, который описывает один тест. | ||
| */ | ||
| class Quiz { | ||
|
|
||
| private final Task.Generator taskGenerator; | ||
|
|
||
| private Task currentTask = null; | ||
| private final int taskCount; | ||
| private boolean answeredOnCurrentTask = false; | ||
| private int correctAnswerNumber = 0; | ||
| private int wrongAnswerNumber = 0; | ||
| private int incorrectInputNumber = 0; | ||
|
|
||
| /** | ||
| * @param generator генератор заданий | ||
| * @param taskCount количество заданий в тесте | ||
| */ | ||
| Quiz(Task.Generator generator, int taskCount) { | ||
| if (taskCount <= 0) { | ||
| throw new IllegalArgumentException("Task count should be positive"); | ||
| } | ||
| if (generator == null) { | ||
| throw new IllegalArgumentException("Generator is null"); | ||
| } | ||
| this.taskGenerator = generator; | ||
| this.taskCount = taskCount; | ||
| } | ||
|
|
||
| /** | ||
| * @return задание, повторный вызов вернет слелующее | ||
| * @see Task | ||
| */ | ||
| Task nextTask() { | ||
| if (isFinished()) { | ||
| throw new QuizFinishedException(); | ||
| } | ||
|
|
||
| if (answeredOnCurrentTask || currentTask == null) { | ||
| currentTask = taskGenerator.generate(); | ||
| answeredOnCurrentTask = false; | ||
| } | ||
|
|
||
| return currentTask; | ||
| } | ||
|
|
||
| /** | ||
| * Предоставить ответ ученика. Если результат {@link Result#INCORRECT_INPUT}, то счетчик | ||
| * неправильных ответов не увеличивается, а {@link #nextTask()} в следующий раз вернет тот же | ||
| * самый объект {@link Task}. | ||
| */ | ||
| Result provideAnswer(String answer) { | ||
| if (isFinished()) { | ||
| throw new QuizFinishedException(); | ||
| } | ||
|
|
||
| Result result = currentTask.validate(answer); | ||
|
|
||
| switch (result) { | ||
| case OK -> { | ||
| correctAnswerNumber++; | ||
| answeredOnCurrentTask = true; | ||
| } | ||
| case WRONG -> { | ||
| wrongAnswerNumber++; | ||
| answeredOnCurrentTask = true; | ||
| } | ||
| case INCORRECT_INPUT -> incorrectInputNumber++; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * @return завершен ли тест | ||
| */ | ||
| boolean isFinished() { | ||
| return correctAnswerNumber + wrongAnswerNumber == taskCount; | ||
| } | ||
|
|
||
| /** | ||
| * @return количество правильных ответов | ||
| */ | ||
| int getCorrectAnswerNumber() { | ||
| return correctAnswerNumber; | ||
| } | ||
|
|
||
| /** | ||
| * @return количество неправильных ответов | ||
| */ | ||
| int getWrongAnswerNumber() { | ||
| return wrongAnswerNumber; | ||
| } | ||
|
|
||
| /** | ||
| * @return количество раз, когда был предоставлен неправильный ввод | ||
| */ | ||
| int getIncorrectInputNumber() { | ||
| return incorrectInputNumber; | ||
| } | ||
|
|
||
| /** | ||
| * @return оценка, которая является отношением количества правильных ответов к количеству всех | ||
| * вопросов. Оценка выставляется только в конце! | ||
| */ | ||
| double getMark() { | ||
| if (!isFinished()) { | ||
| throw new QuizNotFinishedException(); | ||
| } | ||
|
|
||
| return ((double) correctAnswerNumber) / taskCount; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package by.dudkoandrei.quizer; | ||
|
|
||
| /** | ||
| * Enum, который описывает результат ответа на задание. | ||
| */ | ||
| public enum Result { | ||
| OK, // Получен правильный ответ | ||
| WRONG, // Получен неправильный ответ | ||
| INCORRECT_INPUT // Некорректный ввод. Например, текст, когда ожидалось число | ||
| } |
19 changes: 19 additions & 0 deletions
19
lab-01/src/by/dudkoandrei/quizer/constants/EquationTaskGeneratorConstants.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package by.dudkoandrei.quizer.constants; | ||
|
|
||
| import by.dudkoandrei.quizer.tasks.Task.Generator; | ||
| import by.dudkoandrei.quizer.tasks.math_tasks.EquationTask; | ||
| import by.dudkoandrei.quizer.tasks.math_tasks.MathTask.Operation; | ||
| import java.util.EnumSet; | ||
|
|
||
| public class EquationTaskGeneratorConstants { | ||
|
|
||
| public static final Generator generator = new EquationTask.Generator( | ||
| 0, | ||
| 25, | ||
| 4, | ||
| EnumSet.allOf(Operation.class)); | ||
| public static final Generator integerGenerator = new EquationTask.Generator( | ||
| 0, | ||
| 25, | ||
| EnumSet.allOf(Operation.class)); | ||
| } |
19 changes: 19 additions & 0 deletions
19
lab-01/src/by/dudkoandrei/quizer/constants/ExpressionTaskGeneratorConstants.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package by.dudkoandrei.quizer.constants; | ||
|
|
||
| import by.dudkoandrei.quizer.tasks.Task.Generator; | ||
| import by.dudkoandrei.quizer.tasks.math_tasks.ExpressionTask; | ||
| import by.dudkoandrei.quizer.tasks.math_tasks.MathTask.Operation; | ||
| import java.util.EnumSet; | ||
|
|
||
| public class ExpressionTaskGeneratorConstants { | ||
|
|
||
| public static final Generator generator = new ExpressionTask.Generator( | ||
| 0, | ||
| 25, | ||
| 4, | ||
| EnumSet.allOf(Operation.class)); | ||
| public static final Generator integerGenerator = new ExpressionTask.Generator( | ||
| 0, | ||
| 25, | ||
| EnumSet.allOf(Operation.class)); | ||
| } |
26 changes: 26 additions & 0 deletions
26
lab-01/src/by/dudkoandrei/quizer/constants/TextGeneratorConstats.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package by.dudkoandrei.quizer.constants; | ||
|
|
||
| import by.dudkoandrei.quizer.task_generators.PoolTaskGenerator; | ||
| import by.dudkoandrei.quizer.tasks.Task.Generator; | ||
| import by.dudkoandrei.quizer.tasks.TextTask; | ||
|
|
||
| public class TextGeneratorConstats { | ||
|
|
||
| public static final Generator generator = new PoolTaskGenerator( | ||
| false, | ||
| new TextTask("Fill in the blank: \"_____. I'm your father.\"\n" | ||
| + "a) Luke\n" | ||
| + "b) Boy\n" | ||
| + "c) Skywalker\n" | ||
| + "d) No", "d"), | ||
| new TextTask("Mace Windu light-saber color?\n" | ||
| + "a) Red\n" | ||
| + "b) Purple\n" | ||
| + "c) Green\n" | ||
| + "d) Blue", "b"), | ||
| new TextTask("What species of alien is Yoda?\n" | ||
| + "a) Ortolan\n" | ||
| + "b) Human\n" | ||
| + "c) Unknown\n" | ||
| + "d) Gungan", "c")); | ||
| } |
8 changes: 8 additions & 0 deletions
8
lab-01/src/by/dudkoandrei/quizer/exceptions/GeneratorsFailedException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package by.dudkoandrei.quizer.exceptions; | ||
|
|
||
| public class GeneratorsFailedException extends RuntimeException { | ||
|
|
||
| public GeneratorsFailedException() { | ||
| super("All generators threw exceptions"); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
lab-01/src/by/dudkoandrei/quizer/exceptions/NoMoreTasksException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package by.dudkoandrei.quizer.exceptions; | ||
|
|
||
| public class NoMoreTasksException extends RuntimeException { | ||
|
|
||
| public NoMoreTasksException() { | ||
| super("No more tasks"); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
lab-01/src/by/dudkoandrei/quizer/exceptions/QuizFinishedException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package by.dudkoandrei.quizer.exceptions; | ||
|
|
||
| public class QuizFinishedException extends RuntimeException { | ||
|
|
||
| public QuizFinishedException() { | ||
| super("Quiz is finished"); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
lab-01/src/by/dudkoandrei/quizer/exceptions/QuizNotFinishedException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package by.dudkoandrei.quizer.exceptions; | ||
|
|
||
| public class QuizNotFinishedException extends RuntimeException { | ||
|
|
||
| public QuizNotFinishedException() { | ||
| super("Quiz is not finished"); | ||
| } | ||
| } |
73 changes: 73 additions & 0 deletions
73
lab-01/src/by/dudkoandrei/quizer/task_generators/GroupTaskGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package by.dudkoandrei.quizer.task_generators; | ||
|
|
||
| import by.dudkoandrei.quizer.exceptions.GeneratorsFailedException; | ||
| import by.dudkoandrei.quizer.tasks.Task; | ||
| import by.dudkoandrei.quizer.tasks.Task.Generator; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Task.Generator, который позволяет объединить несколько других Task.Generator. | ||
| */ | ||
| public class GroupTaskGenerator implements Task.Generator { | ||
|
|
||
| private final List<Generator> generators; | ||
| private int lastGeneratorIndex = 0; | ||
|
|
||
| /** | ||
| * Конструктор с переменным числом аргументов. | ||
| * | ||
| * @param generators генераторы, которые в конструктор передаются через запятую | ||
| */ | ||
| public GroupTaskGenerator(Task.Generator... generators) { | ||
| this(Arrays.asList(generators)); | ||
| } | ||
|
|
||
| /** | ||
| * Конструктор, который принимает коллекцию генераторов. | ||
| * | ||
| * @param generators генераторы, которые передаются в конструктор в Collection (например, | ||
| * {@link ArrayList}) | ||
| */ | ||
| public GroupTaskGenerator(Collection<Generator> generators) { | ||
| if (generators == null) { | ||
| throw new IllegalArgumentException("generators is null"); | ||
| } | ||
| if (generators.isEmpty()) { | ||
| throw new IllegalArgumentException("generators is empty"); | ||
| } | ||
| if (generators.contains(null)) { | ||
| throw new IllegalArgumentException("generators contains null"); | ||
| } | ||
|
|
||
| this.generators = new ArrayList<>(generators); | ||
|
|
||
| Collections.shuffle(this.generators); | ||
| } | ||
|
|
||
| /** | ||
| * @return результат метода generate() случайного генератора из списка. Если этот генератор | ||
| * выбросил исключение в методе generate(), выбирается другой. Если все генераторы выбрасывают | ||
| * исключение, то и тут выбрасывается исключение. | ||
| */ | ||
| public Task generate() { | ||
| for (int offset = 1; offset <= generators.size(); ++offset) { | ||
| try { | ||
| int index = getGeneratorIndex(offset); | ||
| Task t = generators.get(index).generate(); | ||
| lastGeneratorIndex = index; | ||
| return t; | ||
| } catch (Exception ignored) { | ||
| } | ||
| } | ||
|
|
||
| throw new GeneratorsFailedException(); | ||
| } | ||
|
|
||
| private int getGeneratorIndex(int offset) { | ||
| return (lastGeneratorIndex + offset) % generators.size(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Мне кажется, что лучше хранить последний результат.