Skip to content

Commit

Permalink
Merge pull request #190 from NaychiMin/189_print_orderlist
Browse files Browse the repository at this point in the history
Task 189, implement function to show total sales for all days and sales by specific days.
  • Loading branch information
DextheChik3n authored Nov 1, 2023
2 parents b4b6231 + 59aeae4 commit a176f7b
Show file tree
Hide file tree
Showing 19 changed files with 531 additions and 261 deletions.
6 changes: 6 additions & 0 deletions data/orders.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1 | chicken rice | 2 | 4.0 | true
1 | chicken roast | 2 | 4.0 | true
1 | chicken roast | 2 | 4.0 | true
2 | chicken rice | 2 | 4.0 | true
2 | chicken roast | 2 | 4.0 | true
2 | chicken roast | 8 | 16.0 | false
5 changes: 2 additions & 3 deletions src/main/java/seedu/cafectrl/CafeCtrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ private CafeCtrl() throws IOException {
this.ui.showToUser(Messages.INITIALISE_STORAGE_MESSAGE);
this.storage = new Storage(this.ui);
this.menu = this.storage.loadMenu();
this.pantry = this.storage.loadPantryStock();
this.pantry = this.storage.loadPantryStock(menu);
this.sales = this.storage.loadOrderList(menu);
currentDate = new CurrentDate();
this.sales = new Sales();
}

private void setup() {
Expand All @@ -64,7 +63,7 @@ private void run() throws IOException {
ui.printLine();
}
} while (!command.isExit());
//this.storage.saveAll(this.menu, this.sales, this.pantry);
this.storage.saveAll(this.menu, this.sales, this.pantry);
}

