Skip to content

Commit

Permalink
Add logging to add dish related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DextheChik3n committed Nov 12, 2023
1 parent 7ec7c57 commit 0dc39bb
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/seedu/cafectrl/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,10 @@ private static Command prepareAdd(String arguments, Menu menu, Ui ui) {

private static Matcher detectErrorInPreAddParse(String arguments) throws ParserException {
if (isRepeatedArgument(arguments, ADD_DISH_NAME_ARGUMENT)) {
logger.log(Level.WARNING, "Repeated dish/ argument!");
throw new ParserException(ErrorMessages.REPEATED_NAME_ARGUMENT);
} else if (isRepeatedArgument(arguments, ADD_DISH_PRICE_ARGUMENT)) {
logger.log(Level.WARNING, "Repeated price/ argument!");
throw new ParserException(ErrorMessages.REPEATED_PRICE_ARGUMENT);
}

Expand All @@ -290,6 +292,7 @@ private static Matcher detectErrorInPreAddParse(String arguments) throws ParserE

private static void detectErrorPostDishNameParse(String dishName, Menu menu) throws ParserException {
if (dishName.isEmpty()) {
logger.warning("Dish name empty!");
throw new ParserException(ErrorMessages.MISSING_DISH_NAME);
} else if (isNameLengthInvalid(dishName)) {
logger.warning("Invalid name length!");
Expand All @@ -298,6 +301,7 @@ private static void detectErrorPostDishNameParse(String dishName, Menu menu) thr
logger.warning("Repeated dish!");
throw new ParserException(ErrorMessages.REPEATED_DISH_MESSAGE);
} else if (containsSpecialChar(dishName)) {
logger.warning("Special character in dish name!");
throw new ParserException(ErrorMessages.NAME_CANNOT_CONTAIN_SPECIAL_CHAR);
}
}
Expand Down Expand Up @@ -363,15 +367,18 @@ private static void parseIngredient(

private static Matcher detectErrorPreIngredientParse(String inputIngredient) throws ParserException {
if (isRepeatedArgument(inputIngredient, INGREDIENT_ARGUMENT)) {
logger.log(Level.WARNING, "Repeated ingredient/ argument!");
throw new ParserException(ErrorMessages.REPEATED_INGREDIENT_ARGUMENT);
} else if (isRepeatedArgument(inputIngredient, QTY_ARGUMENT)) {
logger.log(Level.WARNING, "Repeated qty/ argument!");
throw new ParserException(ErrorMessages.REPEATED_QTY_ARGUMENT);
}

final Pattern ingredientPattern = Pattern.compile(INGREDIENT_ARGUMENT_FORMAT_REGEX);
Matcher ingredientMatcher = ingredientPattern.matcher(inputIngredient);

if (!ingredientMatcher.matches()) {
logger.log(Level.WARNING, "Mismatched ingredient arguments!");
throw new ParserException(ErrorMessages.INVALID_INGREDIENT_ARGUMENTS);
}

Expand All @@ -384,22 +391,29 @@ private static void detectErrorPostIngredientParse(

//error case
if (ingredientName.isEmpty()) {
logger.log(Level.WARNING, "Missing ingredient name!");
throw new ParserException(ErrorMessages.MISSING_INGREDIENT_NAME);
} else if (isNameLengthInvalid(ingredientName)) {
logger.log(Level.WARNING, "Exceed max ingredient name length!");
throw new ParserException(ErrorMessages.INVALID_INGREDIENT_NAME_LENGTH_MESSAGE);
} else if (isInvalidQty(ingredientQty)) {
logger.log(Level.WARNING, "Exceed ingredient qty range!");
throw new ParserException(ErrorMessages.INVALID_INGREDIENT_QTY);
} else if (isEmptyUnit(ingredientUnit)) {
logger.log(Level.WARNING, "Missing ingredient qty unit!");
throw new ParserException(ErrorMessages.EMPTY_UNIT_MESSAGE);
} else if (!isValidUnit(ingredientUnit)) {
logger.log(Level.WARNING, "Invalid ingredient qty unit!");
throw new ParserException(ErrorMessages.INVALID_UNIT_MESSAGE);
} else if (containsSpecialChar(ingredientName)) {
logger.log(Level.WARNING, "Special character in ingredient name!");
throw new ParserException(ErrorMessages.NAME_CANNOT_CONTAIN_SPECIAL_CHAR);
}

//unusual case
//user input repeated ingredient name for add dish command
if (isExcludeRepeatedIngredients && isRepeatedIngredientName(ingredientName, ingredients)) {
logger.log(Level.WARNING, "Repeated ingredient name for AddDishCommand!");
throw new ParserException(ErrorMessages.REPEATED_INGREDIENT_NAME);
}
}
Expand All @@ -418,15 +432,18 @@ public static float parsePriceToFloat(String priceText) throws ParserException {

// Check whether price text is empty
if (priceText.isEmpty()) {
logger.log(Level.WARNING, "Missing dish price!");
throw new ParserException(ErrorMessages.MISSING_PRICE);
} else if (!priceMatcher.matches()) {
logger.log(Level.WARNING, "Exceed price valid range!");
throw new ParserException(ErrorMessages.WRONG_PRICE_TYPE_FOR_EDIT_PRICE);
}

float price;
try {
price = Float.parseFloat(trimmedPriceText);
} catch (NumberFormatException e) {
logger.log(Level.WARNING, e.getMessage(), e);
throw new ParserException(ErrorMessages.WRONG_PRICE_TYPE_FOR_EDIT_PRICE);
}

Expand Down

0 comments on commit 0dc39bb

Please sign in to comment.