Skip to content

Commit

Permalink
Improve code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
keiteo committed Nov 10, 2019
1 parent 86ad50a commit c7db189
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ public class EndTestCommand extends Command {

public EndTestCommand(KeyboardFlashCardsParser keyboardFlashCardsParser) {
requireNonNull(keyboardFlashCardsParser);

this.keyboardFlashCardsParser = keyboardFlashCardsParser;
}

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

model.updatePerformance(model);
logger.log(Level.INFO, "Updating performance");

keyboardFlashCardsParser.endTestMode();
logger.log(Level.INFO, "Enabling KeyboardFlashCardsParser to accept normal commands");

CommandResult result = new CommandResult("Test ended");
result.setTestMode(false, true);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ abstract class NextQuestionCommand extends Command {
@Override
public CommandResult execute(Model model) {
requireNonNull(model);

if (!model.hasTestFlashCard()) {
logger.log(Level.INFO, "No more flashcards left to test!");

keyboardFlashCardsParser.endTestMode();
logger.log(Level.INFO, "Enabling KeyboardFlashCardsParser to accept normal commands");

model.updatePerformance(model);
logger.log(Level.INFO, "Updating performance");

CommandResult result = new CommandResult(MESSAGE_SUCCESS_END_OF_TEST);
result.setTestMode(false, true);
return result;
Expand All @@ -42,6 +46,7 @@ public CommandResult execute(Model model) {
model.setTestFlashCard();
keyboardFlashCardsParser.setAwaitingAnswer(true);
logger.log(Level.INFO, "Getting the next flashcard");

return new CommandResult(
messageSuccess,
model.getTestFlashCardPanel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ public class RateQuestionCommand extends NextQuestionCommand {

public RateQuestionCommand(KeyboardFlashCardsParser keyboardFlashCardsParser, Rating rating) {
super(keyboardFlashCardsParser, MESSAGE_SUCCESS);

requireNonNull(rating);

this.keyboardFlashCardsParser = keyboardFlashCardsParser;
this.rating = rating;
}

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

updateModelStatistics(model);
updateFlashCardRating(model);
return super.execute(model);
Expand All @@ -50,8 +53,10 @@ public boolean equals(Object other) {
/** Updates statistics in the model. */
private void updateModelStatistics(Model model) {
assert model != null;

String rating = this.rating.toString();
assert rating.equals("good") || rating.equals("hard") || rating.equals("easy");

switch (rating) {
case "good":
model.editStats(0);
Expand All @@ -70,14 +75,17 @@ private void updateModelStatistics(Model model) {
/** Updates the rating of a flashcard. */
private void updateFlashCardRating(Model model) {
assert model != null;

FlashCard flashCardToUpdate = model.getCurrentTestFlashCard();
assert flashCardToUpdate != null;

model.setFlashCard(flashCardToUpdate, createUpdatedFlashCard(flashCardToUpdate));
}

/** Creates a new immutable flashcard with the updated rating. */
private FlashCard createUpdatedFlashCard(FlashCard flashCardToUpdate) {
assert flashCardToUpdate != null;

return new FlashCard(flashCardToUpdate.getQuestion(),
flashCardToUpdate.getAnswer(),
rating, flashCardToUpdate.getCategories());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ public class ShowAnswerCommand extends Command {

public ShowAnswerCommand(KeyboardFlashCardsParser keyboardFlashCardsParser) {
requireNonNull(keyboardFlashCardsParser);

this.keyboardFlashCardsParser = keyboardFlashCardsParser;
}

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

model.showAnswer();
keyboardFlashCardsParser.setAwaitingAnswer(false);
return new CommandResult(MESSAGE_SHOW_ANSWER_SUCCESS);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/logic/commands/StartCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ public StartCommand(KeyboardFlashCardsParser keyboardFlashCardsParser, String ta
@Override
public CommandResult execute(Model model) {
requireNonNull(model);

List<FlashCard> testList = searchTag(model);
model.initializeTestModel(testList);

if (!model.hasTestFlashCard()) {
logger.log(Level.WARNING, String.format("No flashcards found with the following tag(s):\n%s", tagName));
return new CommandResult(MESSAGE_NO_FLASHCARDS);
}

keyboardFlashCardsParser.startTestMode();
model.setTestFlashCard();
keyboardFlashCardsParser.setAwaitingAnswer(true);
Expand All @@ -63,6 +66,7 @@ public CommandResult execute(Model model) {
MESSAGE_START_TEST_SUCCESS,
model.getTestFlashCardPanel());
result.setTestMode(true, false);

return result;

}
Expand All @@ -78,9 +82,11 @@ public boolean equals(Object other) {
/** Searches the list of flashcard to fetch the relevant tags. */
private List<FlashCard> searchTag(Model model) {
assert model != null;

if (tagName.isEmpty()) {
return new LinkedList<>(model.getFlashCardList());
}

CategoryContainsAnyKeywordsPredicate predicate = getSearchTermPredicate();
logger.log(Level.INFO, "Getting a list of flashcards to test");
return model.getFilteredFlashCardListNoCommit(predicate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public RateQuestionCommand parse(String args) throws ParseException {
&& !rating.equals(Rating.HARD)) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, RateQuestionCommand.MESSAGE_USAGE));
}

return new RateQuestionCommand(keyboardFlashCardsParser, new Rating(rating));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public StartCommand parse(String args) throws ParseException {
if (!args.matches(alphaNumericWithSpacesRegex) && !args.isEmpty()) {
throw new ParseException(BAD_ARGUMENTS);
}

String trimmedArgs = args.trim();
return new StartCommand(keyboardFlashCardsParser, trimmedArgs);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/model/FlashCardTestModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private void setTestFlashCardPanel() {

public TestFlashCardPanel getTestFlashCardPanel() {
requireNonNull(currentFlashCard);

setTestFlashCardPanel();
return testFlashCardPanel;
}
Expand Down

0 comments on commit c7db189

Please sign in to comment.