forked from nus-cs2103-AY1920S1/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/main/java/seedu/weme/logic/parser/contextparser/ViewParser.java
This file contains 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,45 @@ | ||
package seedu.weme.logic.parser.contextparser; | ||
|
||
import static seedu.weme.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.weme.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND; | ||
|
||
import java.util.regex.Matcher; | ||
|
||
import seedu.weme.logic.commands.Command; | ||
import seedu.weme.logic.commands.generalcommand.HelpCommand; | ||
import seedu.weme.logic.commands.generalcommand.RedoCommand; | ||
import seedu.weme.logic.commands.generalcommand.UndoCommand; | ||
import seedu.weme.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses user input in the view context. | ||
*/ | ||
public class ViewParser extends WemeParser { | ||
|
||
/** | ||
* Parses user input into command for execution. | ||
* | ||
* @param userInput full user input string | ||
* @return the command based on the user input | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
@Override | ||
public Command parseCommand(String userInput) throws ParseException { | ||
final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim()); | ||
if (!matcher.matches()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
final String commandWord = matcher.group(COMMAND_WORD); | ||
final String arguments = matcher.group(ARGUMENTS); | ||
switch (commandWord) { | ||
case UndoCommand.COMMAND_WORD: | ||
case RedoCommand.COMMAND_WORD: | ||
throw new ParseException(MESSAGE_UNKNOWN_COMMAND); | ||
// override Undo and Redo command to prevent user from undoing in view context. | ||
|
||
default: | ||
return super.parseCommand(userInput); | ||
} | ||
} | ||
} |
This file contains 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
53 changes: 53 additions & 0 deletions
53
src/main/java/seedu/weme/logic/prompter/contextprompter/ViewPrompter.java
This file contains 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,53 @@ | ||
package seedu.weme.logic.prompter.contextprompter; | ||
|
||
import static seedu.weme.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND; | ||
import static seedu.weme.logic.parser.contextparser.WemeParser.ARGUMENTS; | ||
import static seedu.weme.logic.parser.contextparser.WemeParser.BASIC_COMMAND_FORMAT; | ||
import static seedu.weme.logic.parser.contextparser.WemeParser.COMMAND_WORD; | ||
import static seedu.weme.logic.prompter.util.PrompterUtil.GENERAL_COMMANDS; | ||
import static seedu.weme.logic.prompter.util.PrompterUtil.VIEW_COMMANDS; | ||
import static seedu.weme.logic.prompter.util.PrompterUtil.VIEW_COMMANDS_DESCRIPTION_MAP; | ||
import static seedu.weme.logic.prompter.util.PrompterUtil.promptCommandWord; | ||
|
||
import java.util.regex.Matcher; | ||
|
||
import seedu.weme.logic.commands.generalcommand.RedoCommand; | ||
import seedu.weme.logic.commands.generalcommand.UndoCommand; | ||
import seedu.weme.logic.prompter.exceptions.PromptException; | ||
import seedu.weme.logic.prompter.prompt.CommandPrompt; | ||
import seedu.weme.model.Model; | ||
|
||
/** | ||
* Prompt command arguments in the statistics context. | ||
*/ | ||
public class ViewPrompter extends WemePrompter { | ||
|
||
@Override | ||
public CommandPrompt promptCommand(Model model, String userInput) throws PromptException { | ||
final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim()); | ||
if (!matcher.matches()) { | ||
return new CommandPrompt(VIEW_COMMANDS | ||
.stream() | ||
.sorted() | ||
.map(command -> VIEW_COMMANDS_DESCRIPTION_MAP.get(command)) | ||
.reduce((x, y) -> x + '\n' + y) | ||
.orElse(""), | ||
VIEW_COMMANDS.stream().sorted().findFirst().orElse("")); | ||
} | ||
|
||
final String commandWord = matcher.group(COMMAND_WORD); | ||
final String arguments = matcher.group(ARGUMENTS); | ||
|
||
// Ignore if trying to do undo or redo | ||
if (GENERAL_COMMANDS.contains(commandWord) && !commandWord.equals(UndoCommand.COMMAND_WORD) | ||
&& !commandWord.equals(RedoCommand.COMMAND_WORD)) { | ||
return super.promptCommand(model, userInput); | ||
} | ||
|
||
if (arguments.isBlank()) { | ||
return promptCommandWord(VIEW_COMMANDS, commandWord, VIEW_COMMANDS_DESCRIPTION_MAP); | ||
} else { | ||
throw new PromptException(MESSAGE_UNKNOWN_COMMAND); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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