Skip to content
This repository has been archived by the owner on Mar 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #53 from Auxinnn/branch-quiz-result
Browse files Browse the repository at this point in the history
Branch quiz result
  • Loading branch information
Parcly-Taxel authored Oct 17, 2019
2 parents e9d108b + 456ee94 commit d9fd55a
Show file tree
Hide file tree
Showing 26 changed files with 482 additions and 53 deletions.
9 changes: 2 additions & 7 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ Format: `quiz [n/NUMBER_OF_QUESTIONS] [d/DIFFICULTY] [s/SUBJECT]`

==== Answer the quiz question : `type answer` +
Answers the specified question. +
Format: `INDEX : ANSWER`
Format: `INDEX:ANSWER`

==== Change the difficulty: : `change -dif`

Expand All @@ -175,11 +175,6 @@ Format: +
Changes the subject of questions. +
Format: `change -s SUBJECT [MORE_SUBJECTS]`

==== Skip a question : `skip`

Skips current question and goto the next one. +
Format: `skip`

==== Show an answer : `show`

Shows an answer for a question with specific index. +
Expand All @@ -192,7 +187,7 @@ Format: `mark`

==== Exit test mode : `quit`

Exits from the test mode. +
Exits from the quiz mode. +
Format: `quit`

=== Get statistics
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public class Messages {
public static final String MESSAGE_NOTES_LISTED_OVERVIEW = "%1$d notes listed!";
public static final String MESSAGE_INVALID_QUESTION_DISPLAYED_INDEX = "The question index provided is invalid";
public static final String MESSAGE_QUESTIONS_LISTED_OVERVIEW = "%1$d questions listed!";
public static final String MESSAGE_INVALID_QUIZ_QUESTION_DISPLAYED_INDEX = "The question index provided is invalid";
public static final String MESSAGE_INVALID_TASK_DISPLAYED_INDEX = "The task index provided is invalid";
}
19 changes: 8 additions & 11 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,28 @@ public LogicManager(Model model, Storage storage) {

@Override
public CommandResult execute(String commandText) throws CommandException, ParseException {
CommandResult commandResult;
if (!isQuiz) {
logger.info("----------------[USER COMMAND][" + commandText + "]");

CommandResult commandResult;
Command command = addressBookParser.parseCommand(commandText);
commandResult = command.execute(model);

try {
storage.saveAddressBook(model.getAddressBook());
} catch (IOException ioe) {
throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe);
}

return commandResult;
} else {
logger.info("----------------[USER INPUT][" + commandText + "]");

CommandResult commandResult;
Command command = quizParser.parseCommand(commandText);
commandResult = command.execute(model);
}

return commandResult;
try {
storage.saveAddressBook(model.getAddressBook());
} catch (IOException ioe) {
throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe);
}
return commandResult;
}