public static void main(String[] args) throws IOException {
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/seedu/cafectrl/command/AddOrderCommand.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package seedu.cafectrl.command;

import seedu.cafectrl.data.Chef;
import seedu.cafectrl.data.Menu;
import seedu.cafectrl.data.Order;
import seedu.cafectrl.data.OrderList;
import seedu.cafectrl.data.Pantry;
import seedu.cafectrl.ui.Ui;
import seedu.cafectrl.data.Chef;

import java.text.DecimalFormat;
import seedu.cafectrl.ui.Ui;

public class AddOrderCommand extends Command {
public static final String COMMAND_WORD = "add_order";
private static final DecimalFormat dollarValue = new DecimalFormat("0.00");
public static final String MESSAGE_USAGE = "To add a new order: \n"
+ COMMAND_WORD
+ "name/DISH_NAME qty/QUANTITY\n"
Expand All @@ -20,24 +19,25 @@ public class AddOrderCommand extends Command {
protected Ui ui;
protected Pantry pantry;
protected OrderList orderList;
protected Menu menu;

Order order;
public AddOrderCommand(Order order, Ui ui, Pantry pantry, OrderList orderList) {
public AddOrderCommand(Order order, Ui ui, Pantry pantry, OrderList orderList, Menu menu) {
this.order = order;
this.ui = ui;
this.pantry = pantry;
this.orderList = orderList;
this.menu = menu;
}
@Override
public void execute() {
orderList.addOrder(order);
Chef chef = new Chef(order, pantry, ui);
Chef chef = new Chef(order, pantry, ui, menu);
chef.cookDish();
//pantry.printPantryStock();
if (order.getIsComplete()) {
orderList.addCost(order);
}
ui.showTotalCost(dollarValue.format(orderList.getTotalOrderListCost()));
//orderList.printOrderList();

}
}
26 changes: 26 additions & 0 deletions src/main/java/seedu/cafectrl/command/ShowSalesByDayCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package seedu.cafectrl.command;

import seedu.cafectrl.data.Menu;
import seedu.cafectrl.data.Sales;
import seedu.cafectrl.ui.Ui;

public class ShowSalesByDayCommand extends Command {
public static final String COMMAND_WORD = "show_sale";

private final int day;
private final Ui ui;
private final Sales sales;
private final Menu menu;

public ShowSalesByDayCommand(int day, Ui ui, Sales sales, Menu menu) {
this.day = day;
this.ui = ui;
this.sales = sales;
this.menu = menu;
}

@Override
public void execute() {
sales.printSaleByDay(ui, menu, day);
}
}
23 changes: 23 additions & 0 deletions src/main/java/seedu/cafectrl/command/ShowSalesCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package seedu.cafectrl.command;

import seedu.cafectrl.data.Menu;
import seedu.cafectrl.data.Sales;
import seedu.cafectrl.ui.Ui;

public class ShowSalesCommand extends Command {
public static final String COMMAND_WORD = "show_sales";
private Sales sales;
private Ui ui;
private Menu menu;

public ShowSalesCommand(Sales sales, Ui ui, Menu menu) {
this.sales = sales;
this.ui = ui;
this.menu = menu;
}

@Override
public void execute() {
sales.printSales(ui, menu);
}
}
21 changes: 16 additions & 5 deletions src/main/java/seedu/cafectrl/data/Chef.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
package seedu.cafectrl.data;

import seedu.cafectrl.ui.Messages;
import seedu.cafectrl.ui.Ui;

import java.text.DecimalFormat;

public class Chef {
private final Order order;
private final Pantry pantry;
private final Ui ui;
private Menu menu;
private final DecimalFormat dollarValue = new DecimalFormat("0.00");

public Chef(Order order, Pantry pantry, Ui ui) {
public Chef(Order order, Pantry pantry, Ui ui, Menu menu) {
this.order = order;
this.pantry = pantry;
this.ui = ui;
this.menu = menu;
}

public void cookDish() {
try {
if (!order.getIsComplete()) {
ui.showChefMessage();
pantry.decreaseIngredientsStock(order.getIngredientList());
order.setComplete();
boolean isComplete = pantry.isDishCooked(order.getIngredientList());
order.setComplete(isComplete);
}
ui.showToUser("Is order completed?: " + order.getIsComplete());
String orderStatus = order.getIsComplete()? Messages.COMPLETE_ORDER : Messages.INCOMPLETE_ORDER;
String totalCost = dollarValue.format(order.getTotalOrderCost());
ui.showOrderStatus(orderStatus, totalCost);
pantry.calculateDishAvailability(menu);
} catch (Exception e) {
ui.showToUser("Unable to cook: " + e.getMessage());
ui.showToUser(e.getMessage());
}
}


}
17 changes: 17 additions & 0 deletions src/main/java/seedu/cafectrl/data/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,21 @@ public boolean isValidDishIndex(int dishIndex) {
int offSetDishIndex = dishIndex - Ui.OFFSET_LIST_INDEX;
return offSetDishIndex >= 0 && offSetDishIndex < this.getSize();
}

//@@author NaychiMin
/**
* Retrieves an ArrayList of Order objects representing aggregated orders for each menu item.
* Each Order object is initialized with a dish from the menu and a quantity of 0.
* Used in the print_sales function under Sales class.
*
* @return An ArrayList of Order objects representing aggregated orders for each menu item.
*/
public ArrayList<Order> getAggregatedOrders() {
ArrayList<Order> aggregatedOrders = new ArrayList<>();
for (int i = 0; i < menuItems.size(); i++) {
Order order = new Order(menuItems.get(i), 0);
aggregatedOrders.add(order);
}
return aggregatedOrders;
}
}
18 changes: 13 additions & 5 deletions src/main/java/seedu/cafectrl/data/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
public class Order {
private static final DecimalFormat dollarValue = new DecimalFormat("0.00");
private final Dish orderedDish;
private final int dishQty;
private int dishQty;
private final ArrayList<Ingredient> ingredientList;
private boolean isComplete = false;
private final float totalOrderCost;
private float totalOrderCost;

public Order(Dish orderedDish, int dishQty) {
this.dishQty = dishQty;
Expand All @@ -21,11 +21,12 @@ public Order(Dish orderedDish, int dishQty) {
this.totalOrderCost = totalOrderCost();
}

public Order(Dish orderedDish, int dishQty, float orderCost) {
public Order(Dish orderedDish, int dishQty, float orderCost, boolean isComplete) {
this.dishQty = dishQty;
this.orderedDish = orderedDish;
this.ingredientList = setIngredientList();
this.totalOrderCost = orderCost;
this.isComplete = isComplete;
}

@Override
Expand Down Expand Up @@ -72,8 +73,8 @@ public float getTotalOrderCost() {
return totalOrderCost;
}

public void setComplete() {
this.isComplete = true;
public void setComplete(boolean isComplete) {
this.isComplete = isComplete;
}

public boolean getIsComplete() {
Expand All @@ -88,4 +89,11 @@ public int getQuantity() {
return dishQty;
}

public void setQuantity(int quantity) {
this.dishQty = quantity;
}

public void setTotalOrderCost(float cost) {
this.totalOrderCost = cost;
}
}
96 changes: 82 additions & 14 deletions src/main/java/seedu/cafectrl/data/OrderList.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package seedu.cafectrl.data;

import seedu.cafectrl.ui.Ui;

import java.text.DecimalFormat;
import java.util.ArrayList;

/**
* The OrderList class represents a list of orders for a specific day.
* It manages the collection of orders and calculates the total cost for the day.
*/
public class OrderList {
private static final DecimalFormat dollarValue = new DecimalFormat("0.00");
private static final String HEADER_FORMAT = "%-20s %-10s %-20s\n";
private ArrayList<Order> orderList;
private float totalOrderListCost;

/**
* Constructs an empty OrderList with no orders and zero total order cost.
*/
public OrderList() {
this.orderList = new ArrayList<>();
this.totalOrderListCost = 0;
}
public OrderList(ArrayList<Order> decodedOrderList){
this.orderList = decodedOrderList;
this.totalOrderListCost = 0;
}

public ArrayList<Order> getOrderList() {
return orderList;
Expand All @@ -28,9 +35,6 @@ public Order getOrder(int orderID) {
public void removeOrder(int orderID) {
orderList.remove(orderID);
}
public boolean isEmpty() {
return orderList.isEmpty();
}

public void addOrder(Order order) {
orderList.add(order);
Expand All @@ -45,16 +49,80 @@ public float getTotalOrderListCost() {
return totalOrderListCost;
}

public void printOrderList() {
//@@author NaychiMin
/**
* Prints the order list for a specific day, including dish names, quantities, and total cost prices.
*
* @param menu The Menu object representing the cafe's menu.
*/
public void printOrderList(Menu menu, Ui ui) {
ArrayList<Order> aggregatedOrders = menu.getAggregatedOrders();
if (orderList.isEmpty()) {
ui.showToUser("No orders for this day.");
return;
}
System.out.println("\nPrinting Orders");
for (int i = 0; i < getSize(); i++) {
Order order = getOrder(i);
String orderString = order.toString();
System.out.println(orderString);

for (Order order : getOrderList()) {
aggregateOrder(order, aggregatedOrders);
}

for (Order aggregatedOrder : aggregatedOrders) {
ui.showToUser(String.format(HEADER_FORMAT,
aggregatedOrder.getDishName(),
aggregatedOrder.getQuantity(),
aggregatedOrder.totalOrderCost()));
}
System.out.println("\nTotal Order cost: $" + dollarValue.format(getTotalOrderListCost()));

ui.showToUser("Total for day: $" + dollarValue.format(calculateTotalCost(aggregatedOrders)));
}

/**
* Aggregates orders by updating quantities and total order costs for the same dish.
*
* @param order The Order object to be aggregated.
* @param aggregatedOrders The ArrayList of aggregated orders.
*/
private void aggregateOrder(Order order, ArrayList<Order> aggregatedOrders) {
if (order.getIsComplete()) {
int index = getIndexByDishName(aggregatedOrders, order.getDishName());
aggregatedOrders.get(index).setQuantity(aggregatedOrders.get(index).getQuantity()
+ order.getQuantity());
aggregatedOrders.get(index).setTotalOrderCost(aggregatedOrders.get(index).getTotalOrderCost()
+ order.getTotalOrderCost());
}
}

/**
* Finds the index of an order in the aggregated orders list based on the dish name.
*
* @param aggregatedOrders The ArrayList of aggregated orders.
* @param dishName The dish name to search for.
* @return The index of the order with the specified dish name, or -1 if not found.
*/
private int getIndexByDishName(ArrayList<Order> aggregatedOrders, String dishName) {
for (int i = 0; i < aggregatedOrders.size(); i++) {
Order order = aggregatedOrders.get(i);
String orderDishName = order.getDishName();
dishName = dishName.trim();
if (orderDishName.equalsIgnoreCase(dishName)) {
return i;
}
}
return -1;
}

/**
* Calculates the total cost of all orders for a specific day.
*
* @param orders The ArrayList of orders.
* @return The total cost of all orders for the day.
*/
private float calculateTotalCost(ArrayList<Order> orders) {
float totalCost = 0;
for (Order order : orders) {
totalCost += order.getTotalOrderCost();
}
return totalCost;
}

}
Loading

0 comments on commit a176f7b

Please sign in to comment.