Skip to content

Commit

Permalink
update exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
MrOPPA1 committed Nov 3, 2023
1 parent 662d141 commit 664d4e0
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 69 deletions.
18 changes: 12 additions & 6 deletions src/main/java/seedu/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/seedu/duke/commands/meal/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/duke/commands/meal/MealCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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");
}
}
98 changes: 50 additions & 48 deletions src/main/java/seedu/duke/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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));
Expand All @@ -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();
}
}

Expand Down
28 changes: 18 additions & 10 deletions src/test/java/seedu/duke/parser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 extends Command> T parseAndAssertCommandType(String input, Class<T> expectedCommandClass) {
private <T extends Command> T parseAndAssertCommandType(String input, Class<T> expectedCommandClass)
throws Exception {
final Command result = parser.parseCommand(input);
assertTrue(result.getClass().isAssignableFrom(expectedCommandClass));
return (T) result;
Expand Down

0 comments on commit 664d4e0

Please sign in to comment.