Skip to content

Commit

Permalink
Edit decode sales function according to PR review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
NaychiMin committed Nov 12, 2023
1 parent 0fe6ef4 commit 47c74ad
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/main/java/seedu/cafectrl/data/OrderList.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void printOrderList(Menu menu, Ui ui) {

ui.showSalesAll(aggregatedOrder.getDishName(),
aggregatedOrder.getQuantity(),
dollarValue.format(aggregatedOrder.calculateTotalOrderCost()));
dollarValue.format(calculateTotalCost(aggregatedOrders)));
}

ui.showSalesBottom();
Expand All @@ -80,10 +80,10 @@ private void aggregateOrder(Order order, ArrayList<Order> aggregatedOrders) {
logger.info("Aggregating order...");
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());
aggregatedOrders.get(index)
.setQuantity(aggregatedOrders.get(index).getQuantity() + order.getQuantity());
aggregatedOrders.get(index)
.setTotalOrderCost(aggregatedOrders.get(index).getTotalOrderCost() + order.getTotalOrderCost());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/cafectrl/data/dish/Dish.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public Dish(String name, ArrayList<Ingredient> ingredients, float price) {
}
public Dish(String name, float price) {
this.name = name;
this.ingredients = null;
this.ingredients = new ArrayList<>();
this.price = price;
}

Expand Down
18 changes: 10 additions & 8 deletions src/main/java/seedu/cafectrl/storage/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,24 +205,27 @@ private static void decodeSalesData(String orderLine, ArrayList<OrderList> order
//@@author

int quantity = Integer.parseInt(orderData[2].trim());
float decodedTotalOrderCost = Float.parseFloat(orderData[3].trim());
String completeStatus = orderData[4].trim();
float decodedDishPrice = Float.parseFloat(orderData[3].trim());
float decodedTotalOrderCost = Float.parseFloat(orderData[4].trim());
String completeStatus = orderData[5].trim();
Dish dish = menu.getDishFromName(dishName);

boolean isDataAccurate = isDishValid(orderLine, dish)
&& isOrderCostAccurate(orderLine, dish, quantity, decodedTotalOrderCost)
&& isOrderCostAccurate(orderLine, quantity, decodedDishPrice, decodedTotalOrderCost)
&& isCompleteStatusAccurate(orderLine, completeStatus);
if (!isDataAccurate) {
return;
}

Dish dishToAdd = new Dish(dishName, decodedDishPrice);
//creates new order and adds to orderList for specific day
boolean isComplete = Boolean.parseBoolean(completeStatus.toLowerCase());
Order orderedDish = new Order(menu.getDishFromName(dishName), quantity, decodedTotalOrderCost, isComplete);
Order orderedDish = new Order(dishToAdd, quantity, decodedTotalOrderCost, isComplete);
fillOrderListSize(orderLists, day);
orderLists.get(day).addOrder(orderedDish);
} catch (Exception e) {
logger.log(Level.WARNING, "Line corrupted: " + e.getMessage(), e);
System.out.println(e.getMessage());
ui.showToUser(ErrorMessages.INVALID_SALES_DATA + orderLine);
}
}
Expand Down Expand Up @@ -251,10 +254,9 @@ private static boolean isDishValid(String orderLine, Dish dish) {
* @param decodedTotalOrderCost The decoded total order cost from the order line.
* @return True if the decoded total order cost matches the expected total order cost, false otherwise.
*/
private static boolean isOrderCostAccurate(String orderLine, Dish dish,
int quantity, float decodedTotalOrderCost) {
float costOfDish = dish.getPrice();
float expectedTotalOrderCost = quantity * costOfDish;
private static boolean isOrderCostAccurate(String orderLine, int quantity,
float decodedDishPrice, float decodedTotalOrderCost) {
float expectedTotalOrderCost = quantity * decodedDishPrice;
if (decodedTotalOrderCost == expectedTotalOrderCost) {
return true;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/cafectrl/storage/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public static ArrayList<String> encodeSales(Sales sales) {
orderString.append((day + 1) + DIVIDER);
orderString.append(order.getDishName() + DIVIDER);
orderString.append(order.getQuantity() + DIVIDER);
orderString.append(order.getOrderedDish().getPrice() + DIVIDER);
orderString.append(order.calculateTotalOrderCost() + DIVIDER);
orderString.append(order.getIsComplete());
orderString.append(System.lineSeparator());
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/cafectrl/ui/ErrorMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public class ErrorMessages {
public static final String INVALID_MENU_DATA = "menu.txt: Invalid format, this dish will be removed -> ";

public static final String INVALID_SALES_DATA = "orders.txt: Invalid format, this order will be removed -> ";
public static final String INVALID_ORDER_DATA = "orders.txt: Invalid dish, this order will be removed -> ";
public static final String INVALID_ORDER_DATA = "orders.txt: Dish is not in current menu, "
+ "this order will be removed -> ";
public static final String INVALID_ORDER_STATUS = "orders.txt: Invalid status, this order will be removed -> ";
public static final String INACCURATE_ORDER_COST_DATA = "orders.txt: The total order cost of this order -> \"%s\" "
+ "is inaccurate and will hence be updated from %.2f to %.2f instead.";
Expand Down

0 comments on commit 47c74ad

Please sign in to comment.