diff --git a/src/main/java/seedu/duke/command/ListIngredientCommand.java b/src/main/java/seedu/duke/command/ListIngredientCommand.java index 0beee5c17b..90ccca997f 100644 --- a/src/main/java/seedu/duke/command/ListIngredientCommand.java +++ b/src/main/java/seedu/duke/command/ListIngredientCommand.java @@ -1,19 +1,30 @@ package seedu.duke.command; import seedu.duke.data.Menu; +import seedu.duke.data.dish.Dish; import seedu.duke.ui.Ui; /** * Lists all ingredients used in the selected dish to the user. */ -public class ListIngredientCommand extends Command{ +public class ListIngredientCommand extends Command { public static final String COMMAND_WORD = "list_ingredients"; public static final String MESSAGE_USAGE = COMMAND_WORD + ": Lists out the ingredients needed along with the quantity for a specific dish.\n" + "Parameters: INDEX\n" + "Example: " + COMMAND_WORD + " 1"; + + public ListIngredientCommand(int listIndex) { + this.index = listIndex; + } + @Override public void execute(Menu menu, Ui ui) { - ui.printIngredients(menu, index); - }; + Dish selectedDish = menu.getMenuItemsList().get(index - Ui.OFFSET_LIST_INDEX); + if (selectedDish != null) { + ui.printIngredients(selectedDish); + } else { + ui.showToUser("Please select a valid dish index :)"); + } + } } diff --git a/src/main/java/seedu/duke/parser/Parser.java b/src/main/java/seedu/duke/parser/Parser.java index a5309dbbb0..1307009dd2 100644 --- a/src/main/java/seedu/duke/parser/Parser.java +++ b/src/main/java/seedu/duke/parser/Parser.java @@ -28,8 +28,8 @@ public static Command parseCommand(Menu menu, String userInput) { switch (command[0].toLowerCase()) { case ListMenuCommand.COMMAND_WORD: return new ListMenuCommand(); - //case ListIngredientCommand.COMMAND_WORD: - //return prepareListIngredient(menu,userInput); + case ListIngredientCommand.COMMAND_WORD: + return prepareListIngredient(userInput); case DeleteDishCommand.COMMAND_WORD: return prepareDelete(userInput); case ExitCommand.COMMAND_WORD: @@ -45,15 +45,13 @@ public static Command parseCommand(Menu menu, String userInput) { /** * Parses arguments in the context of the ListIngredient command. - * @param menu - * @param userInput + * @param userInput arguments string to parse as index number * @return the prepared command */ - private static Command prepareListIngredient(Menu menu, String userInput) { + private static Command prepareListIngredient(String userInput) { try { final int listIndex = parseArgsAsDisplayedIndex(userInput, ListIngredientCommand.COMMAND_WORD); - //return new ListIngredientCommand(Menu menu, listIndex); - return new IncorrectCommand("NAYCHI DO YOUR WORK"); + return new ListIngredientCommand(listIndex); } catch (ParseException e) { return new IncorrectCommand("MESSAGE_INVALID_COMMAND_FORMAT" + ListIngredientCommand.MESSAGE_USAGE); } catch (NumberFormatException nfe) { diff --git a/src/main/java/seedu/duke/ui/Ui.java b/src/main/java/seedu/duke/ui/Ui.java index 5e8d546e2f..7b497f6a25 100644 --- a/src/main/java/seedu/duke/ui/Ui.java +++ b/src/main/java/seedu/duke/ui/Ui.java @@ -1,6 +1,5 @@ package seedu.duke.ui; -import seedu.duke.data.Menu; import seedu.duke.data.dish.Dish; import seedu.duke.data.dish.Ingredient; @@ -33,19 +32,13 @@ public void showGoodbye() { * Prints out the quantity of each ingredient needed for the * dish that the user selects. * - * @param menu The list of menu items. - * @param dishIndex The index of the dish whose ingredients need to be printed. + * @param selectedDish Dish for ingredients to be listed out. */ - public void printIngredients(Menu menu, int dishIndex) { - Dish selectedDish = menu.getMenuItemsList().get(dishIndex - 1); - if (selectedDish != null) { - showToUser(selectedDish.getName() + " Ingredients: \n"); - - for (Ingredient ingredient : selectedDish.getIngredients()) { - showToUser(ingredient.getName() + " - " + ingredient.getQuantity()); - } - } else { - showToUser("Please select a valid dish index :)"); + public void printIngredients(Dish selectedDish) { + showToUser(selectedDish.getName() + " Ingredients: \n"); + + for (Ingredient ingredient : selectedDish.getIngredients()) { + showToUser(ingredient.getName() + " - " + ingredient.getQuantity()); } }