@Override
public ReadOnlyAddressBook getAddressBook() {
return model.getAddressBook();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

import static java.util.Objects.requireNonNull;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.question.Answer;
import seedu.address.model.question.Question;
import seedu.address.model.question.QuestionBody;
import seedu.address.model.quiz.QuizResult;

/**
* Checks the quiz answer input by users.
Expand All @@ -18,19 +27,35 @@ public class QuizCheckAnswer extends Command {
private final Answer answer;

/**
* @param index of the question the answer is mapped to.
* @param index of the question the answer is mapped to.
* @param answer of the question input by user.
*/
public QuizCheckAnswer(int index, Answer answer) {
this.index = index;
this.answer = answer;
}

public String getQuizTime() {
Date now = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
return simpleDateFormat.format(now);
}

@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (model.checkQuizAnswer(index, answer)) {
List<Question> listShownList = model.getFilteredQuizQuestionList();
if (index >= listShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_QUIZ_QUESTION_DISPLAYED_INDEX);
}

boolean result = model.checkQuizAnswer(index, answer);
QuestionBody questionBody = model.getFilteredQuizQuestionList().get(index).getQuestionBody();
QuizResult quizResult = new QuizResult(answer, questionBody, getQuizTime(), result);
model.addQuizResult(quizResult);

if (result) {
return new CommandResult(ANSWER_CORRECT);
} else {
return new CommandResult(ANSWER_WRONG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import seedu.address.logic.LogicManager;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.question.Difficulty;
import seedu.address.model.question.Question;
Expand All @@ -32,8 +33,7 @@ public class QuizModeCommand extends Command {
+ PREFIX_SUBJECT + "Maths ";

public static final String MESSAGE_SUCCESS = "You have successfully entered quiz mode!";
public static final String MESSAGE_NOT_ENOUGH_QUESTIONS = "There are not enough questions to do!";

public static final String INSUFFICIENT_QUESTION = "There are insufficient questions to do the quiz!";

private final int numOfQuestions;
private final Subject subject;
Expand All @@ -49,12 +49,16 @@ public QuizModeCommand(int numOfQuestions, Subject subject, Difficulty difficult
}

@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
try {
ObservableList<Question> quizQuestionList = model.getQuizQuestions(numOfQuestions, subject, difficulty);
model.setQuizQuestionList(quizQuestionList);
LogicManager.setIsQuiz(true);
} catch (IllegalArgumentException e) {
throw new CommandException(INSUFFICIENT_QUESTION);
}

ObservableList<Question> quizQuestionList = model.getQuizQuestions(numOfQuestions, subject, difficulty);
model.setQuizQuestionList(quizQuestionList);
LogicManager.setIsQuiz(true);
return new CommandResult(MESSAGE_SUCCESS, false, false, true, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.question.Answer;

Expand All @@ -14,6 +15,7 @@ public class QuizShowAnswerCommand extends Command {
public static final String COMMAND_WORD = "show";

public static final String MESSAGE_SUCCESS = "The answer is: %1$s";
public static final String INDEX_EXCEED_RANGE = "The index input is out of the range of quiz questions!";

private final int index;

Expand All @@ -22,10 +24,15 @@ public QuizShowAnswerCommand(int index) {
}

@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
Answer answer = model.showQuizAnswer(index);

Answer answer;
try {
answer = model.showQuizAnswer(index);
} catch (IndexOutOfBoundsException e) {
throw new CommandException(INDEX_EXCEED_RANGE);
}
return new CommandResult(String.format(MESSAGE_SUCCESS, answer.toString()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
public class QuizAnswerParser implements Parser<QuizCheckAnswer> {
private static final String ANSWER_INVALID_FORMAT = "Invalid answer format! \n%1$s";
private static final String SHOW_ANSWER_FORMAT = "Format: INDEX : ANSWER\n" + "Example: 1 : abstraction";
private static final String SHOW_ANSWER_FORMAT = "Format: INDEX:ANSWER\n" + "Example: 1:abstraction";

/**
* Used for initial separation of question index and answer.
Expand All @@ -32,7 +32,7 @@ public QuizCheckAnswer parse(String userInput) throws ParseException {

final String index = matcher.group("index");
final String stringAnswer = matcher.group("answer");
final int intIndex = Integer.parseInt(index);
final int intIndex = Integer.parseInt(index) - 1;
final Answer answer = new Answer(stringAnswer);

return new QuizCheckAnswer(intIndex, answer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* Parses input arguments and creates a new QuizModeCommand object.
*/
public class QuizModeCommandParser implements Parser<QuizModeCommand> {
private static final String INVALID_NUMBER = "The number input should be a positive integer!";

/**
* Parses the given {@code String} of arguments in the context of the QuizModeCommand
Expand All @@ -35,15 +36,21 @@ public QuizModeCommand parse(String args) throws ParseException {
ArgumentTokenizer.tokenize(args, PREFIX_NUMBER, PREFIX_DIFFICULTY, PREFIX_SUBJECT);

int numOfQuestions;
Subject subject;
Difficulty difficulty;

if (!arePrefixesPresent(argMultimap, PREFIX_DIFFICULTY, PREFIX_SUBJECT)
|| (!argMultimap.getPreamble().isEmpty())) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, QuizModeCommand.MESSAGE_USAGE));
}

numOfQuestions = ParserUtil.parseNumber(argMultimap.getValue(PREFIX_NUMBER).get());
Subject subject = ParserUtil.parseSubject(argMultimap.getValue(PREFIX_SUBJECT).get());
Difficulty difficulty = ParserUtil.parseDifficulty(argMultimap.getValue(PREFIX_DIFFICULTY).get());
try {
numOfQuestions = ParserUtil.parseNumber(argMultimap.getValue(PREFIX_NUMBER).get());
subject = ParserUtil.parseSubject(argMultimap.getValue(PREFIX_SUBJECT).get());
difficulty = ParserUtil.parseDifficulty(argMultimap.getValue(PREFIX_DIFFICULTY).get());
} catch (NumberFormatException e) {
throw new ParseException(INVALID_NUMBER);
}
return new QuizModeCommand(numOfQuestions, subject, difficulty);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Command parseCommand(String userInput) throws ParseException {
return new QuitQuizModeCommand();

case QuizShowAnswerCommand.COMMAND_WORD:
return new QuizShowAnswerCommand(Integer.parseInt(arguments));
return new QuizShowAnswerParser().parse(arguments.trim());

default:
return new QuizAnswerParser().parse(userInput);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package seedu.address.logic.parser.quiz;

import static java.util.Objects.requireNonNull;

import seedu.address.logic.commands.quiz.QuizShowAnswerCommand;
import seedu.address.logic.parser.Parser;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new QuizShowAnswerCommand object.
*/
public class QuizShowAnswerParser implements Parser<QuizShowAnswerCommand> {
public static final String INVALID_INDEX = "The index input should be a positive integer!";

private int index;

/**
* Parses the given {@code String} of user input in the context of showing answer
* and returns an QuizShowAnswerCommand object for execution.
*/
public QuizShowAnswerCommand parse(String args) throws ParseException {
requireNonNull(args);

try {
index = Integer.parseInt(args) - 1;
} catch (NumberFormatException e) {
throw new ParseException(INVALID_INDEX);
}
return new QuizShowAnswerCommand(index);
}
}
40 changes: 31 additions & 9 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.stream.Collectors;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
Expand All @@ -15,6 +17,8 @@
import seedu.address.model.question.Subject;
import seedu.address.model.question.UniqueQuestionList;
import seedu.address.model.quiz.QuizQuestionList;
import seedu.address.model.quiz.QuizResult;
import seedu.address.model.quiz.QuizResultList;
import seedu.address.model.task.Task;
import seedu.address.model.task.TaskList;

Expand All @@ -27,6 +31,7 @@ public class AddressBook implements ReadOnlyAddressBook {
private final UniqueNoteList notes;
private final UniqueQuestionList questions;
private final QuizQuestionList quiz;
private final QuizResultList quizResults;
private final TaskList tasks;

/*
Expand All @@ -41,6 +46,7 @@ public class AddressBook implements ReadOnlyAddressBook {
notes = new UniqueNoteList();
questions = new UniqueQuestionList();
quiz = new QuizQuestionList();
quizResults = new QuizResultList();
tasks = new TaskList();
}

Expand Down Expand Up @@ -172,15 +178,18 @@ public void removeQuestion(Question key) {
*/
public ObservableList<Question> getQuizQuestions(int numOfQuestions, Subject subject, Difficulty difficulty) {
ObservableList<Question> quizQuestions = FXCollections.observableArrayList();

for (int i = 0; i < questions.getSize(); i++) {
Question question = questions.get(i);
if (question.getSubject().equals(subject) && question.getDifficulty().equals(difficulty)) {
quizQuestions.add(question);
}
if (quizQuestions.size() == numOfQuestions) {
break;
}
List<Question> filteredQuestions = getQuestionList()
.stream()
.filter(question -> subject.equals(question.getSubject())
&& difficulty.equals(question.getDifficulty()))
.collect(Collectors.toList());

Random random = new Random();
for (int i = 0; i < numOfQuestions; i++) {
int randomIndex = random.nextInt(filteredQuestions.size());
Question question = filteredQuestions.get(randomIndex);
quizQuestions.add(question);
filteredQuestions.remove(randomIndex);
}
return quizQuestions;
}
Expand Down Expand Up @@ -213,6 +222,14 @@ public void clearQuizQuestionList() {
quiz.clearQuizQuestionList();
}

public void addQuizResult(QuizResult quizResult) {
quizResults.add(quizResult);
}

public boolean hasQuizResult(QuizResult quizResult) {
return quizResults.contains(quizResult);
}

// util methods

@Override
Expand All @@ -236,6 +253,11 @@ public ObservableList<Question> getQuizQuestionList() {
return quiz.asUnmodifiableObservableList();
}

@Override
public ObservableList<QuizResult> getQuizResultList() {
return quizResults.asUnmodifiableObservableList();
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand Down
Loading

0 comments on commit d9fd55a

Please sign in to comment.