Skip to content

Commit

Permalink
Enhance error handling for decoding sales.txt by checking if order
Browse files Browse the repository at this point in the history
status is valid.
  • Loading branch information
NaychiMin committed Nov 12, 2023
1 parent 09d8cc1 commit 0fe6ef4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/main/java/seedu/cafectrl/storage/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,18 @@ private static void decodeSalesData(String orderLine, ArrayList<OrderList> order

int quantity = Integer.parseInt(orderData[2].trim());
float decodedTotalOrderCost = Float.parseFloat(orderData[3].trim());
boolean isComplete = "true".equals(orderData[4].trim());

String completeStatus = orderData[4].trim();
Dish dish = menu.getDishFromName(dishName);

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

//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);
fillOrderListSize(orderLists, day);
orderLists.get(day).addOrder(orderedDish);
Expand All @@ -234,11 +235,11 @@ private static void decodeSalesData(String orderLine, ArrayList<OrderList> order
* @return True if the Dish is valid, false otherwise.
*/
private static boolean isDishValid(String orderLine, Dish dish) {
if (dish == null) {
ui.showToUser(ErrorMessages.INVALID_ORDER_DATA + orderLine);
return false;
if (dish != null) {
return true;
}
return true;
ui.showToUser(ErrorMessages.INVALID_ORDER_DATA + orderLine);
return false;
}

/**
Expand All @@ -254,13 +255,23 @@ private static boolean isOrderCostAccurate(String orderLine, Dish dish,
int quantity, float decodedTotalOrderCost) {
float costOfDish = dish.getPrice();
float expectedTotalOrderCost = quantity * costOfDish;
if (decodedTotalOrderCost != expectedTotalOrderCost) {
String messageToUser = String.format(ErrorMessages.INACCURATE_ORDER_COST_DATA,
orderLine, decodedTotalOrderCost, expectedTotalOrderCost );
ui.showToUser(messageToUser);
return false;
if (decodedTotalOrderCost == expectedTotalOrderCost) {
return true;
}
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")) {
return true;
}
ui.showToUser(ErrorMessages.INVALID_ORDER_STATUS + orderLine);
return false;
}


Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/cafectrl/ui/ErrorMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public class ErrorMessages {

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_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 0fe6ef4

Please sign in to comment.