From e896b7be9f037ff1013c43d5cc7351b3475aaad1 Mon Sep 17 00:00:00 2001 From: ShaniceTang Date: Mon, 6 Nov 2023 23:06:39 +0800 Subject: [PATCH 01/11] Add more specific error message for units Fixes #265 --- .../ListIngredientCommand_execute.puml | 2 +- .../command/BuyIngredientCommand.java | 2 +- .../java/seedu/cafectrl/parser/Parser.java | 61 ++++++------------- .../java/seedu/cafectrl/ui/ErrorMessages.java | 7 +++ 4 files changed, 28 insertions(+), 44 deletions(-) diff --git a/docs/diagrams/sequence/ListIngredientCommand_execute.puml b/docs/diagrams/sequence/ListIngredientCommand_execute.puml index da32337fb0..62d0919c2c 100644 --- a/docs/diagrams/sequence/ListIngredientCommand_execute.puml +++ b/docs/diagrams/sequence/ListIngredientCommand_execute.puml @@ -1,4 +1,4 @@ -@startuml + @startuml !define COMMAND class ListIngredientCommand !define UI class Ui !define MENU class Menu diff --git a/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java b/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java index edb3bac19b..37009ca313 100644 --- a/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java +++ b/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java @@ -17,7 +17,7 @@ public class BuyIngredientCommand extends Command { + COMMAND_WORD + " ingredient/INGREDIENT1_NAME qty/INGREDIENT1_QTY" + "[, ingredient/INGREDIENT2_NAME, qty/INGREDIENT2_QTY...]\n" + "Example:" - + COMMAND_WORD + " ingredient/rice qty/200g, ingredient/chicken qty/100g"; + + COMMAND_WORD + " ingredient/milk qty/200ml, ingredient/chicken qty/100g"; protected Ui ui; protected Pantry pantry; diff --git a/src/main/java/seedu/cafectrl/parser/Parser.java b/src/main/java/seedu/cafectrl/parser/Parser.java index c41951926c..cdc2e0a51c 100644 --- a/src/main/java/seedu/cafectrl/parser/Parser.java +++ b/src/main/java/seedu/cafectrl/parser/Parser.java @@ -55,7 +55,7 @@ public class Parser implements ParserUtil { private static final String PRICE_MATCHER_GROUP_LABEL = "dishPrice"; private static final String INGREDIENTS_MATCHER_GROUP_LABEL = "ingredients"; private static final String INGREDIENT_ARGUMENT_STRING = "\\s*ingredient/(?[A-Za-z0-9\\s]+) " - + "qty/\\s*(?[0-9]+)\\s*(?g|ml)\\s*"; + + "qty/\\s*(?[0-9]+)\\s*(?[A-Za-z]*)\\s*"; private static final String INGREDIENT_NAME_REGEX_GROUP_LABEL = "ingredientName"; private static final String INGREDIENT_QTY_REGEX_GROUP_LABEL = "ingredientQty"; private static final String INGREDIENT_UNIT_REGEX_GROUP_LABEL = "ingredientUnit"; @@ -257,8 +257,7 @@ private static ArrayList parseIngredients(String ingredientsListStri Matcher ingredientMatcher = ingredientPattern.matcher(inputIngredient); if (!ingredientMatcher.matches()) { - throw new ParserException(ErrorMessages.INVALID_ADD_DISH_FORMAT_MESSAGE - + AddDishCommand.MESSAGE_USAGE); + throw new ParserException(ErrorMessages.INVALID_INGREDIENT_ARGUMENTS); } String ingredientName = ingredientMatcher.group(INGREDIENT_NAME_REGEX_GROUP_LABEL).trim(); @@ -275,6 +274,14 @@ private static ArrayList parseIngredients(String ingredientsListStri continue; } + if (isEmptyUnit(ingredientUnit)) { + throw new ParserException(ErrorMessages.EMPTY_UNIT_MESSAGE); + } + + if (!isValidUnit(ingredientUnit)) { + throw new ParserException(ErrorMessages.INVALID_UNIT_MESSAGE); + } + Ingredient ingredient = new Ingredient(ingredientName, ingredientQty, ingredientUnit); ingredients.add(ingredient); @@ -427,7 +434,6 @@ private static Command prepareBuyIngredient(String arguments, Ui ui, Pantry pant Matcher matcher = buyIngredientArgumentsPattern.matcher(arguments.trim()); if (!matcher.matches()) { - return new IncorrectCommand(ErrorMessages.MISSING_ARGUMENT_FOR_BUY_INGREDIENT + BuyIngredientCommand.MESSAGE_USAGE, ui); } @@ -438,11 +444,18 @@ private static Command prepareBuyIngredient(String arguments, Ui ui, Pantry pant ArrayList ingredients = parseIngredients(ingredientsListString); return new BuyIngredientCommand(ingredients, ui, pantry); } catch (Exception e) { - return new IncorrectCommand(ErrorMessages.INVALID_ARGUMENT_FOR_BUY_INGREDIENT - + BuyIngredientCommand.MESSAGE_USAGE, ui); + return new IncorrectCommand(e.getMessage(), ui); } } + private static boolean isValidUnit(String ingredientUnit) { + return ingredientUnit.equals("g") || ingredientUnit.equals("ml"); + } + + private static boolean isEmptyUnit(String ingredientUnit) { + return ingredientUnit.equals(""); + } + //@@author ziyi105 private static Command prepareHelpCommand(Ui ui) { return new HelpCommand(ui); @@ -567,40 +580,4 @@ private static OrderList setOrderList(CurrentDate currentDate, Sales sales) { int currentDay = currentDate.getCurrentDay(); return sales.getOrderList(currentDay); } - - //@@author Cazh1 - /** - * Parses the given arguments string to identify task index number. - * - * @param userInput arguments string to parse as index number - * @param command expected String name of the command called - * @return the parsed index number - * @throws ParseException if no region of the args string could be found for the index - * @throws NumberFormatException the args string region is not a valid number - */ - private static int parseArgsAsDisplayedIndex(String userInput, String command) - throws ParseException, NumberFormatException { - String formattedString = userInput.replace(command, "").trim(); - return Integer.parseInt(formattedString); - } - - //@@author ShaniceTang - /** - * Extracts the quantity (numeric part) from a given string containing both quantity and unit. - * @param qty A string containing both quantity and unit (e.g., "100g"). - * @return An integer representing the extracted quantity. - */ - public static int extractQty(String qty) { - return Integer.parseInt(qty.replaceAll("[^0-9]", "")); - } - - //@@author ShaniceTang - /** - * Extracts the unit (non-numeric part) from a given string containing both quantity and unit. - * @param qty A string containing both quantity and unit (e.g., "100g"). - * @return A string representing the extracted unit. - */ - public static String extractUnit(String qty) { - return qty.replaceAll("[0-9]", ""); - } } diff --git a/src/main/java/seedu/cafectrl/ui/ErrorMessages.java b/src/main/java/seedu/cafectrl/ui/ErrorMessages.java index e145b8556e..b49408613b 100644 --- a/src/main/java/seedu/cafectrl/ui/ErrorMessages.java +++ b/src/main/java/seedu/cafectrl/ui/ErrorMessages.java @@ -45,4 +45,11 @@ public class ErrorMessages { + "command.\n"; public static final String INVALID_DAY_FORMAT = "Sorry, please enter a valid integer for the 'day' field!"; public static final String EDIT_SAME_PRICE = "New price is exactly the same as old price, is that what you want?"; + public static final String EMPTY_UNIT_MESSAGE = "Unit cannot be empty! Please use either g or ml :)"; + public static final String INVALID_UNIT_MESSAGE = "Invalid unit! Please use either g or ml :)"; + public static final String INVALID_INGREDIENT_ARGUMENTS = "Invalid arguments for ingredients!\n" + + "Ingredient format: ingredient/INGREDIENT1_NAME qty/INGREDIENT1_QTY" + + "[, ingredient/INGREDIENT2_NAME, qty/INGREDIENT2_QTY...]\n" + + "Example: ingredient/milk qty/200ml, ingredient/chicken qty/100g"; + } From 3aff252fa4f50e64d5b8e70611f892e82b19925e Mon Sep 17 00:00:00 2001 From: ShaniceTang Date: Mon, 6 Nov 2023 23:07:21 +0800 Subject: [PATCH 02/11] Update buy ingredient feature description in UserGuide.md Fixes #254 --- docs/UserGuide.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/UserGuide.md b/docs/UserGuide.md index 78e30dfa12..2b27fec9da 100644 --- a/docs/UserGuide.md +++ b/docs/UserGuide.md @@ -94,7 +94,7 @@ Format: `add name/DISH_NAME price/PRICE ingredient/INGREDIENT1_NAME qty/INGREDIE * The `PRICE` must be a positive 2 decimal place number. -* The `IMGREDIENT_QTY` must contain the unit ml or g specifically. +* The `INGREDIENT_QTY` must contain the unit ml or g specifically. * e.g. `qty/50g` or `qty/1000ml` Example: @@ -199,6 +199,11 @@ Adds one or more ingredients to the pantry Format: `buy_ingredient ingredient/INGREDIENT1_NAME qty/INGREDIENT1_QTY[, ingredient/INGREDIENT2_NAME qty/INGREDIENT2_QTY, ...]` +* `INGREDIENT_QTY` must have the following format: + * Must be an integer + * Must contain the unit ml or g specifically + * e.g. `qty/50g` or `qty/1000ml` + Example: `buy_ingredient ingredient/chicken qty/500g, ingredient/milk qty/1000ml` Output: From ed91421cbc6411432290ce891feef6f34b7fc25d Mon Sep 17 00:00:00 2001 From: ShaniceTang Date: Mon, 6 Nov 2023 23:22:59 +0800 Subject: [PATCH 03/11] Improve view stock functionality Fixes #246, Fixes #226 --- .../java/seedu/cafectrl/command/ViewTotalStockCommand.java | 7 ++++++- src/main/java/seedu/cafectrl/ui/Messages.java | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/cafectrl/command/ViewTotalStockCommand.java b/src/main/java/seedu/cafectrl/command/ViewTotalStockCommand.java index 6cd6d1ec88..8be0ba4eca 100644 --- a/src/main/java/seedu/cafectrl/command/ViewTotalStockCommand.java +++ b/src/main/java/seedu/cafectrl/command/ViewTotalStockCommand.java @@ -7,6 +7,7 @@ import java.util.ArrayList; +//@@author ShaniceTang public class ViewTotalStockCommand extends Command { public static final String COMMAND_WORD = "view_stock"; @@ -22,9 +23,13 @@ public ViewTotalStockCommand(Pantry pantry, Ui ui) { @Override public void execute() { - ui.showToUser(Messages.VIEW_STOCK); pantryStock = pantry.getPantryStock(); + if (pantryStock.isEmpty()) { + ui.showToUser(Messages.EMPTY_STOCK); + return; + } + ui.showToUser(Messages.VIEW_STOCK); for (Ingredient ingredient : pantryStock) { ui.showToUser(ingredient.getName() + "\t\t\t" + ingredient.getQty() + ingredient.getUnit()); } diff --git a/src/main/java/seedu/cafectrl/ui/Messages.java b/src/main/java/seedu/cafectrl/ui/Messages.java index 3a2a2bdf41..0590681b0c 100644 --- a/src/main/java/seedu/cafectrl/ui/Messages.java +++ b/src/main/java/seedu/cafectrl/ui/Messages.java @@ -25,6 +25,7 @@ public class Messages { /** Messages for view stock command */ public static final String VIEW_STOCK = "You have the following ingredients in pantry:" + "\nIngredients\t\tQty"; + public static final String EMPTY_STOCK = "Sorry! Pantry is currently empty!"; /** Messages for help command */ public static final String LIST_OF_COMMANDS = "These are all the commands I recognise: "; From ca603d470fd7827a9dda512b18c1ebb5c8d0e6bb Mon Sep 17 00:00:00 2001 From: ShaniceTang Date: Mon, 6 Nov 2023 23:23:18 +0800 Subject: [PATCH 04/11] Add author tag for delete command class --- src/main/java/seedu/cafectrl/command/DeleteDishCommand.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/seedu/cafectrl/command/DeleteDishCommand.java b/src/main/java/seedu/cafectrl/command/DeleteDishCommand.java index 1e59594b06..d400d957a0 100644 --- a/src/main/java/seedu/cafectrl/command/DeleteDishCommand.java +++ b/src/main/java/seedu/cafectrl/command/DeleteDishCommand.java @@ -5,6 +5,7 @@ import seedu.cafectrl.ui.ErrorMessages; import seedu.cafectrl.ui.Ui; +//@@author ShaniceTang /** * Deletes a menu item identified using it's last displayed index from the menu. */ From 0b274efa3e06bfcf4c47b8982c9013a9cdda92b9 Mon Sep 17 00:00:00 2001 From: ShaniceTang Date: Tue, 7 Nov 2023 16:00:08 +0800 Subject: [PATCH 05/11] Handle invalid ingredient quantity Fixes #227 --- docs/UserGuide.md | 4 +--- src/main/java/seedu/cafectrl/parser/Parser.java | 15 ++++++++++++++- .../java/seedu/cafectrl/ui/ErrorMessages.java | 2 +- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/docs/UserGuide.md b/docs/UserGuide.md index 2b27fec9da..0ce826ce3b 100644 --- a/docs/UserGuide.md +++ b/docs/UserGuide.md @@ -199,9 +199,7 @@ Adds one or more ingredients to the pantry Format: `buy_ingredient ingredient/INGREDIENT1_NAME qty/INGREDIENT1_QTY[, ingredient/INGREDIENT2_NAME qty/INGREDIENT2_QTY, ...]` -* `INGREDIENT_QTY` must have the following format: - * Must be an integer - * Must contain the unit ml or g specifically +* `INGREDIENT_QTY` must contain the unit ml or g specifically * e.g. `qty/50g` or `qty/1000ml` Example: `buy_ingredient ingredient/chicken qty/500g, ingredient/milk qty/1000ml` diff --git a/src/main/java/seedu/cafectrl/parser/Parser.java b/src/main/java/seedu/cafectrl/parser/Parser.java index cdc2e0a51c..f8b00c0b94 100644 --- a/src/main/java/seedu/cafectrl/parser/Parser.java +++ b/src/main/java/seedu/cafectrl/parser/Parser.java @@ -31,7 +31,6 @@ import seedu.cafectrl.data.dish.Ingredient; import seedu.cafectrl.ui.Ui; -import java.text.ParseException; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -71,8 +70,11 @@ public class Parser implements ParserUtil { private static final String LIST_INGREDIENTS_ARGUMENT_STRING = "(\\d+)"; private static final String DELETE_ARGUMENT_STRING = "(\\d+)"; private static final String EDIT_PRICE_ARGUMENT_STRING = "index/(.*) price/(.*)"; + 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 SHOW_SALE_BY_DAY_ARGUMENT_STRING = "day/(\\d+)"; //@@author ziyi105 @@ -274,6 +276,10 @@ private static ArrayList parseIngredients(String ingredientsListStri continue; } + if (isInvalidQty(ingredientQty)) { + throw new ParserException(ErrorMessages.INVALID_INGREDIENT_QTY); + } + if (isEmptyUnit(ingredientUnit)) { throw new ParserException(ErrorMessages.EMPTY_UNIT_MESSAGE); } @@ -290,6 +296,7 @@ private static ArrayList parseIngredients(String ingredientsListStri return ingredients; } + /** * Converts text of price to float while also checking if the price input is within reasonable range * @param priceText text input for price argument @@ -443,6 +450,8 @@ private static Command prepareBuyIngredient(String arguments, Ui ui, Pantry pant try { ArrayList ingredients = parseIngredients(ingredientsListString); return new BuyIngredientCommand(ingredients, ui, pantry); + } catch (NumberFormatException e) { + return new IncorrectCommand(ErrorMessages.INVALID_INGREDIENT_QTY, ui); } catch (Exception e) { return new IncorrectCommand(e.getMessage(), ui); } @@ -456,6 +465,10 @@ private static boolean isEmptyUnit(String ingredientUnit) { return ingredientUnit.equals(""); } + private static boolean isInvalidQty(int ingredientQty) { + return ingredientQty < 1; + } + //@@author ziyi105 private static Command prepareHelpCommand(Ui ui) { return new HelpCommand(ui); diff --git a/src/main/java/seedu/cafectrl/ui/ErrorMessages.java b/src/main/java/seedu/cafectrl/ui/ErrorMessages.java index b49408613b..f974fcdca3 100644 --- a/src/main/java/seedu/cafectrl/ui/ErrorMessages.java +++ b/src/main/java/seedu/cafectrl/ui/ErrorMessages.java @@ -51,5 +51,5 @@ public class ErrorMessages { + "Ingredient format: ingredient/INGREDIENT1_NAME qty/INGREDIENT1_QTY" + "[, ingredient/INGREDIENT2_NAME, qty/INGREDIENT2_QTY...]\n" + "Example: ingredient/milk qty/200ml, ingredient/chicken qty/100g"; - + public static final String INVALID_INGREDIENT_QTY = "Invalid quantity! Please enter a valid quantity :)"; } From 48a1ad9d018db43407eec02aba5992887829f33c Mon Sep 17 00:00:00 2001 From: ShaniceTang Date: Wed, 8 Nov 2023 13:22:08 +0800 Subject: [PATCH 06/11] Handle ingredient quantity entered by user is too large bug --- src/main/java/seedu/cafectrl/parser/Parser.java | 2 +- src/main/java/seedu/cafectrl/ui/ErrorMessages.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/cafectrl/parser/Parser.java b/src/main/java/seedu/cafectrl/parser/Parser.java index c83f2d66fa..18fdef7d03 100644 --- a/src/main/java/seedu/cafectrl/parser/Parser.java +++ b/src/main/java/seedu/cafectrl/parser/Parser.java @@ -496,7 +496,7 @@ private static boolean isEmptyUnit(String ingredientUnit) { } private static boolean isInvalidQty(int ingredientQty) { - return ingredientQty < 1; + return ingredientQty < 1 || ingredientQty > 1000000; } //@@author ziyi105 diff --git a/src/main/java/seedu/cafectrl/ui/ErrorMessages.java b/src/main/java/seedu/cafectrl/ui/ErrorMessages.java index d7219caa5d..911eecb133 100644 --- a/src/main/java/seedu/cafectrl/ui/ErrorMessages.java +++ b/src/main/java/seedu/cafectrl/ui/ErrorMessages.java @@ -59,7 +59,7 @@ public class ErrorMessages { + "Ingredient format: ingredient/INGREDIENT1_NAME qty/INGREDIENT1_QTY" + "[, ingredient/INGREDIENT2_NAME, qty/INGREDIENT2_QTY...]\n" + "Example: ingredient/milk qty/200ml, ingredient/chicken qty/100g"; - public static final String INVALID_INGREDIENT_QTY = "Invalid quantity! Please enter a valid quantity :)"; + public static final String INVALID_INGREDIENT_QTY = "Quantity out of range! Quantity range is 1 to 1000000 :)"; public static final String INVALID_SHOW_SALE_DAY_FORMAT_MESSAGE = "Error: " + "Incorrect format for the show_sale command.\n"; public static final String INVALID_DAY_FORMAT = "Sorry, please enter a valid integer " From f27a45d17cf63c4d6f643510aa2436e6a76a28cb Mon Sep 17 00:00:00 2001 From: ShaniceTang Date: Wed, 8 Nov 2023 13:22:48 +0800 Subject: [PATCH 07/11] Remove repeated printing of same ingredients of buyIngredient command --- .../command/BuyIngredientCommand.java | 31 ++++++++++++++----- .../java/seedu/cafectrl/parser/Parser.java | 9 +++--- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java b/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java index 37009ca313..6f7574227a 100644 --- a/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java +++ b/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java @@ -22,6 +22,7 @@ public class BuyIngredientCommand extends Command { protected Ui ui; protected Pantry pantry; private ArrayList ingredients; + private ArrayList ingredientsToBePrinted = new ArrayList<>(); private String ingredientString = ""; // Used to store the message about the bought ingredients /** @@ -45,7 +46,7 @@ public void execute() { try { addIngredient(); ui.printBuyIngredientHeader(); - ui.showToUser(ingredientString); + ui.showToUser(ingredientString.strip()); } catch (RuntimeException e) { ui.showToUser(e.getMessage()); } @@ -61,7 +62,12 @@ private void addIngredient() { ingredient = pantry.addIngredientToStock(ingredient.getName(), ingredient.getQty(), ingredient.getUnit()); - buildBuyIngredientMessage(ingredient, i); + ingredients.set(i, ingredient); + } + + for (int i = ingredients.size() - 1; i > -1; i--) { + Ingredient ingredient = ingredients.get(i); + buildBuyIngredientMessage(ingredient); } } @@ -69,17 +75,26 @@ private void addIngredient() { * Builds a message about the bought ingredient and appends it to the result message. * * @param ingredient The Ingredient object to build the message for. - * @param index The index of the ingredient in the list. */ - private void buildBuyIngredientMessage(Ingredient ingredient, int index) { + private void buildBuyIngredientMessage(Ingredient ingredient) { + if (isReapeatedIngredient(ingredient)) { + return; + } + ingredientsToBePrinted.add(ingredient); ingredientString += "Ingredient: " + ingredient.getName() + "\t\tQty: " + ingredient.getQty() - + ingredient.getUnit(); + + ingredient.getUnit() + "\n"; + } - //append new line if current ingredient is not last - if(index < ingredients.size() - ui.OFFSET_LIST_INDEX) { - ingredientString += "\n"; + private boolean isReapeatedIngredient(Ingredient ingredient) { + for (Ingredient printedIngredient : ingredientsToBePrinted) { + String printedIngredientName = printedIngredient.getName(); + String ingredientName = ingredient.getName(); + if (printedIngredientName.equalsIgnoreCase(ingredientName)) { + return true; + } } + return false; } } diff --git a/src/main/java/seedu/cafectrl/parser/Parser.java b/src/main/java/seedu/cafectrl/parser/Parser.java index 18fdef7d03..a5b6d58bb0 100644 --- a/src/main/java/seedu/cafectrl/parser/Parser.java +++ b/src/main/java/seedu/cafectrl/parser/Parser.java @@ -232,7 +232,7 @@ private static Command prepareAdd(String arguments, Menu menu, Ui ui) { throw new ParserException(Messages.REPEATED_DISH_MESSAGE); } - ArrayList ingredients = parseIngredients(ingredientsListString); + ArrayList ingredients = parseIngredients(ingredientsListString, true); Dish dish = new Dish(dishName, ingredients, price); @@ -251,9 +251,10 @@ private static Command prepareAdd(String arguments, Menu menu, Ui ui) { * @throws IllegalArgumentException if the input string of ingredients is in an incorrect format. * @throws ParserException if the input string does not match the constraints */ - private static ArrayList parseIngredients(String ingredientsListString) + private static ArrayList parseIngredients(String ingredientsListString, boolean excludeRepeatedIngredients) throws IllegalArgumentException, ParserException { String[] inputIngredientList = {ingredientsListString}; + ArrayList ingredients = new ArrayList<>(); //check if there is more than 1 ingredient @@ -281,7 +282,7 @@ private static ArrayList parseIngredients(String ingredientsListStri throw new ParserException(ErrorMessages.INVALID_INGREDIENT_NAME_LENGTH_MESSAGE); } - if (isRepeatedIngredientName(ingredientName, ingredients)) { + if (excludeRepeatedIngredients && isRepeatedIngredientName(ingredientName, ingredients)) { continue; } @@ -478,7 +479,7 @@ private static Command prepareBuyIngredient(String arguments, Ui ui, Pantry pant String ingredientsListString = matcher.group(0); try { - ArrayList ingredients = parseIngredients(ingredientsListString); + ArrayList ingredients = parseIngredients(ingredientsListString, false); return new BuyIngredientCommand(ingredients, ui, pantry); } catch (NumberFormatException e) { return new IncorrectCommand(ErrorMessages.INVALID_INGREDIENT_QTY, ui); From d02e5e5de7d4d4045da42d2e0e0f367564c16aa4 Mon Sep 17 00:00:00 2001 From: ShaniceTang Date: Wed, 8 Nov 2023 13:49:53 +0800 Subject: [PATCH 08/11] Improve coding standard and quality --- src/main/java/seedu/cafectrl/parser/Parser.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/cafectrl/parser/Parser.java b/src/main/java/seedu/cafectrl/parser/Parser.java index cf510a9847..33e6e3492c 100644 --- a/src/main/java/seedu/cafectrl/parser/Parser.java +++ b/src/main/java/seedu/cafectrl/parser/Parser.java @@ -257,7 +257,8 @@ private static Command prepareAdd(String arguments, Menu menu, Ui ui) { * @throws IllegalArgumentException if the input string of ingredients is in an incorrect format. * @throws ParserException if the input string does not match the constraints */ - private static ArrayList parseIngredients(String ingredientsListString, boolean excludeRepeatedIngredients) + private static ArrayList parseIngredients(String ingredientsListString, + boolean excludeRepeatedIngredients) throws IllegalArgumentException, ParserException { String[] inputIngredientList = {ingredientsListString}; From 0bdc88e3641e77e29735736e061cdc588edd011c Mon Sep 17 00:00:00 2001 From: ShaniceTang Date: Wed, 8 Nov 2023 14:09:28 +0800 Subject: [PATCH 09/11] Remove merge conflict tags --- docs/UserGuide.md | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/docs/UserGuide.md b/docs/UserGuide.md index 68d97fcd75..8527baccc8 100644 --- a/docs/UserGuide.md +++ b/docs/UserGuide.md @@ -94,15 +94,9 @@ Adds a dish consisting of its ingredients to the menu Format: `add name/DISH_NAME price/PRICE ingredient/INGREDIENT1_NAME qty/INGREDIENT1_QTY[, ingredient/INGREDIENT2_NAME qty/INGREDIENT2_QTY, ...]` -<<<<<<< HEAD - -* The `PRICE` must be a positive 2 decimal place number. -* The `INGREDIENT_QTY` must contain the unit ml or g specifically. -======= * `DISH_NAME` * `PRICE` must be a positive number and can be up to 2 decimal places. -* `IMGREDIENT_QTY` must contain the unit ml or g specifically. ->>>>>>> 82f16561f679acc71d616f7eb76a00c2e37361bb +* `INGREDIENT_QTY` must contain the unit ml or g specifically. * e.g. `qty/50g` or `qty/1000ml` Example: From 8cd632ac871547841077be29cf37a9d798238e59 Mon Sep 17 00:00:00 2001 From: ShaniceTang Date: Wed, 8 Nov 2023 14:29:28 +0800 Subject: [PATCH 10/11] Remove repeated method isRepeatedIngredient --- .../cafectrl/command/BuyIngredientCommand.java | 14 ++------------ src/main/java/seedu/cafectrl/parser/Parser.java | 2 +- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java b/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java index 6f7574227a..604ca5f7d8 100644 --- a/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java +++ b/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java @@ -3,6 +3,7 @@ import seedu.cafectrl.data.Pantry; import seedu.cafectrl.data.dish.Ingredient; import seedu.cafectrl.ui.Ui; +import seedu.cafectrl.parser.Parser; import java.util.ArrayList; @@ -77,7 +78,7 @@ private void addIngredient() { * @param ingredient The Ingredient object to build the message for. */ private void buildBuyIngredientMessage(Ingredient ingredient) { - if (isReapeatedIngredient(ingredient)) { + if (Parser.isRepeatedIngredientName(ingredient.getName(), ingredientsToBePrinted)) { return; } ingredientsToBePrinted.add(ingredient); @@ -85,16 +86,5 @@ private void buildBuyIngredientMessage(Ingredient ingredient) { + "\t\tQty: " + ingredient.getQty() + ingredient.getUnit() + "\n"; } - - private boolean isReapeatedIngredient(Ingredient ingredient) { - for (Ingredient printedIngredient : ingredientsToBePrinted) { - String printedIngredientName = printedIngredient.getName(); - String ingredientName = ingredient.getName(); - if (printedIngredientName.equalsIgnoreCase(ingredientName)) { - return true; - } - } - return false; - } } diff --git a/src/main/java/seedu/cafectrl/parser/Parser.java b/src/main/java/seedu/cafectrl/parser/Parser.java index 33e6e3492c..f21c991fd0 100644 --- a/src/main/java/seedu/cafectrl/parser/Parser.java +++ b/src/main/java/seedu/cafectrl/parser/Parser.java @@ -386,7 +386,7 @@ static boolean isRepeatedDishName(String inputDishName, Menu menu) throws NullPo * @return true if ingredient name already exists in menu, false otherwise * @throws NullPointerException if the input string is null */ - static boolean isRepeatedIngredientName(String inputName, ArrayList ingredients) + public static boolean isRepeatedIngredientName(String inputName, ArrayList ingredients) throws NullPointerException { if (inputName == null) { throw new NullPointerException(); From 61f260187a9ee4071cb5085a21acb33961d5ca9e Mon Sep 17 00:00:00 2001 From: ShaniceTang Date: Wed, 8 Nov 2023 14:40:46 +0800 Subject: [PATCH 11/11] Improve coding standard --- .../diagrams/sequence/ListIngredientCommand_execute.puml | 2 +- .../seedu/cafectrl/command/BuyIngredientCommand.java | 3 ++- src/main/java/seedu/cafectrl/parser/Parser.java | 9 +++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/docs/diagrams/sequence/ListIngredientCommand_execute.puml b/docs/diagrams/sequence/ListIngredientCommand_execute.puml index 62d0919c2c..da32337fb0 100644 --- a/docs/diagrams/sequence/ListIngredientCommand_execute.puml +++ b/docs/diagrams/sequence/ListIngredientCommand_execute.puml @@ -1,4 +1,4 @@ - @startuml +@startuml !define COMMAND class ListIngredientCommand !define UI class Ui !define MENU class Menu diff --git a/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java b/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java index 604ca5f7d8..c252619a18 100644 --- a/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java +++ b/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java @@ -25,6 +25,7 @@ public class BuyIngredientCommand extends Command { private ArrayList ingredients; private ArrayList ingredientsToBePrinted = new ArrayList<>(); private String ingredientString = ""; // Used to store the message about the bought ingredients + private int FIRST_INDEX = 0; /** * Constructs a BuyIngredientCommand with the specified ingredients, user interface, and pantry. @@ -66,7 +67,7 @@ private void addIngredient() { ingredients.set(i, ingredient); } - for (int i = ingredients.size() - 1; i > -1; i--) { + for (int i = ingredients.size() - ui.OFFSET_LIST_INDEX; i >= FIRST_INDEX; i--) { Ingredient ingredient = ingredients.get(i); buildBuyIngredientMessage(ingredient); } diff --git a/src/main/java/seedu/cafectrl/parser/Parser.java b/src/main/java/seedu/cafectrl/parser/Parser.java index f21c991fd0..2ec26fb6c0 100644 --- a/src/main/java/seedu/cafectrl/parser/Parser.java +++ b/src/main/java/seedu/cafectrl/parser/Parser.java @@ -71,6 +71,11 @@ public class Parser implements ParserUtil { 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 int MIN_QTY = 1; + private static final int MAX_QTY = 1000000; + private static final String GRAMS_UNIT = "g"; + private static final String ML_UNIT = "ml"; + private static final String SHOW_SALE_BY_DAY_ARGUMENT_STRING = "day/(\\d+)"; @@ -500,7 +505,7 @@ private static Command prepareBuyIngredient(String arguments, Ui ui, Pantry pant } private static boolean isValidUnit(String ingredientUnit) { - return ingredientUnit.equals("g") || ingredientUnit.equals("ml"); + return ingredientUnit.equals(GRAMS_UNIT) || ingredientUnit.equals(ML_UNIT); } private static boolean isEmptyUnit(String ingredientUnit) { @@ -508,7 +513,7 @@ private static boolean isEmptyUnit(String ingredientUnit) { } private static boolean isInvalidQty(int ingredientQty) { - return ingredientQty < 1 || ingredientQty > 1000000; + return ingredientQty < MIN_QTY || ingredientQty > MAX_QTY; } //@@author ziyi105