Skip to content

Commit

Permalink
Merge pull request #304 from NaychiMin/302-add-junit-tests-v2.1
Browse files Browse the repository at this point in the history
Task 302, Add Junit tests for Pantry, Sales, ListTotalSalesCommand & ListSalesCommand
  • Loading branch information
Cazh1 authored Nov 10, 2023
2 parents 3799981 + 9211e8e commit 74ac588
Show file tree
Hide file tree
Showing 9 changed files with 706 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public void execute() {
} catch (Exception e) {
ui.showToUser(ErrorMessages.INVALID_SALE_DAY);
}
}

public int getDay() {
return day;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/seedu/cafectrl/data/Pantry.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ public int calculateMaxDishes(Dish dish, Menu menu, Order order) {
int maxNumofDish = Integer.MAX_VALUE;
ArrayList<Ingredient> dishIngredients = retrieveIngredientsForDish(dish.getName(), menu);
boolean isRestockHeaderDisplayed = false;

for (Ingredient dishIngredient : dishIngredients) {
int numOfDish = calculateMaxDishForEachIngredient(dishIngredient);
maxNumofDish = Math.min(numOfDish, maxNumofDish);
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/seedu/cafectrl/data/Sales.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,16 @@ 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;
}
ui.showSalesBottom();
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;
}

Expand Down Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions src/main/java/seedu/cafectrl/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
}

/**
Expand Down
200 changes: 200 additions & 0 deletions src/test/java/seedu/cafectrl/command/ListSaleByDayCommandTest.java
Original file line number Diff line number Diff line change
@@ -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<Ingredient> 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<OrderList> 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<Ingredient> 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<OrderList> 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<Ingredient> 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<OrderList> 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);
}
}
Loading

0 comments on commit 74ac588

Please sign in to comment.