Skip to content

Commit

Permalink
Refactor calculateMaxDish to improve code quality.
Browse files Browse the repository at this point in the history
  • Loading branch information
NaychiMin committed Nov 9, 2023
1 parent 5528731 commit f459461
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions src/main/java/seedu/cafectrl/data/Pantry.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,43 @@ public int calculateMaxDishes(Dish dish, Menu menu, Order order) {
int maxNumofDish = Integer.MAX_VALUE;
ArrayList<Ingredient> 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;
}

/**
Expand Down

0 comments on commit f459461

Please sign in to comment.