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

Refactor calculateMaxDish function in 'Pantry' class. #300

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading