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

Fix minor bugs for several features #391

Merged
merged 10 commits into from
Nov 13, 2023
7 changes: 5 additions & 2 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,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);
Expand All @@ -90,7 +90,7 @@ private void buildBuyIngredientMessage(Ingredient ingredient) {
}
ingredientsToBePrinted.add(ingredient);
ingredientString += "Ingredient: " + ingredient.getName()
+ "\nQty: " + ingredient.getQty()
+ "\nTotal Qty: " + ingredient.getQty()
+ ingredient.getUnit() + "\n\n";
}
}
Expand Down
25 changes: 15 additions & 10 deletions src/main/java/seedu/cafectrl/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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/.+)*)";
ShaniceTang marked this conversation as resolved.
Show resolved Hide resolved
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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -652,15 +656,16 @@ 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);

try {
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 Down
11 changes: 7 additions & 4 deletions src/main/java/seedu/cafectrl/storage/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ private static void decodeDishString(String dishString, ArrayList<Dish> 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<Ingredient> 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) {
Expand All @@ -92,13 +95,13 @@ private static ArrayList<Ingredient> decodeIngredientData(String[] ingredientsSt
ArrayList<Ingredient> 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);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/seedu/cafectrl/ui/ErrorMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!";
Expand Down Expand Up @@ -111,4 +109,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!";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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+", " "));
Expand All @@ -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+", " "));
Expand Down
26 changes: 21 additions & 5 deletions src/test/java/seedu/cafectrl/parser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading