Skip to content

Commit

Permalink
Improved error messages in Parser Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
sreesubbash committed Nov 5, 2019
1 parent 5322620 commit 5ad669e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 23 deletions.
2 changes: 2 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ Examples:
* `start`
* `start easy`

NOTE: Entering the start command after wordbank is selected will enter game mode. In event that there are fewer than 3 flash cards, the game would not run. You would have to go to `open` mode to add flash cards.

==== Customise game play: `settings`
Enters settings page, so that the user can customise the game play.

Expand Down
78 changes: 55 additions & 23 deletions src/main/java/seedu/address/logic/parser/ParserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,7 @@ public ParserManager() {

}

private boolean checkIfCommandIsValid(String text) {
List<ModeEnum> allModes = new ArrayList<>();
allModes.add(ModeEnum.OPEN);
allModes.add(ModeEnum.HOME);
allModes.add(ModeEnum.SETTINGS);
allModes.add(ModeEnum.GAME);
SpecificModeParser tempParser;
for (ModeEnum modeEnum : allModes) {
tempParser = setCurrentParser(modeEnum);
try {
Command command = tempParser.parseCommand(text);
if (command != null) {
return true;
}
} catch (ParseException e) {

}
}
return false;

}

/**
* Gets current mode from internal state.
Expand Down Expand Up @@ -253,15 +233,67 @@ public Command parseCommand(String userInput) throws ParseException, ModeSwitchE
return switchCommand;
} else if (currentModeCommand != null) {
return currentModeCommand;
} else if (checkIfModeCommandIsValid(userInput)) {
throw new ParseException("This command does not work right now\n"
+ "Try switching mode, selecting WordBank or Stopping game");
} else if (checkIfSwitchCommandValid(userInput)) {
throw new ParseException("Unable to switch mode. Try selecting WordBank or stopping game.");
} else {
if (checkIfCommandIsValid(userInput)) {
throw new ParseException("Wrong mode, WordBank not loaded or Game is running");
}
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}

}

/**
* Checks if user input can be parsed by other mode parsers.
* @param text is user input
* @return true if it can be parsed
*/
private boolean checkIfModeCommandIsValid(String text) {
List<ModeEnum> allModes = new ArrayList<>();
allModes.add(ModeEnum.OPEN);
allModes.add(ModeEnum.HOME);
allModes.add(ModeEnum.SETTINGS);
allModes.add(ModeEnum.GAME);
SpecificModeParser tempParser;
for (ModeEnum modeEnum : allModes) {
tempParser = setCurrentParser(modeEnum);
try {
Command command = tempParser.parseCommand(text);
if (command != null) {
return true;
}
} catch (ParseException e) {
return true;
}
}
return false;

}

/**
* Checks if user input can be parsed by switch parsers.
* @param text is user input
* @return true if it can be parsed
*/
private boolean checkIfSwitchCommandValid(String text) {
SpecificModeParser tempParser = new SpecificModeParser();
tempParser.add(SwitchToOpenCommand.class, null);
tempParser.add(SwitchToHomeCommand.class, null);
tempParser.add(SwitchToStartCommand.class, StartCommandParser.class);
tempParser.add(SwitchToSettingsCommand.class, null);
try {
Command command = tempParser.parseCommand(text);
if (command != null) {
return true;
}
} catch (ParseException e) {
return true;
}
return false;

}

/**
* Gets a list of modes available to switch to based on internal state
*
Expand Down

0 comments on commit 5ad669e

Please sign in to comment.