diff --git a/src/main/java/seedu/cafectrl/data/Pantry.java b/src/main/java/seedu/cafectrl/data/Pantry.java index a1a1aa7664..80a18ce8ef 100644 --- a/src/main/java/seedu/cafectrl/data/Pantry.java +++ b/src/main/java/seedu/cafectrl/data/Pantry.java @@ -155,26 +155,43 @@ public int calculateMaxDishes(Dish dish, Menu menu, Order order) { int maxNumofDish = Integer.MAX_VALUE; ArrayList dishIngredients = retrieveIngredientsForDish(dish.getName(), menu); boolean isRestockHeaderDisplayed = false; - int dishQty = order.getQuantity(); for (Ingredient dishIngredient : dishIngredients) { int numOfDish = calculateMaxDishForEachIngredient(dishIngredient); maxNumofDish = Math.min(numOfDish, maxNumofDish); - if (!isRestockHeaderDisplayed && (numOfDish < dishQty)) { - ui.showToUser(Messages.RESTOCK_CORNER, Messages.RESTOCK_TITLE, Messages.RESTOCK_CORNER); - isRestockHeaderDisplayed = true; + if (!order.getIsComplete()) { + isRestockHeaderDisplayed = showRestockHeaderIfNeeded(isRestockHeaderDisplayed); + handleIncompleteDishCase(dishIngredient, order); + } else { + if (numOfDish == 0) { + isRestockHeaderDisplayed = showRestockHeaderIfNeeded(isRestockHeaderDisplayed); + } + handleZeroDishCase(dishIngredient); } + } - if (numOfDish < dishQty && !order.getIsComplete()) { - handleRestock(dishIngredient, dishQty); - } + return maxNumofDish; + } - if (numOfDish == 0) { - handleRestock(dishIngredient, 1); - } + private boolean showRestockHeaderIfNeeded(boolean isRestockHeaderDisplayed) { + if (!isRestockHeaderDisplayed) { + ui.showToUser(Messages.RESTOCK_CORNER, Messages.RESTOCK_TITLE, Messages.RESTOCK_CORNER); + isRestockHeaderDisplayed = true; + } + return isRestockHeaderDisplayed; + } + + private void handleIncompleteDishCase(Ingredient dishIngredient, Order order) { + int orderQuantity = order.getQuantity(); + if (calculateMaxDishForEachIngredient(dishIngredient) < orderQuantity) { + handleRestock(dishIngredient, orderQuantity); + } + } + private void handleZeroDishCase(Ingredient dishIngredient) { + if (calculateMaxDishForEachIngredient(dishIngredient) == 0) { + handleRestock(dishIngredient, 1); } - return maxNumofDish; } /**