From 664d4e07c0b9aa77f85c323b72cde80ce292d986 Mon Sep 17 00:00:00 2001 From: OPPA1 <3105784412@qq.com> Date: Fri, 3 Nov 2023 15:45:55 +0800 Subject: [PATCH] update exception handler --- src/main/java/seedu/duke/Duke.java | 18 ++-- .../seedu/duke/commands/meal/AddCommand.java | 12 ++- .../seedu/duke/commands/meal/MealCommand.java | 6 ++ src/main/java/seedu/duke/parser/Parser.java | 98 ++++++++++--------- .../java/seedu/duke/parser/ParserTest.java | 28 ++++-- 5 files changed, 93 insertions(+), 69 deletions(-) diff --git a/src/main/java/seedu/duke/Duke.java b/src/main/java/seedu/duke/Duke.java index 363dc9907a..82bc476f37 100644 --- a/src/main/java/seedu/duke/Duke.java +++ b/src/main/java/seedu/duke/Duke.java @@ -74,12 +74,18 @@ private void exit() { private void runCommandLoopUntilExitCommand() { Command command; do { - String userCommandText = ui.getUserCommand(); - command = new Parser().parseCommand(userCommandText); - CommandResult result = executeCommand(command); - ui.showResultToUser(result); - - } while (!ExitCommand.isExit(command)); + try { + String userCommandText = ui.getUserCommand(); + command = new Parser().parseCommand(userCommandText); + CommandResult result = executeCommand(command); + ui.showResultToUser(result); + if (ExitCommand.isExit(command)) { + break; + } + } catch (Exception e) { + ui.showToUser(e.getMessage()); + } + } while (true); } /** diff --git a/src/main/java/seedu/duke/commands/meal/AddCommand.java b/src/main/java/seedu/duke/commands/meal/AddCommand.java index c51eab1720..4a07b3c049 100644 --- a/src/main/java/seedu/duke/commands/meal/AddCommand.java +++ b/src/main/java/seedu/duke/commands/meal/AddCommand.java @@ -3,20 +3,22 @@ import seedu.duke.commands.CommandResult; import seedu.duke.data.meal.Meal; +import java.util.List; + public class AddCommand extends MealCommand { - public static final String COMMAND_WORD = "meal add"; + public static final String COMMAND_WORD = "meal_add"; public static final String MESSAGE_USAGE = COMMAND_WORD + ": Add a meal and record the amount of calories.\n" + "Example: " + COMMAND_WORD + " potato 15"; private static final int validArgumentAmount = 2; private final String name; private final int calories; - public AddCommand(String[] arguments) throws Exception { - if (arguments == null || arguments.length != 2) { + public AddCommand(List arguments) throws Exception { + if (arguments == null || arguments.size() != validArgumentAmount) { throw new Exception("Incorrect amount of the arguments."); } - name = arguments[0]; - calories = Integer.parseInt(arguments[1]); + name = arguments.get(0); + calories = Integer.parseInt(arguments.get(1)); } @Override diff --git a/src/main/java/seedu/duke/commands/meal/MealCommand.java b/src/main/java/seedu/duke/commands/meal/MealCommand.java index faa19d983e..95186922ea 100644 --- a/src/main/java/seedu/duke/commands/meal/MealCommand.java +++ b/src/main/java/seedu/duke/commands/meal/MealCommand.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import seedu.duke.commands.Command; +import seedu.duke.commands.CommandResult; import seedu.duke.data.meal.Meal; public class MealCommand extends Command { @@ -27,4 +28,9 @@ protected void checkArgument(String[] arguments, int validArgumentAmount) throws throw new Exception("Incorrect amount of the arguments."); } } + + @Override + public CommandResult execute() throws Exception { + throw new UnsupportedOperationException("meal command"); + } } diff --git a/src/main/java/seedu/duke/parser/Parser.java b/src/main/java/seedu/duke/parser/Parser.java index 5259853628..c30caec5d5 100644 --- a/src/main/java/seedu/duke/parser/Parser.java +++ b/src/main/java/seedu/duke/parser/Parser.java @@ -21,6 +21,7 @@ import seedu.duke.commands.logcommands.UpdateLogCommand; import seedu.duke.commands.meal.MealCommand; import seedu.duke.data.exception.IllegalValueException; +import seedu.duke.commands.meal.*; /** * Parses user input. @@ -42,8 +43,9 @@ public class Parser { * * @param userInput full user input string * @return the command based on the user input + * @throws Exception */ - public Command parseCommand(String userInput) { + public Command parseCommand(String userInput) throws Exception { final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim()); if (!matcher.matches()) { return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE)); @@ -54,53 +56,53 @@ public Command parseCommand(String userInput) { switch (commandWord) { - // case AddCommand.COMMAND_WORD: - // return prepareAdd(arguments); - // - // case DeleteCommand.COMMAND_WORD: - // return prepareDelete(arguments); - // - // case ClearCommand.COMMAND_WORD: - // return new ClearCommand(); - // - // case FindCommand.COMMAND_WORD: - // return prepareFind(arguments); - // - // case ListCommand.COMMAND_WORD: - // return new ListCommand(); - // - // case ViewCommand.COMMAND_WORD: - // return prepareView(arguments); - // - // case ViewAllCommand.COMMAND_WORD: - // return prepareViewAll(arguments); - // - - case LogCommand.COMMAND_WORD: - return new LogCommand(Arrays.asList(arguments.trim().split(" "))); - - case DeleteLogCommand.COMMAND_WORD: - return new DeleteLogCommand(Arrays.asList(arguments.trim().split(" "))); - - case ViewLogCommand.COMMAND_WORD: - return new ViewLogCommand(Arrays.asList(arguments.trim().split(" "))); - - case UpdateLogCommand.COMMAND_WORD: - return new UpdateLogCommand(Arrays.asList(arguments.trim().split(" "))); - - case MealCommand.COMMAND_WORD: - return new MealCommand(Arrays.asList(arguments.trim().split(" "))); - - case GoalCommand.COMMAND_WORD: - return new GoalCommand(userInput); - - case ExitCommand.COMMAND_WORD: - return new ExitCommand(); - - case HelpCommand.COMMAND_WORD: // Fallthrough - - default: - return new HelpCommand(); + // case AddCommand.COMMAND_WORD: + // return prepareAdd(arguments); + // + // case DeleteCommand.COMMAND_WORD: + // return prepareDelete(arguments); + // + // case ClearCommand.COMMAND_WORD: + // return new ClearCommand(); + // + // case FindCommand.COMMAND_WORD: + // return prepareFind(arguments); + // + // case ListCommand.COMMAND_WORD: + // return new ListCommand(); + // + // case ViewCommand.COMMAND_WORD: + // return prepareView(arguments); + // + // case ViewAllCommand.COMMAND_WORD: + // return prepareViewAll(arguments); + // + + case LogCommand.COMMAND_WORD: + return new LogCommand(Arrays.asList(arguments.trim().split(" "))); + + case DeleteLogCommand.COMMAND_WORD: + return new DeleteLogCommand(Arrays.asList(arguments.trim().split(" "))); + + case ViewLogCommand.COMMAND_WORD: + return new ViewLogCommand(Arrays.asList(arguments.trim().split(" "))); + + case UpdateLogCommand.COMMAND_WORD: + return new UpdateLogCommand(Arrays.asList(arguments.trim().split(" "))); + + case seedu.duke.commands.meal.AddCommand.COMMAND_WORD: + return new AddCommand(Arrays.asList(arguments.trim().split(" "))); + + case GoalCommand.COMMAND_WORD: + return new GoalCommand(userInput); + + case ExitCommand.COMMAND_WORD: + return new ExitCommand(); + + case HelpCommand.COMMAND_WORD: // Fallthrough + + default: + return new HelpCommand(); } } diff --git a/src/test/java/seedu/duke/parser/ParserTest.java b/src/test/java/seedu/duke/parser/ParserTest.java index 6c91c6fff1..fd76b80b0b 100644 --- a/src/test/java/seedu/duke/parser/ParserTest.java +++ b/src/test/java/seedu/duke/parser/ParserTest.java @@ -21,42 +21,48 @@ public void setUp() { } @Test - public void parse_emptyInput_returnsIncorrect() { + public void parse_emptyInput_returnsIncorrect() throws Exception { final String[] emptyInputs = { "", " ", "\n \n" }; final String resultMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE); parseAndAssertIncorrectWithMessage(resultMessage, emptyInputs); } @Test - public void parse_unknownCommandWord_returnsHelp() { + public void parse_unknownCommandWord_returnsHelp() throws Exception { final String input = "unknowncommandword arguments arguments"; parseAndAssertCommandType(input, HelpCommand.class); } /* - * Tests for 0-argument commands ======================================================================= + * Tests for 0-argument commands + * ======================================================================= */ @Test - public void parse_helpCommand_parsedCorrectly() { + public void parse_helpCommand_parsedCorrectly() throws Exception { final String input = "help"; parseAndAssertCommandType(input, HelpCommand.class); } @Test - public void parse_exitCommand_parsedCorrectly() { + public void parse_exitCommand_parsedCorrectly() throws Exception { final String input = "exit"; parseAndAssertCommandType(input, ExitCommand.class); } /* - * Utility methods ==================================================================================== + * Utility methods + * ============================================================================= + * ======= */ /** - * Asserts that parsing the given inputs will return IncorrectCommand with the given feedback message. + * Asserts that parsing the given inputs will return IncorrectCommand with the + * given feedback message. + * + * @throws Exception */ - private void parseAndAssertIncorrectWithMessage(String feedbackMessage, String... inputs) { + private void parseAndAssertIncorrectWithMessage(String feedbackMessage, String... inputs) throws Exception { for (String input : inputs) { final IncorrectCommand result = parseAndAssertCommandType(input, IncorrectCommand.class); assertEquals(result.feedbackToUser, feedbackMessage); @@ -66,11 +72,13 @@ private void parseAndAssertIncorrectWithMessage(String feedbackMessage, String.. /** * Parses input and asserts the class/type of the returned command object. * - * @param input to be parsed + * @param input to be parsed * @param expectedCommandClass expected class of returned command * @return the parsed command object + * @throws Exception */ - private T parseAndAssertCommandType(String input, Class expectedCommandClass) { + private T parseAndAssertCommandType(String input, Class expectedCommandClass) + throws Exception { final Command result = parser.parseCommand(input); assertTrue(result.getClass().isAssignableFrom(expectedCommandClass)); return (T) result;