Skip to content

Commit

Permalink
Edit decoding of sales.txt to fix bug related to showing total order
Browse files Browse the repository at this point in the history
cost.
  • Loading branch information
NaychiMin committed Nov 12, 2023
1 parent 47c74ad commit bcd4781
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 31 deletions.
2 changes: 1 addition & 1 deletion 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(calculateTotalCost(aggregatedOrders)));
dollarValue.format(aggregatedOrder.getTotalOrderCost()));
}

ui.showSalesBottom();
Expand Down
32 changes: 4 additions & 28 deletions src/main/java/seedu/cafectrl/storage/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,14 @@ private static void decodeSalesData(String orderLine, ArrayList<OrderList> order
fillOrderListSize(orderLists, day);
return;
}
//@@author

int quantity = Integer.parseInt(orderData[2].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);
String completeStatus = orderData[4].trim();
float totalOrderCost = quantity * decodedDishPrice;

Dish dish = menu.getDishFromName(dishName);
boolean isDataAccurate = isDishValid(orderLine, dish)
&& isOrderCostAccurate(orderLine, quantity, decodedDishPrice, decodedTotalOrderCost)
&& isCompleteStatusAccurate(orderLine, completeStatus);
if (!isDataAccurate) {
return;
Expand All @@ -220,7 +218,7 @@ && isOrderCostAccurate(orderLine, quantity, decodedDishPrice, decodedTotalOrderC
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(dishToAdd, quantity, decodedTotalOrderCost, isComplete);
Order orderedDish = new Order(dishToAdd, quantity, totalOrderCost, isComplete);
fillOrderListSize(orderLists, day);
orderLists.get(day).addOrder(orderedDish);
} catch (Exception e) {
Expand All @@ -245,28 +243,6 @@ private static boolean isDishValid(String orderLine, Dish dish) {
return false;
}

/**
* Checks if the decoded total order cost matches the expected total order cost and shows an error message if not.
*
* @param orderLine The order line in the format "day|dishName|quantity|totalOrderCost|isComplete".
* @param dish The Dish object in the order.
* @param quantity The quantity of the dish in the order.
* @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, int quantity,
float decodedDishPrice, float decodedTotalOrderCost) {
float expectedTotalOrderCost = quantity * decodedDishPrice;
if (decodedTotalOrderCost == expectedTotalOrderCost) {
return true;
}
String messageToUser = String.format(ErrorMessages.INACCURATE_ORDER_COST_DATA,
orderLine, decodedTotalOrderCost, expectedTotalOrderCost );
ui.showToUser(messageToUser);
return false;

}

private static boolean isCompleteStatusAccurate(String orderLine, String completeStatus) {
if (completeStatus.equalsIgnoreCase("true")
|| completeStatus.equalsIgnoreCase("false")) {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/seedu/cafectrl/storage/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ public static ArrayList<String> encodeSales(Sales sales) {
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());
encodedList.add(String.valueOf(orderString));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void execute_existingSales_listTotalSales() {
Order order4 = new Order(dishChickenChop, 1);
order4.setComplete(true);
Order order5 = new Order(dishChickenChop, 1);
order5.setComplete(true);
order5.setComplete(true);

// Create a dummy order list for day 3
OrderList orderList3 = new OrderList();
Expand Down

0 comments on commit bcd4781

Please sign in to comment.