Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task 314, Mismatching unit from Menu #319

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void execute() {
addIngredient();
ui.printBuyIngredientHeader();
ui.showToUser(ingredientString.strip());
} catch (RuntimeException e) {
} catch (Exception e) {
logger.log(Level.WARNING, "BuyIngredientCommand unsuccessful: " + e.getMessage(), e);
ui.showToUser(e.getMessage());
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/seedu/cafectrl/data/Pantry.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ private Ingredient addIngredientQuantity(int qty, int ingredientIndex, String un
Ingredient ingredient = pantryStock.get(ingredientIndex);
if (!unit.equalsIgnoreCase(ingredient.getUnit())) {
logger.warning("Unit does not match previous unit");
throw new RuntimeException(ErrorMessages.UNIT_NOT_MATCHING
+ "\nUnit used previously: " + ingredient.getUnit());
throw new RuntimeException(ingredient.getName()
+ ErrorMessages.UNIT_NOT_MATCHING
+ ingredient.getUnit()
+ ErrorMessages.IGNORE_REMAINING_INGREDIENTS);
}
qty += ingredient.getQty(); //adds new qty to current qty
ingredient.setQty(qty);
Expand Down
53 changes: 44 additions & 9 deletions src/main/java/seedu/cafectrl/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public Command parseCommand(Menu menu, String userInput, Ui ui,
return prepareViewTotalStock(ui, pantry);

case BuyIngredientCommand.COMMAND_WORD:
return prepareBuyIngredient(arguments, ui, pantry);
return prepareBuyIngredient(arguments, ui, pantry, menu);

case HelpCommand.COMMAND_WORD:
return prepareHelpCommand(ui);
Expand Down Expand Up @@ -251,7 +251,7 @@ private static Command prepareAdd(String arguments, Menu menu, Ui ui) {
throw new ParserException(ErrorMessages.NAME_CANNOT_CONTAIN_SPECIAL_CHAR);
}

ArrayList<Ingredient> ingredients = parseIngredients(ingredientsListString, true);
ArrayList<Ingredient> ingredients = parseIngredients(ingredientsListString, true, menu);

Dish dish = new Dish(dishName, ingredients, price);

Expand All @@ -267,13 +267,15 @@ private static Command prepareAdd(String arguments, Menu menu, Ui ui) {

/**
* Parses the user's input text ingredients.
*
* @param ingredientsListString user's input string of ingredients, multiple ingredients seperated by ',' is allowed
* @param menu
* @return list of ingredients that consists of the dish
* @throws IllegalArgumentException if the input string of ingredients is in an incorrect format.
* @throws ParserException if the input string does not match the constraints
* @throws ParserException if the input string does not match the constraints
*/
private static ArrayList<Ingredient> parseIngredients(String ingredientsListString,
boolean excludeRepeatedIngredients) throws IllegalArgumentException, ParserException {
boolean excludeRepeatedIngredients, Menu menu) throws IllegalArgumentException, ParserException {
logger.info("Parsing ingredients...");
String[] inputIngredientList = {ingredientsListString};
ArrayList<Ingredient> ingredients = new ArrayList<>();
Expand Down Expand Up @@ -313,6 +315,7 @@ private static ArrayList<Ingredient> parseIngredients(String ingredientsListStri
}

Ingredient ingredient = new Ingredient(ingredientName, ingredientQty, ingredientUnit);
checkForMismatchUnit(menu, ingredient);
ingredients.add(ingredient);
}

Expand Down Expand Up @@ -489,23 +492,21 @@ private static Command prepareViewTotalStock(Ui ui, Pantry pantry) {
return new ViewTotalStockCommand(pantry, ui);
}

private static Command prepareBuyIngredient(String arguments, Ui ui, Pantry pantry) {
private static Command prepareBuyIngredient(String arguments, Ui ui, Pantry pantry, Menu menu) {
Pattern buyIngredientArgumentsPattern = Pattern.compile(BUY_INGREDIENT_ARGUMENT_STRING);
Matcher matcher = buyIngredientArgumentsPattern.matcher(arguments.trim());

if (!matcher.matches()) {
logger.warning("Unmatching regex!");
return new IncorrectCommand(ErrorMessages.MISSING_ARGUMENT_FOR_BUY_INGREDIENT
return new IncorrectCommand(ErrorMessages.INVALID_ARGUMENT_FOR_BUY_INGREDIENT
+ BuyIngredientCommand.MESSAGE_USAGE, ui);
}

String ingredientsListString = matcher.group(0);

try {
ArrayList<Ingredient> ingredients = parseIngredients(ingredientsListString, false);
ArrayList<Ingredient> ingredients = parseIngredients(ingredientsListString, false, menu);
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);
}
Expand All @@ -523,6 +524,40 @@ public static boolean isInvalidQty(int ingredientQty) {
return ingredientQty < MIN_QTY || ingredientQty > MAX_QTY;
}

public static void checkForMismatchUnit(Menu menu, Ingredient newIngredient) throws ParserException {
logger.info("Checking for mismatched units...");
ArrayList<Dish> dishArrayList = menu.getMenuItemsList();
for (Dish dish : dishArrayList) {
traverseIngredientsOfDish(newIngredient, dish);
}
}

private static void traverseIngredientsOfDish(Ingredient newIngredient, Dish dish) throws ParserException {
ArrayList<Ingredient> ingredientArrayList = dish.getIngredients();
for (Ingredient currentIngredient : ingredientArrayList) {
logger.info("Comparing name: " + newIngredient.getName() + " and " + currentIngredient.getName());
compareIngredientName(newIngredient, currentIngredient);
}
}

private static void compareIngredientName(Ingredient newIngredient,
Ingredient currentIngredient) throws ParserException {
if (currentIngredient.getName().equalsIgnoreCase(newIngredient.getName())) {
logger.info("Comparing units: " + newIngredient.getUnit() + " and " + currentIngredient.getUnit());
compareUnits(newIngredient, currentIngredient);
}
}

private static void compareUnits(Ingredient newIngredient, Ingredient currentIngredient) throws ParserException {
if (!currentIngredient.getUnit().equalsIgnoreCase(newIngredient.getUnit())) {
logger.warning("Units not matching!");
throw new ParserException(newIngredient.getName()
+ ErrorMessages.UNIT_NOT_MATCHING
+ currentIngredient.getUnit()
+ ErrorMessages.RETYPE_COMMAND_MESSAGE);
}
}

//@@author ziyi105
/**
* Check whether a text contains special character
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/seedu/cafectrl/ui/ErrorMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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 MISSING_ARGUMENT_FOR_BUY_INGREDIENT = "Error: Missing arguments "
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 dish index! Could you make sure that is it of type int \n"
Expand All @@ -34,8 +34,6 @@ public class ErrorMessages {
+ "Type 'help' to view the accepted list of commands";
public static final String INVALID_DISH_INDEX = "Do we even have this dish? "
+ "Double check the index of the dish you wanna modify!";
public static final String INVALID_ARGUMENT_FOR_BUY_INGREDIENT = "Error: Invalid arguments "
+ "for buy ingredient command.";
public static final String INVALID_ADD_ORDER_FORMAT_MESSAGE = "Error: Incorrect format "
+ "for the add order command.";
public static final String DATA_FOLDER_NOT_FOUND_MESSAGE = "Data Folder was not found!\nIt's ok... "
Expand All @@ -44,8 +42,11 @@ public class ErrorMessages {
+ "it hasn't even made it to our menu yet!";
public static final String ERROR_IN_PANTRY_STOCK_DATA = "Error in pantry stock data file! "
+ "Skipping this particular ingredient!";
public static final String UNIT_NOT_MATCHING = "Sorry, you have used a "
+ "different unit for this ingredient!";
public static final String UNIT_NOT_MATCHING = ": Sorry, you have used a "
+ "different unit for this ingredient!"
+ "\nUnit used previously: ";
public static final String IGNORE_REMAINING_INGREDIENTS = "\nRemaining ingredients after this not added";
public static final String RETYPE_COMMAND_MESSAGE = "\nPlease re-enter command with appropriate units.";
public static final String MENU_FILE_NOT_FOUND_MESSAGE = "Menu data was not found!\n"
+ "No worries, new menu has been created";
public static final String PANTRY_FILE_NOT_FOUND_MESSAGE = "Pantry stock data was not found!\n"
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/seedu/cafectrl/data/PantryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ void addIngredientToStock_differentUnitForBuyIngredient_returnErrorMessage() {
pantry.addIngredientToStock("chicken", 500, "ml");
});

String expectedErrorMessage = ErrorMessages.UNIT_NOT_MATCHING + "\nUnit used previously: g";
String expectedErrorMessage = "chicken" + ErrorMessages.UNIT_NOT_MATCHING
+ "g" + ErrorMessages.IGNORE_REMAINING_INGREDIENTS;
assertEquals(expectedErrorMessage, exception.getMessage());
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/seedu/cafectrl/parser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ void parseCommand_missingArgsForBuyIngredient_returnErrorMessage() {

IncorrectCommand incorrectCommand = (IncorrectCommand) result;
String feedbackToUser = incorrectCommand.feedbackToUser;
assertEquals(ErrorMessages.MISSING_ARGUMENT_FOR_BUY_INGREDIENT
assertEquals(ErrorMessages.INVALID_ARGUMENT_FOR_BUY_INGREDIENT
+ BuyIngredientCommand.MESSAGE_USAGE, feedbackToUser);
}

Expand Down
Loading