diff --git a/docs/UserGuide.md b/docs/UserGuide.md index 099390343d..c67cc2d360 100644 --- a/docs/UserGuide.md +++ b/docs/UserGuide.md @@ -255,8 +255,11 @@ Example: `buy_ingredient ingredient/chicken qty/500g, ingredient/milk qty/1000ml Output: ``` Added to stock: -Ingredient: chicken Qty: 500g -Ingredient: milk Qty: 1000ml +Ingredient: milk +Total Qty: 1000ml + +Ingredient: chicken +Total Qty: 500g ``` diff --git a/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java b/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java index faa5ea4616..100747defe 100644 --- a/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java +++ b/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java @@ -69,7 +69,7 @@ public void execute() { private void addIngredient() { for(int i = 0; i < ingredients.size(); i++) { Ingredient ingredient = ingredients.get(i); - ingredient = pantry.addIngredientToStock(ingredient.getName(), + ingredient = pantry.addIngredientToStock(ingredient.getName().toLowerCase(), ingredient.getQty(), ingredient.getUnit()); ingredients.set(i, ingredient); @@ -92,7 +92,7 @@ private void buildBuyIngredientMessage(Ingredient ingredient) { } ingredientsToBePrinted.add(ingredient); ingredientString += "Ingredient: " + ingredient.getName() - + "\nQty: " + ingredient.getQty() + + "\nTotal Qty: " + ingredient.getQty() + ingredient.getUnit() + "\n\n"; } } diff --git a/src/main/java/seedu/cafectrl/parser/Parser.java b/src/main/java/seedu/cafectrl/parser/Parser.java index e127192510..7218a6ded1 100644 --- a/src/main/java/seedu/cafectrl/parser/Parser.java +++ b/src/main/java/seedu/cafectrl/parser/Parser.java @@ -72,11 +72,12 @@ public class Parser implements ParserUtil { + "qty/([A-Za-z0-9\\s]+)"; /** The rest of Command Handler Patterns*/ + private static final String LIST_INGREDIENTS_ARGUMENT_STRING = "dish/(.+)"; - private static final String DELETE_ARGUMENT_STRING = "(\\d+)"; + private static final String DELETE_ARGUMENT_STRING = "(.+)"; private static final String EDIT_PRICE_ARGUMENT_STRING = "dish/(.*)\\sprice/(.*)"; - private static final String BUY_INGREDIENT_ARGUMENT_STRING = "(ingredient/[A-Za-z0-9\\s]+ qty/[A-Za-z0-9\\s]+" - + "(?:, ingredient/[A-Za-z0-9\\s]+ qty/[A-Za-z0-9\\s]+)*)"; + private static final String BUY_INGREDIENT_ARGUMENT_STRING = "(ingredient/[A-Za-z0-9\\s]+ qty/.+" + + "(?:, ingredient/[A-Za-z0-9\\s]+ qty/.+)*)"; private static final String SHOW_SALE_BY_DAY_ARGUMENT_STRING = "day/(.+)"; private static final int MIN_QTY = 1; private static final int MAX_QTY = 1000000; @@ -617,13 +618,16 @@ private static Command prepareDelete(Menu menu, String arguments, Ui ui) { } int listIndexArgGroup = 1; - int dishIndex = Integer.parseInt(matcher.group(listIndexArgGroup)); - if (!menu.isValidDishIndex(dishIndex)) { - return new IncorrectCommand(ErrorMessages.INVALID_DISH_INDEX, ui); + try { + int dishIndex = Integer.parseInt(matcher.group(listIndexArgGroup)); + if (!menu.isValidDishIndex(dishIndex)) { + return new IncorrectCommand(ErrorMessages.INVALID_DISH_INDEX, ui); + } + return new DeleteDishCommand(dishIndex, menu, ui); + } catch (NumberFormatException e) { + return new IncorrectCommand(ErrorMessages.DISH_INDEX_NOT_INT, ui); } - - return new DeleteDishCommand(dishIndex, menu, ui); } /** @@ -652,8 +656,7 @@ private static Command prepareBuyIngredient(String arguments, Ui ui, Pantry pant if (!matcher.matches()) { logger.warning("Unmatched regex!"); - return new IncorrectCommand(ErrorMessages.INVALID_ARGUMENT_FOR_BUY_INGREDIENT - + BuyIngredientCommand.MESSAGE_USAGE, ui); + return new IncorrectCommand(ErrorMessages.INVALID_INGREDIENT_ARGUMENTS, ui); } String ingredientsListString = matcher.group(0); @@ -661,6 +664,8 @@ private static Command prepareBuyIngredient(String arguments, Ui ui, Pantry pant try { ArrayList ingredients = parseIngredients(ingredientsListString, false, menu); return new BuyIngredientCommand(ingredients, ui, pantry); + } catch (NumberFormatException e) { + return new IncorrectCommand(ErrorMessages.INVALID_INGREDIENT_ARGUMENTS, ui); } catch (Exception e) { return new IncorrectCommand(e.getMessage(), ui); } diff --git a/src/main/java/seedu/cafectrl/storage/Decoder.java b/src/main/java/seedu/cafectrl/storage/Decoder.java index 030959f900..939c0ddbaf 100644 --- a/src/main/java/seedu/cafectrl/storage/Decoder.java +++ b/src/main/java/seedu/cafectrl/storage/Decoder.java @@ -61,13 +61,16 @@ private static void decodeDishString(String dishString, ArrayList menuDish String dishName = ""; try { String[] dishStringArray = dishString.split(DIVIDER); - dishName = dishStringArray[0].trim(); + dishName = dishStringArray[0].trim().toLowerCase(); checkNameValidity(dishName); - float dishPrice = Float.parseFloat(dishStringArray[1]); + float dishPrice = Parser.parsePriceToFloat(dishStringArray[1]); String[] ingredientStringArray = Arrays.copyOfRange(dishStringArray, 2, dishStringArray.length); ArrayList ingredientsList = decodeIngredientData(ingredientStringArray); menuDishList.add(new Dish(dishName, ingredientsList, dishPrice)); } catch (ParserException e) { + logger.log(Level.WARNING, "Dish has invalid price: " + e.getMessage(), e); + ui.showToUser(ErrorMessages.INVALID_MENU_DATA + dishString); + } catch (RuntimeException e) { logger.log(Level.WARNING, "Dish has no ingredients: " + e.getMessage(), e); ui.showToUser(e.getMessage() + dishName); } catch (Exception e) { @@ -92,13 +95,13 @@ private static ArrayList decodeIngredientData(String[] ingredientsSt ArrayList ingredientList = new ArrayList<>(); if (ingredientsStringArray.length < 1) { - throw new ParserException(ErrorMessages.MISSING_INGREDIENT_MENU_DATA); + throw new RuntimeException(ErrorMessages.MISSING_INGREDIENT_MENU_DATA); } for(String ingredientString : ingredientsStringArray) { logger.info("Ingredient to decode: " + ingredientString); String[] array = ingredientString.split(INGREDIENT_DIVIDER); - String name = array[0].trim(); + String name = array[0].trim().toLowerCase(); checkNameValidity(name); int qty = Integer.parseInt(array[1].trim()); checkQtyValidity(qty); diff --git a/src/main/java/seedu/cafectrl/ui/ErrorMessages.java b/src/main/java/seedu/cafectrl/ui/ErrorMessages.java index a37a5774e0..55aed8bc61 100644 --- a/src/main/java/seedu/cafectrl/ui/ErrorMessages.java +++ b/src/main/java/seedu/cafectrl/ui/ErrorMessages.java @@ -36,8 +36,6 @@ public class ErrorMessages { + "for list ingredients command."; public static final String MISSING_ARGUMENT_FOR_DELETE = "Error: Missing arguments " + "for delete command."; - public static final String INVALID_ARGUMENT_FOR_BUY_INGREDIENT = "Error: Invalid arguments " - + "for buy ingredient command."; public static final String WRONG_DISH_INDEX_TYPE_FOR_EDIT_PRICE = "Something is wrong with " + "the arguments! The types for dish and price are integer and float respectively, \n" + "and do not type in duplicated arguments at one time!"; @@ -109,4 +107,5 @@ public class ErrorMessages { public static final String MISSING_FILEPATH = "Error in FileManager: No File Path entered"; public static final String MISSING_INGREDIENT_MENU_DATA = "menu.txt: Missing ingredients, " + "this dish will be removed -> "; + public static final String DISH_INDEX_NOT_INT = "Sorry! Dish index has to be an int!"; } diff --git a/src/test/java/seedu/cafectrl/command/BuyIngredientCommandTest.java b/src/test/java/seedu/cafectrl/command/BuyIngredientCommandTest.java index 0bfa6dd8a8..d6c3295efc 100644 --- a/src/test/java/seedu/cafectrl/command/BuyIngredientCommandTest.java +++ b/src/test/java/seedu/cafectrl/command/BuyIngredientCommandTest.java @@ -36,9 +36,9 @@ void execute_validInput_returnCorrectOutput() { System.setOut(originalOut); String expectedOutput = "Added to stock: \n" - + "Ingredient: milk\t\tQty: 100ml\n" - + "Ingredient: rice\t\tQty: 1000g\n" - + "Ingredient: chicken\t\tQty: 500g"; + + "Ingredient: milk\nTotal Qty: 100ml\n\n" + + "Ingredient: rice\nTotal Qty: 1000g\n\n" + + "Ingredient: chicken\nTotal Qty: 500g\n\n"; assertEquals(expectedOutput.trim().replaceAll("\\s+", " "), actualOutput.trim().replaceAll("\\s+", " ")); @@ -65,7 +65,7 @@ void execute_validInputWithExistingIngredient_returnCorrectOutput() { System.setOut(originalOut); String expectedOutput = "Added to stock: \n" - + "Ingredient: chicken\t\tQty: 1000g\n"; + + "Ingredient: chicken\nTotal Qty: 1000g\n"; assertEquals(expectedOutput.trim().replaceAll("\\s+", " "), actualOutput.trim().replaceAll("\\s+", " ")); diff --git a/src/test/java/seedu/cafectrl/parser/ParserTest.java b/src/test/java/seedu/cafectrl/parser/ParserTest.java index df30d0ab4b..bdf4e2cdb0 100644 --- a/src/test/java/seedu/cafectrl/parser/ParserTest.java +++ b/src/test/java/seedu/cafectrl/parser/ParserTest.java @@ -2,7 +2,6 @@ import org.junit.jupiter.api.Test; import seedu.cafectrl.command.AddDishCommand; -import seedu.cafectrl.command.BuyIngredientCommand; import seedu.cafectrl.command.DeleteDishCommand; import seedu.cafectrl.command.ListIngredientCommand; import seedu.cafectrl.command.ListSaleByDayCommand; @@ -158,7 +157,7 @@ public void parseCommand_missingDeleteIndex_returnsErrorMessage() { } @Test - public void parseCommand_invalidDeleteIndex_returnsErrorMessage() { + public void parseCommand_notIntDeleteIndex_returnsErrorMessage() { Menu menu = new Menu(); Ui ui = new Ui(); Pantry pantry = new Pantry(ui); @@ -172,7 +171,25 @@ public void parseCommand_invalidDeleteIndex_returnsErrorMessage() { IncorrectCommand incorrectCommand = (IncorrectCommand) result; String feedbackToUser = incorrectCommand.feedbackToUser; - assertEquals(ErrorMessages.MISSING_ARGUMENT_FOR_DELETE, feedbackToUser); + assertEquals(ErrorMessages.DISH_INDEX_NOT_INT, feedbackToUser); + } + + @Test + public void parseCommand_invalidDeleteIndex_returnsErrorMessage() { + Menu menu = new Menu(); + Ui ui = new Ui(); + Pantry pantry = new Pantry(ui); + Sales sales = new Sales(); + CurrentDate currentDate = new CurrentDate(); + String userInput = "delete -1"; + ParserUtil parserUtil = new Parser(); + Command result = parserUtil.parseCommand(menu, userInput, ui, pantry, sales, currentDate); + + assertTrue(result instanceof IncorrectCommand); + + IncorrectCommand incorrectCommand = (IncorrectCommand) result; + String feedbackToUser = incorrectCommand.feedbackToUser; + assertEquals(ErrorMessages.INVALID_DISH_INDEX, feedbackToUser); } @Test @@ -968,8 +985,7 @@ void parseCommand_missingArgsForBuyIngredient_returnErrorMessage() { IncorrectCommand incorrectCommand = (IncorrectCommand) result; String feedbackToUser = incorrectCommand.feedbackToUser; - assertEquals(ErrorMessages.INVALID_ARGUMENT_FOR_BUY_INGREDIENT - + BuyIngredientCommand.MESSAGE_USAGE, feedbackToUser); + assertEquals(ErrorMessages.INVALID_INGREDIENT_ARGUMENTS, feedbackToUser); } @Test