Skip to content

Commit

Permalink
Merge branch 'master' into 148_encoding_decoding_orders_v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
NaychiMin committed Oct 31, 2023
2 parents 3137e0b + b76a27a commit 2b98bd6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/main/java/seedu/cafectrl/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class Parser {
//@@author ziyi105
private static final String COMMAND_ARGUMENT_REGEX = "(?<commandWord>[a-z_]+)\\s*(?<arguments>.*)";

//@@author @DextheChik3n
//@@author DextheChik3n
/** Add Dish Command Handler Patterns*/
private static final String ADD_ARGUMENT_STRING = "name/(?<dishName>[A-Za-z0-9\\s]+) "
+ "price/\\s*(?<dishPrice>[0-9]*\\.[0-9]{0,2}|[0-9]+)\\s+"
Expand Down Expand Up @@ -187,7 +187,7 @@ private static Command prepareEditPriceCommand(Menu menu, String arguments, Ui u
//@@author DextheChik3n
/**
* Parses the user input text into ingredients to form a <code>Dish</code> that is added to the <code>Menu</code>
* @param arguments
* @param arguments string that matches group arguments
* @return new AddDishCommand
*/
private static Command prepareAdd(String arguments, Menu menu, Ui ui) {
Expand All @@ -204,10 +204,13 @@ private static Command prepareAdd(String arguments, Menu menu, Ui ui) {
// To retrieve specific arguments from arguments
String dishName = matcher.group(DISH_NAME_MATCHER_GROUP_LABEL).trim();
float price = parsePriceToFloat(matcher.group(PRICE_MATCHER_GROUP_LABEL));
System.out.println(Float.MAX_VALUE);
String ingredientsListString = matcher.group(INGREDIENTS_MATCHER_GROUP_LABEL);

ArrayList<Ingredient> ingredients = ingredientParsing(ingredientsListString);
if (isRepeatedDishName(dishName, menu)) {
return new IncorrectCommand(Messages.REPEATED_DISH_MESSAGE, ui);
}

ArrayList<Ingredient> ingredients = parseIngredients(ingredientsListString);

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

Expand All @@ -217,6 +220,8 @@ private static Command prepareAdd(String arguments, Menu menu, Ui ui) {
+ AddDishCommand.MESSAGE_USAGE, ui);
} catch (ArithmeticException e) {
return new IncorrectCommand(ErrorMessages.INVALID_PRICE_MESSAGE, ui);
} catch (NullPointerException e) {
return new IncorrectCommand(ErrorMessages.NULL_DISH_NAME_MESSAGE, ui);
}
}

Expand All @@ -226,7 +231,7 @@ private static Command prepareAdd(String arguments, Menu menu, Ui ui) {
* @return Ingredient objects that consists of the dish
* @throws IllegalArgumentException if the input string of ingredients is in an incorrect format.
*/
private static ArrayList<Ingredient> ingredientParsing(String ingredientsListString)
private static ArrayList<Ingredient> parseIngredients(String ingredientsListString)
throws IllegalArgumentException {
String[] inputIngredientList = {ingredientsListString};
ArrayList<Ingredient> ingredients = new ArrayList<>();
Expand Down Expand Up @@ -277,6 +282,29 @@ public static float parsePriceToFloat(String priceText) throws ArithmeticExcepti
return price;
}

/**
* Checks in the menu if the dish name already exists in the menu.
* @param inputDishName dish name entered by the user
* @param menu contains all the existing Dishes
* @return boolean of whether a repeated dish name is detected
*/
public static boolean isRepeatedDishName(String inputDishName, Menu menu) throws NullPointerException {
if (inputDishName == null) {
throw new NullPointerException();
}

for (Dish dish: menu.getMenuItemsList()) {
String menuDishNameLowerCase = dish.getName().toLowerCase();
String inputDishNameLowerCase = inputDishName.toLowerCase();

if (menuDishNameLowerCase.equals(inputDishNameLowerCase)) {
return true;
}
}

return false;
}

//@@author NaychiMin
/**
* Parses arguments in the context of the ListIngredient command.
Expand Down Expand Up @@ -341,7 +369,7 @@ private static Command prepareBuyIngredient(String arguments, Ui ui, Pantry pant
}

String ingredientsListString = matcher.group(0);
ArrayList<Ingredient> ingredients = ingredientParsing(ingredientsListString);
ArrayList<Ingredient> ingredients = parseIngredients(ingredientsListString);

try {
return new BuyIngredientCommand(ingredients, ui, pantry);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/cafectrl/ui/ErrorMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class ErrorMessages {
/** Error messages */
public static final String INVALID_ADD_DISH_FORMAT_MESSAGE = "Error: Incorrect format for the add command.\n";
public static final String NULL_DISH_NAME_MESSAGE = "Error: Null dish name detected";
public static final String INVALID_PRICE_MESSAGE = "Error: Price value entered is too big!";
public static final String MISSING_ARGUMENT_FOR_EDIT_PRICE = "Error: Missing arguments "
+ "for edit price command.";
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/cafectrl/ui/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Messages {

/** Messages for add dish command */
public static final String ADD_DISH_MESSAGE = "You have added the following dish...";
public static final String REPEATED_DISH_MESSAGE = "Sorry, this dish name already exists.";

/** Messages for view stock command */
public static final String VIEW_STOCK = "You have the following ingredients in pantry:"
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/seedu/cafectrl/parser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,5 +432,47 @@ void parsePriceToFloat_largePriceString_arithmeticExceptionThrown() throws Arith

assertThrows(ArithmeticException.class, () -> Parser.parsePriceToFloat(inputPriceString));
}

@Test
void isRepeatedDishName_existingDishName_true() {
Menu menu = new Menu();
Dish dish = new Dish("Chicken Rice", 2.50F);
menu.addDish(dish);

String inputDishName = "chicken rice";

assertTrue(Parser.isRepeatedDishName(inputDishName, menu));
}

@Test
void isRepeatedDishName_nonExistingDishName_false() {
Menu menu = new Menu();
Dish dish = new Dish("Chicken Rice", 2.50F);
menu.addDish(dish);

String inputDishName = "chicken chop";

assertFalse(Parser.isRepeatedDishName(inputDishName, menu));
}

@Test
void isRepeatedDishName_nullString_nullPointerExceptionThrown() throws NullPointerException {
Menu menu = new Menu();
Dish dish = new Dish("Chicken Rice", 2.50F);
menu.addDish(dish);

assertThrows(NullPointerException.class, () -> Parser.isRepeatedDishName(null, menu));
}

@Test
void isRepeatedDishName_emptyDishName_false() {
Menu menu = new Menu();
Dish dish = new Dish("Chicken Rice", 2.50F);
menu.addDish(dish);

String inputDishName = "";

assertFalse(Parser.isRepeatedDishName(inputDishName, menu));
}
//@@author
}

0 comments on commit 2b98bd6

Please sign in to comment.