Skip to content

Commit

Permalink
Merge pull request #54 from NaychiMin/23-parser-for-list-ingredients-…
Browse files Browse the repository at this point in the history
…v1.0

Task 23, Implement parsing support for listing ingredients .
  • Loading branch information
DextheChik3n authored Oct 17, 2023
2 parents eb16e2c + d23e91b commit a60c6f8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
17 changes: 14 additions & 3 deletions src/main/java/seedu/duke/command/ListIngredientCommand.java
Original file line number Diff line number Diff line change
@@ -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 :)");
}
}
}
12 changes: 5 additions & 7 deletions src/main/java/seedu/duke/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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) {
Expand Down
19 changes: 6 additions & 13 deletions src/main/java/seedu/duke/ui/Ui.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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());
}
}

Expand Down

0 comments on commit a60c6f8

Please sign in to comment.