diff --git a/src/main/java/seedu/cafectrl/command/ListSaleByDayCommand.java b/src/main/java/seedu/cafectrl/command/ListSaleByDayCommand.java index dbe5f2f864..b9e5fbe8e0 100644 --- a/src/main/java/seedu/cafectrl/command/ListSaleByDayCommand.java +++ b/src/main/java/seedu/cafectrl/command/ListSaleByDayCommand.java @@ -31,6 +31,9 @@ public void execute() { } catch (Exception e) { ui.showToUser(ErrorMessages.INVALID_SALE_DAY); } + } + public int getDay() { + return day; } } diff --git a/src/main/java/seedu/cafectrl/command/ListTotalSales.java b/src/main/java/seedu/cafectrl/command/ListTotalSalesCommand.java similarity index 78% rename from src/main/java/seedu/cafectrl/command/ListTotalSales.java rename to src/main/java/seedu/cafectrl/command/ListTotalSalesCommand.java index 45b20f53e7..8b69b5e675 100644 --- a/src/main/java/seedu/cafectrl/command/ListTotalSales.java +++ b/src/main/java/seedu/cafectrl/command/ListTotalSalesCommand.java @@ -4,14 +4,14 @@ import seedu.cafectrl.data.Sales; import seedu.cafectrl.ui.Ui; -public class ListTotalSales extends Command { +public class ListTotalSalesCommand extends Command { public static final String COMMAND_WORD = "list_total_sales"; public static final String MESSAGE_USAGE = "To show sales for all days:\n" + COMMAND_WORD; private Sales sales; private Ui ui; private Menu menu; - public ListTotalSales(Sales sales, Ui ui, Menu menu) { + public ListTotalSalesCommand(Sales sales, Ui ui, Menu menu) { this.sales = sales; this.ui = ui; this.menu = menu; diff --git a/src/main/java/seedu/cafectrl/data/Pantry.java b/src/main/java/seedu/cafectrl/data/Pantry.java index 80a18ce8ef..a531c7364a 100644 --- a/src/main/java/seedu/cafectrl/data/Pantry.java +++ b/src/main/java/seedu/cafectrl/data/Pantry.java @@ -155,7 +155,6 @@ public int calculateMaxDishes(Dish dish, Menu menu, Order order) { int maxNumofDish = Integer.MAX_VALUE; ArrayList dishIngredients = retrieveIngredientsForDish(dish.getName(), menu); boolean isRestockHeaderDisplayed = false; - for (Ingredient dishIngredient : dishIngredients) { int numOfDish = calculateMaxDishForEachIngredient(dishIngredient); maxNumofDish = Math.min(numOfDish, maxNumofDish); diff --git a/src/main/java/seedu/cafectrl/data/Sales.java b/src/main/java/seedu/cafectrl/data/Sales.java index df2129b093..e88b056079 100644 --- a/src/main/java/seedu/cafectrl/data/Sales.java +++ b/src/main/java/seedu/cafectrl/data/Sales.java @@ -59,7 +59,7 @@ public OrderList getOrderList(int index) { * @param menu The Menu object representing the cafe's menu. */ public void printSales(Ui ui, Menu menu) { - if(orderLists.isEmpty()) { + if(isOrderListsEmpty()) { ui.showToUser("No sales made."); return; } @@ -67,10 +67,8 @@ public void printSales(Ui ui, Menu menu) { for (int day = 0; day < orderLists.size(); day++) { OrderList orderList = orderLists.get(day); - ui.showToUser("Day " + (day + 1) + ":"); - if (orderList.isEmpty() || !orderList.hasCompletedOrders()) { - ui.showToUser("No sales for this day."); + ui.showToUser("No sales for day " + (day + DAY_DISPLAY_OFFSET) + "."); continue; } @@ -103,5 +101,14 @@ public void printSaleByDay(Ui ui, Menu menu, int day) { ui.showToUser(ErrorMessages.INVALID_SALE_DAY); } } + + public boolean isOrderListsEmpty() { + for (OrderList orderList : orderLists) { + if (!orderList.isEmpty()) { + return false; + } + } + return true; + } //@@author } diff --git a/src/main/java/seedu/cafectrl/parser/Parser.java b/src/main/java/seedu/cafectrl/parser/Parser.java index da1b10a0bc..61f82da989 100644 --- a/src/main/java/seedu/cafectrl/parser/Parser.java +++ b/src/main/java/seedu/cafectrl/parser/Parser.java @@ -14,7 +14,7 @@ import seedu.cafectrl.command.ListMenuCommand; import seedu.cafectrl.command.NextDayCommand; import seedu.cafectrl.command.PreviousDayCommand; -import seedu.cafectrl.command.ListTotalSales; +import seedu.cafectrl.command.ListTotalSalesCommand; import seedu.cafectrl.command.ListSaleByDayCommand; import seedu.cafectrl.command.ViewTotalStockCommand; @@ -137,7 +137,7 @@ public Command parseCommand(Menu menu, String userInput, Ui ui, case PreviousDayCommand.COMMAND_WORD: return preparePreviousDay(ui, currentDate); - case ListTotalSales.COMMAND_WORD: + case ListTotalSalesCommand.COMMAND_WORD: return prepareShowSales(sales, menu, ui); case ListSaleByDayCommand.COMMAND_WORD: @@ -587,7 +587,7 @@ private static Command prepareNextDay(Ui ui, Sales sales, CurrentDate currentDat * @return A ShowSalesCommand instance for viewing all sales items. */ private static Command prepareShowSales(Sales sale, Menu menu, Ui ui) { - return new ListTotalSales(sale, ui, menu); + return new ListTotalSalesCommand(sale, ui, menu); } /** diff --git a/src/test/java/seedu/cafectrl/command/ListSaleByDayCommandTest.java b/src/test/java/seedu/cafectrl/command/ListSaleByDayCommandTest.java new file mode 100644 index 0000000000..b379ab351a --- /dev/null +++ b/src/test/java/seedu/cafectrl/command/ListSaleByDayCommandTest.java @@ -0,0 +1,200 @@ +package seedu.cafectrl.command; + +import org.junit.jupiter.api.Test; +import seedu.cafectrl.data.Menu; +import seedu.cafectrl.data.Order; +import seedu.cafectrl.data.OrderList; +import seedu.cafectrl.data.Sales; +import seedu.cafectrl.data.dish.Dish; +import seedu.cafectrl.data.dish.Ingredient; +import seedu.cafectrl.ui.ErrorMessages; +import seedu.cafectrl.ui.Messages; +import seedu.cafectrl.ui.Ui; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ListSaleByDayCommandTest { + + @Test + public void execute_validDayIndex_listSaleOfDay() { + // Create a dummy menu + ArrayList ingredients = new ArrayList<>( + Arrays.asList(new Ingredient("Lettuce", 100, "g"), + new Ingredient("Chicken", 50, "g"))); + + Dish dishChickenRice = new Dish("Chicken Rice", ingredients, 2.50f); + Dish dishChickenChop = new Dish("Chicken Chop", ingredients, 5.00f); + Menu menu = new Menu(); + menu.addDish(dishChickenRice); + menu.addDish(dishChickenChop); + + // Create a dummy order for day 1 + Order order1 = new Order(dishChickenRice, 2); + order1.setComplete(true); + Order order2 = new Order(dishChickenChop, 1); + order2.setComplete(true); + + // Create a dummy order list for day 1 + OrderList orderList1 = new OrderList(); + orderList1.addOrder(order1); + orderList1.addOrder(order2); + + // Create a dummy order for day 2 + Order order3 = new Order(dishChickenRice, 4); + order3.setComplete(false); + Order order4 = new Order(dishChickenChop, 1); + order4.setComplete(true); + Order order5 = new Order(dishChickenChop, 1); + order5.setComplete(true); + + // Create a dummy order list for day 2 + OrderList orderList2 = new OrderList(); + orderList2.addOrder(order3); + + ArrayList orderLists= new ArrayList<>(Arrays.asList(orderList1, orderList2)); + // Create a sales object and add the order lists + Sales sales = new Sales(orderLists); + + Ui ui = new Ui(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream consoleStream = new PrintStream(baos); + + PrintStream originalOut = System.out; + System.setOut(consoleStream); + + int dayToListSale = 1; + ListSaleByDayCommand listSaleByDayCommand = + new ListSaleByDayCommand(dayToListSale, ui, sales, menu); + listSaleByDayCommand.execute(); + + String actualOutput = baos.toString().trim(); + System.setOut(originalOut); + + String expectedOutput = Messages.SHOW_SALES_END_CAP + + Messages.SHOW_SALES_DAY_PART_1 + + dayToListSale + + Messages.SHOW_SALES_DAY_PART_2 + + Messages.SHOW_SALES_CORNER + + Messages.SHOW_SALES_TITLE + + Messages.SHOW_SALES_CORNER + + "| chicken rice | 2 | 5.00 |" + + "| chicken chop | 1 | 5.00 |" + + Messages.SHOW_SALES_END_CAP + + "| Total for day: | $10.00 |" + + Messages.SHOW_SALES_END_CAP; + + String normalizedExpected = expectedOutput.toLowerCase().replaceAll("\\s+", "").trim(); + String normalizedActual = actualOutput.toLowerCase().replaceAll("\\s+", "").trim(); + + assertEquals(normalizedExpected, normalizedActual); + } + + @Test + public void execute_invalidDayIndex_errorMessage() { + // Create a dummy menu + ArrayList ingredients = new ArrayList<>(Arrays.asList(new Ingredient("Lettuce", 100, "g"), + new Ingredient("Chicken", 50, "g"))); + + Dish dishChickenRice = new Dish("Chicken Rice", ingredients, 2.50f); + Dish dishChickenChop = new Dish("Chicken Chop", ingredients, 5.00f); + Menu menu = new Menu(); + menu.addDish(dishChickenRice); + menu.addDish(dishChickenChop); + + // Create a dummy order for day 1 + Order order1 = new Order(dishChickenRice, 2); + order1.setComplete(true); + Order order2 = new Order(dishChickenChop, 1); + order2.setComplete(true); + + // Create a dummy order list for day 1 + OrderList orderList1 = new OrderList(); + orderList1.addOrder(order1); + orderList1.addOrder(order2); + + ArrayList orderLists= new ArrayList<>(Arrays.asList(orderList1)); + // Create a sales object and add the order lists + Sales sales = new Sales(orderLists); + + Ui ui = new Ui(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream consoleStream = new PrintStream(baos); + + PrintStream originalOut = System.out; + System.setOut(consoleStream); + + int dayToListSale = 200; + ListSaleByDayCommand listSaleByDayCommand = + new ListSaleByDayCommand(dayToListSale, ui, sales, menu); + listSaleByDayCommand.execute(); + + String actualOutput = baos.toString().trim(); + System.setOut(originalOut); + + String expectedOutput = ErrorMessages.INVALID_SALE_DAY;; + + String normalizedExpected = expectedOutput.toLowerCase().replaceAll("\\s+", "").trim(); + String normalizedActual = actualOutput.toLowerCase().replaceAll("\\s+", "").trim(); + + assertEquals(normalizedExpected, normalizedActual); + } + + @Test + public void execute_validDayNoSales_showNoSalesMessage() { + // Create a dummy menu + ArrayList ingredients = new ArrayList<>(Arrays.asList(new Ingredient("Lettuce", 100, "g"), + new Ingredient("Chicken", 50, "g"))); + + Dish dishChickenRice = new Dish("Chicken Rice", ingredients, 2.50f); + Dish dishChickenChop = new Dish("Chicken Chop", ingredients, 5.00f); + Menu menu = new Menu(); + menu.addDish(dishChickenRice); + menu.addDish(dishChickenChop); + + // Create a dummy order for day 1 + Order order1 = new Order(dishChickenRice, 2); + order1.setComplete(true); + Order order2 = new Order(dishChickenChop, 1); + order2.setComplete(true); + + // Create a dummy order list for day 1 + OrderList orderList1 = new OrderList(); + orderList1.addOrder(order1); + orderList1.addOrder(order2); + + OrderList orderList2 = new OrderList(); + + OrderList orderList3 = new OrderList(); + orderList1.addOrder(order1); + + ArrayList orderLists= new ArrayList<>(Arrays.asList(orderList1, orderList2, orderList3)); + Sales sales = new Sales(orderLists); + + Ui ui = new Ui(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream consoleStream = new PrintStream(baos); + + PrintStream originalOut = System.out; + System.setOut(consoleStream); + + int dayToListSale = 2; + ListSaleByDayCommand listSaleByDayCommand = + new ListSaleByDayCommand(dayToListSale, ui, sales, menu); + listSaleByDayCommand.execute(); + + String actualOutput = baos.toString().trim(); + System.setOut(originalOut); + + String expectedOutput = "No sales for this day."; + + String normalizedExpected = expectedOutput.toLowerCase().replaceAll("\\s+", "").trim(); + String normalizedActual = actualOutput.toLowerCase().replaceAll("\\s+", "").trim(); + + assertEquals(normalizedExpected, normalizedActual); + } +} diff --git a/src/test/java/seedu/cafectrl/command/ListTotalSalesCommandCommandTest.java b/src/test/java/seedu/cafectrl/command/ListTotalSalesCommandCommandTest.java new file mode 100644 index 0000000000..d794639892 --- /dev/null +++ b/src/test/java/seedu/cafectrl/command/ListTotalSalesCommandCommandTest.java @@ -0,0 +1,144 @@ +package seedu.cafectrl.command; + +import org.junit.jupiter.api.Test; +import seedu.cafectrl.data.Menu; +import seedu.cafectrl.data.Order; +import seedu.cafectrl.data.OrderList; +import seedu.cafectrl.data.Sales; +import seedu.cafectrl.data.dish.Dish; +import seedu.cafectrl.data.dish.Ingredient; +import seedu.cafectrl.ui.Messages; +import seedu.cafectrl.ui.Ui; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ListTotalSalesCommandCommandTest { + + @Test + public void execute_existingSales_listTotalSales() { + // Create a dummy menu + ArrayList ingredients = new ArrayList<>( + Arrays.asList(new Ingredient("Lettuce", 100, "g"), + new Ingredient("Chicken", 50, "g"))); + + Dish dishChickenRice = new Dish("Chicken Rice", ingredients, 2.50f); + Dish dishChickenChop = new Dish("Chicken Chop", ingredients, 5.00f); + Menu menu = new Menu(); + menu.addDish(dishChickenRice); + menu.addDish(dishChickenChop); + + // Create a dummy order for day 1 + Order order1 = new Order(dishChickenRice, 2); + order1.setComplete(true); + Order order2 = new Order(dishChickenChop, 1); + order2.setComplete(true); + + // Create a dummy order list for day 1 + OrderList orderList1 = new OrderList(); + orderList1.addOrder(order1); + orderList1.addOrder(order2); + + // Create a dummy order list for day 2 + OrderList orderList2 = new OrderList(); + + // Create a dummy order for day 3 + Order order3 = new Order(dishChickenRice, 4); + order3.setComplete(false); + Order order4 = new Order(dishChickenChop, 1); + order4.setComplete(true); + Order order5 = new Order(dishChickenChop, 1); + order5.setComplete(true); + + // Create a dummy order list for day 3 + OrderList orderList3 = new OrderList(); + orderList3.addOrder(order3); + orderList3.addOrder(order4); + orderList3.addOrder(order5); + + ArrayList orderLists= new ArrayList<>(Arrays.asList(orderList1, orderList2, orderList3)); + // Create a sales object and add the order lists + Sales sales = new Sales(orderLists); + + + Ui ui = new Ui(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream consoleStream = new PrintStream(baos); + + PrintStream originalOut = System.out; + System.setOut(consoleStream); + + ListTotalSalesCommand listTotalSalesCommand = + new ListTotalSalesCommand(sales, ui, menu); + listTotalSalesCommand.execute(); + + String actualOutput = baos.toString().trim(); + System.setOut(originalOut); + + String expectedOutput = Messages.SHOW_SALES_END_CAP + + Messages.SHOW_SALES_END_CAP + + Messages.SHOW_SALES_DAY_PART_1 + + "1" + + Messages.SHOW_SALES_DAY_PART_2 + + Messages.SHOW_SALES_CORNER + + Messages.SHOW_SALES_TITLE + + Messages.SHOW_SALES_CORNER + + "| chicken rice | 2 | 5.00 |" + + "| chicken chop | 1 | 5.00 |" + + Messages.SHOW_SALES_END_CAP + + "| Total for day: | $10.00 |" + + Messages.SHOW_SALES_END_CAP + + "No sales for day 2." + + Messages.SHOW_SALES_END_CAP + + Messages.SHOW_SALES_DAY_PART_1 + + "3" + + Messages.SHOW_SALES_DAY_PART_2 + + Messages.SHOW_SALES_CORNER + + Messages.SHOW_SALES_TITLE + + Messages.SHOW_SALES_CORNER + + "| chicken chop | 2 | 10.00 |" + + Messages.SHOW_SALES_END_CAP + + "| Total for day: | $10.00 |" + + Messages.SHOW_SALES_END_CAP; + + String normalizedExpected = expectedOutput.toLowerCase().replaceAll("\\s+", "").trim(); + String normalizedActual = actualOutput.toLowerCase().replaceAll("\\s+", "").trim(); + + assertEquals(normalizedExpected, normalizedActual); + } + + @Test + public void execute_noSales_showNoSalesMessage() { + + Menu menu = new Menu(); + OrderList orderList1 = new OrderList(); + ArrayList orderLists= new ArrayList<>(List.of(orderList1)); + Sales sales = new Sales(orderLists); + + Ui ui = new Ui(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream consoleStream = new PrintStream(baos); + + PrintStream originalOut = System.out; + System.setOut(consoleStream); + + ListTotalSalesCommand listTotalSalesCommand = + new ListTotalSalesCommand(sales, ui, menu); + listTotalSalesCommand.execute(); + + String actualOutput = baos.toString().trim(); + System.setOut(originalOut); + + String expectedOutput = "No sales made."; + + String normalizedExpected = expectedOutput.toLowerCase().replaceAll("\\s+", "").trim(); + String normalizedActual = actualOutput.toLowerCase().replaceAll("\\s+", "").trim(); + + assertEquals(normalizedExpected, normalizedActual); + } +} diff --git a/src/test/java/seedu/cafectrl/data/PantryTest.java b/src/test/java/seedu/cafectrl/data/PantryTest.java index ecd5b000a8..fdcbac801f 100644 --- a/src/test/java/seedu/cafectrl/data/PantryTest.java +++ b/src/test/java/seedu/cafectrl/data/PantryTest.java @@ -1,11 +1,17 @@ package seedu.cafectrl.data; import org.junit.jupiter.api.Test; +import seedu.cafectrl.data.dish.Dish; import seedu.cafectrl.data.dish.Ingredient; import seedu.cafectrl.ui.ErrorMessages; +import seedu.cafectrl.ui.Messages; import seedu.cafectrl.ui.Ui; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -26,4 +32,240 @@ void addIngredientToStock_differentUnitForBuyIngredient_returnErrorMessage() { String expectedErrorMessage = ErrorMessages.UNIT_NOT_MATCHING + "\nUnit used previously: g"; assertEquals(expectedErrorMessage, exception.getMessage()); } + + @Test + void isDishCooked_dishCooked_returnsTrue() { + Ui ui = new Ui(); + Pantry pantry = new Pantry(ui); + + //the following models an order of two chicken rice + //each chicken rice needs 100g of chicken and 50g of rice + //dish ingredients contains the total quantity of ingredients needed for the order + ArrayList dishIngredients = new ArrayList<>(); + dishIngredients.add(new Ingredient("Chicken", 200, "g")); + dishIngredients.add(new Ingredient("Rice", 100, "g")); + + //pantryStock is the quantity of ingredients in the pantry + pantry.addIngredientToStock("Chicken", 300, "g"); + pantry.addIngredientToStock("Rice", 200, "g"); + + boolean expectedIsDishCooked = true; + boolean actualIsDishCooked = pantry.isDishCooked(dishIngredients); + + assert pantry.getPantryStock().get(0).getQty() == 100; + assert pantry.getPantryStock().get(1).getQty() == 100; + assertEquals(expectedIsDishCooked, actualIsDishCooked); + } + + @Test + void isDishCooked_dishNotCooked_returnsFalse() { + Ui ui = new Ui(); + Pantry pantry = new Pantry(ui); + + //the following models an order of two chicken rice + //each chicken rice needs 100g of chicken and 50g of rice + //dish ingredients contains the total quantity of ingredients needed for the order + ArrayList dishIngredients = new ArrayList<>(); + dishIngredients.add(new Ingredient("Chicken", 200, "g")); + dishIngredients.add(new Ingredient("Rice", 100, "g")); + + //pantryStock is the quantity of ingredients in the pantry + pantry.addIngredientToStock("Chicken", 100, "g"); + pantry.addIngredientToStock("Rice", 200, "g"); + + boolean expectedIsDishCooked = false; + boolean actualIsDishCooked = pantry.isDishCooked(dishIngredients); + + assert pantry.getPantryStock().get(0).getQty() == 100; + assert pantry.getPantryStock().get(1).getQty() == 200; + assertEquals(expectedIsDishCooked, actualIsDishCooked); + } + + @Test + void calculateDishAvailability_orderCompleteNoRestock_showMaxDish() { + // Create a dummy menu + ArrayList ingredients = new ArrayList<>(Arrays.asList(new Ingredient("Rice", 100, "g"), + new Ingredient("Chicken", 50, "g"))); + + Dish dishChickenRice = new Dish("Chicken Rice", ingredients, 2.50f); + Dish dishChickenChop = new Dish("Chicken Chop", ingredients, 5.00f); + Menu menu = new Menu(); + menu.addDish(dishChickenRice); + menu.addDish(dishChickenChop); + + Order order = new Order(dishChickenRice, 2); + order.setComplete(true); + + Ui ui = new Ui(); + + Pantry pantry = new Pantry(ui); + pantry.addIngredientToStock("Rice", 200, "g"); + pantry.addIngredientToStock("Chicken", 200, "g"); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream consoleStream = new PrintStream(baos); + + PrintStream originalOut = System.out; + System.setOut(consoleStream); + + pantry.calculateDishAvailability(menu, order); + + String actualOutput = baos.toString().trim(); + System.setOut(originalOut); + + String expectedOutput = "Dish: Chicken Rice" + + "Available Dishes: 2" + + Messages.LINE_STRING + + "Dish: Chicken Chop" + + "Available Dishes: 2" ; + + String normalizedExpected = expectedOutput.toLowerCase().replaceAll("\\s+", "").trim(); + String normalizedActual = actualOutput.toLowerCase().replaceAll("\\s+", "").trim(); + + assertEquals(normalizedExpected, normalizedActual); + } + + @Test + void calculateMaxDish_orderCompleteNoRestock_showMaxDish() { + // Create a dummy menu + ArrayList ingredientChickenRice = new ArrayList<>(Arrays.asList(new Ingredient("Rice", 100, "g"), + new Ingredient("Chicken", 50, "g"))); + ArrayList ingredientChickenChop = new ArrayList<>(List.of(new Ingredient("Chicken", 150, "g"))); + + Dish dishChickenRice = new Dish("Chicken Rice", ingredientChickenRice, 2.50f); + Dish dishChickenChop = new Dish("Chicken Chop", ingredientChickenChop, 5.00f); + Menu menu = new Menu(); + menu.addDish(dishChickenRice); + menu.addDish(dishChickenChop); + + Order order = new Order(dishChickenRice, 2); + order.setComplete(true); + + Ui ui = new Ui(); + + Pantry pantry = new Pantry(ui); + pantry.addIngredientToStock("Rice", 200, "g"); + pantry.addIngredientToStock("Chicken", 200, "g"); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream consoleStream = new PrintStream(baos); + + PrintStream originalOut = System.out; + System.setOut(consoleStream); + + //max number of chicken rice + assert pantry.calculateMaxDishes(dishChickenRice, menu, order) == 2; + //max number of chicken chop + assert pantry.calculateMaxDishes(dishChickenChop, menu, order) == 1; + + String actualOutput = baos.toString().trim(); + System.setOut(originalOut); + + String expectedOutput = ""; + + String normalizedExpected = expectedOutput.toLowerCase().replaceAll("\\s+", "").trim(); + String normalizedActual = actualOutput.toLowerCase().replaceAll("\\s+", "").trim(); + + //No restock strings should be printed out + assertEquals(normalizedExpected, normalizedActual); + } + + @Test + void calculateMaxDish_orderCompleteRestock_showMaxDish() { + //This test case is for when the order is completed and there are not enough ingredients for the next + // Create a dummy menu + ArrayList ingredientChickenRice = new ArrayList<>(Arrays.asList(new Ingredient("Rice", 200, "g"), + new Ingredient("Chicken", 200, "g"))); + ArrayList ingredientChickenChop = new ArrayList<>(List.of(new Ingredient("Chicken", 200, "g"))); + + Dish dishChickenRice = new Dish("Chicken Rice", ingredientChickenRice, 2.50f); + Dish dishChickenChop = new Dish("Chicken Chop", ingredientChickenChop, 5.00f); + Menu menu = new Menu(); + menu.addDish(dishChickenRice); + menu.addDish(dishChickenChop); + + Order order = new Order(dishChickenRice, 2); + order.setComplete(true); + + Ui ui = new Ui(); + + Pantry pantry = new Pantry(ui); + pantry.addIngredientToStock("Rice", 0, "g"); + pantry.addIngredientToStock("Chicken", 0, "g"); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream consoleStream = new PrintStream(baos); + + PrintStream originalOut = System.out; + System.setOut(consoleStream); + + //max number of chicken rice + assert pantry.calculateMaxDishes(dishChickenRice, menu, order) == 0; + + String actualOutput = baos.toString().trim(); + System.setOut(originalOut); + + //when dishChickenRice is passed in as the dish parameter + String expectedOutput = Messages.RESTOCK_CORNER + + Messages.RESTOCK_TITLE + + Messages.RESTOCK_CORNER + + "| Rice | 0g | 200g |" + + Messages.RESTOCK_END_CAP + + "| Chicken | 0g | 200g |" + + Messages.RESTOCK_END_CAP; + + String normalizedExpected = expectedOutput.toLowerCase().replaceAll("\\s+", "").trim(); + String normalizedActual = actualOutput.toLowerCase().replaceAll("\\s+", "").trim(); + + assertEquals(normalizedExpected, normalizedActual); + } + + @Test + void calculateMaxDish_orderIncompleteRestock_showMaxDish() { + // Create a dummy menu + ArrayList ingredientChickenRice = new ArrayList<>(Arrays.asList(new Ingredient("Rice", 200, "g"), + new Ingredient("Chicken", 200, "g"))); + ArrayList ingredientChickenChop = new ArrayList<>(List.of(new Ingredient("Chicken", 200, "g"))); + + Dish dishChickenRice = new Dish("Chicken Rice", ingredientChickenRice, 2.50f); + Dish dishChickenChop = new Dish("Chicken Chop", ingredientChickenChop, 5.00f); + Menu menu = new Menu(); + menu.addDish(dishChickenRice); + menu.addDish(dishChickenChop); + + Order order = new Order(dishChickenRice, 2); + order.setComplete(false); + + Ui ui = new Ui(); + + Pantry pantry = new Pantry(ui); + pantry.addIngredientToStock("Rice", 0, "g"); + pantry.addIngredientToStock("Chicken", 0, "g"); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream consoleStream = new PrintStream(baos); + + PrintStream originalOut = System.out; + System.setOut(consoleStream); + + //max number of chicken rice + assert pantry.calculateMaxDishes(dishChickenRice, menu, order) == 0; + + String actualOutput = baos.toString().trim(); + System.setOut(originalOut); + + //when dishChickenRice is passed in as the dish parameter + String expectedOutput = Messages.RESTOCK_CORNER + + Messages.RESTOCK_TITLE + + Messages.RESTOCK_CORNER + + "| Rice | 0g | 400g |" + + Messages.RESTOCK_END_CAP + + "| Chicken | 0g | 400g |" + + Messages.RESTOCK_END_CAP; + + String normalizedExpected = expectedOutput.toLowerCase().replaceAll("\\s+", "").trim(); + String normalizedActual = actualOutput.toLowerCase().replaceAll("\\s+", "").trim(); + + assertEquals(normalizedExpected, normalizedActual); + } } diff --git a/src/test/java/seedu/cafectrl/parser/ParserTest.java b/src/test/java/seedu/cafectrl/parser/ParserTest.java index 633d8b4793..a0ce54d1e6 100644 --- a/src/test/java/seedu/cafectrl/parser/ParserTest.java +++ b/src/test/java/seedu/cafectrl/parser/ParserTest.java @@ -1,13 +1,15 @@ package seedu.cafectrl.parser; import org.junit.jupiter.api.Test; -import seedu.cafectrl.command.Command; 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; +import seedu.cafectrl.command.ListTotalSalesCommand; +import seedu.cafectrl.command.Command; import seedu.cafectrl.command.IncorrectCommand; -import seedu.cafectrl.command.DeleteDishCommand; import seedu.cafectrl.command.ViewTotalStockCommand; -import seedu.cafectrl.command.BuyIngredientCommand; import seedu.cafectrl.data.CurrentDate; import seedu.cafectrl.data.Menu; @@ -32,7 +34,7 @@ */ class ParserTest { @Test - public void parseCommand_validCommand_successfulCommandParse() { + public void parseCommand_validListIngredientsCommand_successfulCommandParse() { ArrayList menuItems = new ArrayList<>(); menuItems.add(new Dish("Chicken Rice", new ArrayList<>(Arrays.asList(new Ingredient("Rice", 50, "g"), @@ -58,7 +60,7 @@ public void parseCommand_validCommand_successfulCommandParse() { } @Test - public void parseCommand_missingIndex_returnsErrorMessage() { + public void parseCommand_missingListIngredientsIndex_returnsErrorMessage() { Menu menu = new Menu(); Ui ui = new Ui(); Pantry pantry = new Pantry(ui); @@ -77,7 +79,7 @@ public void parseCommand_missingIndex_returnsErrorMessage() { } @Test - public void parseCommand_invalidIndex_returnsErrorMessage() { + public void parseCommand_invalidListIngredientsIndex_returnsErrorMessage() { Menu menu = new Menu(); Ui ui = new Ui(); Pantry pantry = new Pantry(ui); @@ -95,7 +97,7 @@ public void parseCommand_invalidIndex_returnsErrorMessage() { } @Test - public void parseCommand_indexOutOfBounds_returnsErrorMessage() { + public void parseCommand_listIngredientIndexOutOfBounds_returnsErrorMessage() { Menu menu = new Menu(); Ui ui = new Ui(); Pantry pantry = new Pantry(ui); @@ -681,4 +683,96 @@ void parseCommand_invalidArgsForBuyIngredient_returnErrorMessage() { assertEquals(ErrorMessages.INVALID_UNIT_MESSAGE, feedbackToUser); } //@@author + + @Test + void parseCommand_listTotalSalesCommand_returnInstanceOfListTotalSalesCommand() { + Menu menu = new Menu(); + Ui ui = new Ui(); + Pantry pantry = new Pantry(ui); + Sales sales = new Sales(); + CurrentDate currentDate = new CurrentDate(); + String userInput = "list_total_sales"; + ParserUtil parserUtil = new Parser(); + Command result = parserUtil.parseCommand(menu, userInput, ui, pantry, sales, currentDate); + + assertTrue(result instanceof ListTotalSalesCommand); + } + + @Test + void parseCommand_validListSaleIndex_returnInstanceOfListSaleByDayCommand() { + Menu menu = new Menu(); + Ui ui = new Ui(); + Pantry pantry = new Pantry(ui); + Sales sales = new Sales(); + CurrentDate currentDate = new CurrentDate(); + String userInput = "list_sale day/1"; + + ParserUtil parserUtil = new Parser(); + Command result = parserUtil.parseCommand(menu, userInput, ui, pantry, sales, currentDate); + + assertTrue(result instanceof ListSaleByDayCommand); + + ListSaleByDayCommand listSaleByDayCommand = (ListSaleByDayCommand) result; + int day = listSaleByDayCommand.getDay(); + assertEquals(1, day); + } + + @Test + void parseCommand_invalidListSaleIndex_showErrorMessage() { + Menu menu = new Menu(); + Ui ui = new Ui(); + Pantry pantry = new Pantry(ui); + Sales sales = new Sales(); + CurrentDate currentDate = new CurrentDate(); + String userInput = "list_sale day/a"; + + 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_DAY_FORMAT, feedbackToUser); + } + + @Test + void parseCommand_missingListSaleIndex_showErrorMessage() { + Menu menu = new Menu(); + Ui ui = new Ui(); + Pantry pantry = new Pantry(ui); + Sales sales = new Sales(); + CurrentDate currentDate = new CurrentDate(); + String userInput = "list_sale day/"; + + 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_SHOW_SALE_DAY_FORMAT_MESSAGE + + ListSaleByDayCommand.MESSAGE_USAGE, feedbackToUser); + } + + @Test + void parseCommand_invalidListSaleFormat_showErrorMessage() { + Menu menu = new Menu(); + Ui ui = new Ui(); + Pantry pantry = new Pantry(ui); + Sales sales = new Sales(); + CurrentDate currentDate = new CurrentDate(); + String userInput = "list_sale /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_SHOW_SALE_DAY_FORMAT_MESSAGE + + ListSaleByDayCommand.MESSAGE_USAGE, feedbackToUser); + } }