diff --git a/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java b/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java index c252619a18..dcd0c71ddb 100644 --- a/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java +++ b/src/main/java/seedu/cafectrl/command/BuyIngredientCommand.java @@ -25,7 +25,7 @@ public class BuyIngredientCommand extends Command { private ArrayList ingredients; private ArrayList ingredientsToBePrinted = new ArrayList<>(); private String ingredientString = ""; // Used to store the message about the bought ingredients - private int FIRST_INDEX = 0; + private int finalIndex = 0; /** * Constructs a BuyIngredientCommand with the specified ingredients, user interface, and pantry. @@ -67,7 +67,7 @@ private void addIngredient() { ingredients.set(i, ingredient); } - for (int i = ingredients.size() - ui.OFFSET_LIST_INDEX; i >= FIRST_INDEX; i--) { + for (int i = ingredients.size() - ui.OFFSET_LIST_INDEX; i >= finalIndex; i--) { Ingredient ingredient = ingredients.get(i); buildBuyIngredientMessage(ingredient); } diff --git a/src/main/java/seedu/cafectrl/data/Sales.java b/src/main/java/seedu/cafectrl/data/Sales.java index e88b056079..c382bb7293 100644 --- a/src/main/java/seedu/cafectrl/data/Sales.java +++ b/src/main/java/seedu/cafectrl/data/Sales.java @@ -10,8 +10,8 @@ */ public class Sales { private static ArrayList orderLists; + private static final int DAY_DISPLAY_OFFSET = 1; private int daysAccounted; - private final int DAY_DISPLAY_OFFSET = 1; public Sales() { this.orderLists = new ArrayList<>(); @@ -21,7 +21,7 @@ public Sales() { public Sales(ArrayList orderLists) { this.orderLists = orderLists; - this.daysAccounted = 0; + this.daysAccounted = orderLists.size() - 1; } //TODO: @Zhong Heng, Remove this method if not used @@ -63,12 +63,12 @@ public void printSales(Ui ui, Menu menu) { ui.showToUser("No sales made."); return; } - ui.showSalesBottom(); + //ui.showSalesBottom(); for (int day = 0; day < orderLists.size(); day++) { OrderList orderList = orderLists.get(day); if (orderList.isEmpty() || !orderList.hasCompletedOrders()) { - ui.showToUser("No sales for day " + (day + DAY_DISPLAY_OFFSET) + "."); + ui.showToUser("", "No sales for day " + (day + DAY_DISPLAY_OFFSET) + ".", ""); continue; } diff --git a/src/main/java/seedu/cafectrl/storage/Decoder.java b/src/main/java/seedu/cafectrl/storage/Decoder.java index 4d49b57c82..f4dfd8e6af 100644 --- a/src/main/java/seedu/cafectrl/storage/Decoder.java +++ b/src/main/java/seedu/cafectrl/storage/Decoder.java @@ -9,6 +9,7 @@ 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.util.ArrayList; @@ -115,32 +116,59 @@ private static boolean isValidPantryStockFormat(String[] decodedPantryStock) { * @return Sales object containing OrderList objects decoded from the provided strings. */ public static Sales decodeSales(ArrayList textLines, Menu menu) { + boolean salesOrderTextTamperDetectionMessagePrinted = false; ArrayList orderLists = new ArrayList<>(); if(textLines.isEmpty()) { return new Sales(); } //for each 'order' in text file for (String line : textLines) { - String[] orderData = line.split(DIVIDER); - int day = Integer.parseInt(orderData[0].trim()) - 1; - String dishName = orderData[1].trim(); - int quantity = Integer.parseInt(orderData[2].trim()); - float totalOrderCost = Float.parseFloat(orderData[3].trim()); - boolean isComplete = "true".equals(orderData[4].trim()); - Dish dish = menu.getDishFromName(dishName); - if(dish == null) { - ui.showDecodedInvalidDish(dishName); - } else { + try { + String[] orderData = line.split(DIVIDER); + int day = Integer.parseInt(orderData[0].trim()) - 1; + String dishName = orderData[1].trim(); + if (dishName.equals(Encoder.NULL_ORDER_DAY)) { + orderLists = fillOrderListSize(orderLists, day); + continue; + } + int quantity = Integer.parseInt(orderData[2].trim()); + float totalOrderCost = Float.parseFloat(orderData[3].trim()); + boolean isComplete = "true".equals(orderData[4].trim()); + Dish dish = menu.getDishFromName(dishName); + if (dish == null) { + ui.showDecodedInvalidDish(dishName); + continue; + } Order orderedDish = new Order(menu.getDishFromName(dishName), quantity, totalOrderCost, isComplete); //increase size of orderLists if needed //this can be used in the event that the text file's first order is not day 0 - while (orderLists.size() <= day) { - orderLists.add(new OrderList()); - } - + orderLists = fillOrderListSize(orderLists, day); orderLists.get(day).addOrder(orderedDish); + } catch (IndexOutOfBoundsException e) { + ui.showToUser(Messages.SALES_LAST_DAY_TEXT_TAMPERED, System.lineSeparator()); + } catch (NumberFormatException e) { + if (!salesOrderTextTamperDetectionMessagePrinted) { + ui.showToUser(Messages.SALES_ORDER_TEXT_TAMPERED, System.lineSeparator()); + salesOrderTextTamperDetectionMessagePrinted = true; + } } } return new Sales(orderLists); } + + //@@author Cazh1 + /** + * Increases the size of the orderlist when there is gap between the previous order and the next + * + * @param orderLists The current partially filled ArrayList of OrderList + * @param day The day of the next order + * @return orderLists after filling in the gaps + */ + private static ArrayList fillOrderListSize(ArrayList orderLists, int day) { + while (orderLists.size() <= day) { + orderLists.add(new OrderList()); + } + return orderLists; + } + } diff --git a/src/main/java/seedu/cafectrl/storage/Encoder.java b/src/main/java/seedu/cafectrl/storage/Encoder.java index ec5c01d7a7..95cb3b29ec 100644 --- a/src/main/java/seedu/cafectrl/storage/Encoder.java +++ b/src/main/java/seedu/cafectrl/storage/Encoder.java @@ -17,6 +17,7 @@ * making the data suitable for saving to a file. */ public class Encoder { + public static final String NULL_ORDER_DAY = "the last day has no orders but please account for it"; private static final String DIVIDER = " | "; //@@author ShaniceTang /** @@ -105,6 +106,30 @@ public static ArrayList encodeSales(Sales sales) { orderString.append(System.lineSeparator()); encodedList.add(String.valueOf(orderString)); } + if (day == sales.getDaysAccounted()) { + encodedList = encodeLastSalesDay(encodedList, orderList, day); + } + } + return encodedList; + } + + //@@author Cazh1 + /** + * Checks if the last day accessed has valid orders added + * + * @param encodedList An ArrayList of strings representing the encoded sales data. + * @param orderList An ArrayList of Orders of the last day accessed + * @param day The last day accessed + * @return encodedList with specific String added at the end if no valid orders were detected + */ + private static ArrayList encodeLastSalesDay(ArrayList encodedList, OrderList orderList, int day) { + if (orderList.getSize() == 0) { + StringBuilder orderString = new StringBuilder(); + //day of each orderList is index + 1 + orderString.append((day + 1) + DIVIDER); + orderString.append(NULL_ORDER_DAY); + orderString.append(System.lineSeparator()); + encodedList.add(String.valueOf(orderString)); } return encodedList; } diff --git a/src/main/java/seedu/cafectrl/ui/Messages.java b/src/main/java/seedu/cafectrl/ui/Messages.java index 6fcd252ece..9c43852aa9 100644 --- a/src/main/java/seedu/cafectrl/ui/Messages.java +++ b/src/main/java/seedu/cafectrl/ui/Messages.java @@ -83,4 +83,13 @@ public class Messages { + "You might have tempered with the file and added in a non existing dish.\n" + "Don't worry :D , we will continue operations without "; + public static final String SALES_LAST_DAY_TEXT_TAMPERED = "Well, well, well! " + + "It seems someone's been playing digital detective and tampered with the last line in the text file.\n" + + "Unfortunately, any empty order lists after the latest valid order have been wiped from my memory banks." + + " Poof!" ; + + public static final String SALES_ORDER_TEXT_TAMPERED = "Well, well, well! " + + "It seems someone's been playing digital detective and tampered with the orders in the text file.\n" + + "Unfortunately, any such orders have been wiped from my memory banks. Poof!" ; + } diff --git a/src/test/java/seedu/cafectrl/command/ListTotalSalesCommandCommandTest.java b/src/test/java/seedu/cafectrl/command/ListTotalSalesCommandCommandTest.java index d794639892..9b70079f05 100644 --- a/src/test/java/seedu/cafectrl/command/ListTotalSalesCommandCommandTest.java +++ b/src/test/java/seedu/cafectrl/command/ListTotalSalesCommandCommandTest.java @@ -81,7 +81,6 @@ public void execute_existingSales_listTotalSales() { System.setOut(originalOut); String expectedOutput = Messages.SHOW_SALES_END_CAP - + Messages.SHOW_SALES_END_CAP + Messages.SHOW_SALES_DAY_PART_1 + "1" + Messages.SHOW_SALES_DAY_PART_2 diff --git a/src/test/java/seedu/cafectrl/command/ViewTotalStockCommandTest.java b/src/test/java/seedu/cafectrl/command/ViewTotalStockCommandTest.java index 68feeba696..788fcbf27c 100644 --- a/src/test/java/seedu/cafectrl/command/ViewTotalStockCommandTest.java +++ b/src/test/java/seedu/cafectrl/command/ViewTotalStockCommandTest.java @@ -3,6 +3,7 @@ import org.junit.jupiter.api.Test; import seedu.cafectrl.data.Pantry; import seedu.cafectrl.data.dish.Ingredient; +import seedu.cafectrl.ui.Messages; import seedu.cafectrl.ui.Ui; import java.io.ByteArrayOutputStream; @@ -32,16 +33,16 @@ void execute_printPantryStock() { String actualOutput = baos.toString().trim(); System.setOut(originalOut); - String expectedOutput = "+-------------------------------------------------------+\n" - + "| You have the following ingredients in pantry: |\n" - + "+----------------------------------------+--------------+\n" - + "| Ingredients | Qty |\n" - + "+----------------------------------------+--------------+\n" + String expectedOutput = Messages.MENU_END_CAP + + Messages.VIEW_STOCK_MESSAGE2 + + Messages.MENU_CORNER + + Messages.VIEW_STOCK_TITLE_MESSAGE + + Messages.MENU_CORNER + "| chicken | 500g |\n" + "| rice | 1000g |\n" - + "+-------------------------------------------------------+\n"; + + Messages.MENU_END_CAP; - assertEquals(expectedOutput.trim().replaceAll("\\s+", " "), - actualOutput.trim().replaceAll("\\s+", " ")); + assertEquals(expectedOutput.trim().replaceAll("\\s+", ""), + actualOutput.trim().replaceAll("\\s+", "")); } }