From cc20faf5a5add93b746d51f28fc1b5598f810200 Mon Sep 17 00:00:00 2001 From: ZH Date: Thu, 9 Nov 2023 23:15:52 +0800 Subject: [PATCH 1/8] Implement text to detect empty OrderList on the last day --- .../command/BuyIngredientCommand.java | 4 +-- src/main/java/seedu/cafectrl/data/Sales.java | 4 +-- .../java/seedu/cafectrl/storage/Decoder.java | 29 ++++++++++++------- .../java/seedu/cafectrl/storage/Encoder.java | 16 ++++++++++ 4 files changed, 39 insertions(+), 14 deletions(-) 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 df2129b093..c6e9a15ef7 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 diff --git a/src/main/java/seedu/cafectrl/storage/Decoder.java b/src/main/java/seedu/cafectrl/storage/Decoder.java index 4d49b57c82..ce1d661ab3 100644 --- a/src/main/java/seedu/cafectrl/storage/Decoder.java +++ b/src/main/java/seedu/cafectrl/storage/Decoder.java @@ -124,23 +124,32 @@ public static Sales decodeSales(ArrayList textLines, Menu menu) { 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) { + if (dish == null) { ui.showDecodedInvalidDish(dishName); - } else { - 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.get(day).addOrder(orderedDish); + 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 + orderLists = fillOrderListSize(orderLists, day); + orderLists.get(day).addOrder(orderedDish); } return new Sales(orderLists); } + + 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..cbee961893 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,21 @@ 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; + } + + 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 + DIVIDER); + orderString.append(System.lineSeparator()); + encodedList.add(String.valueOf(orderString)); } return encodedList; } From 6b8594dd0cd98653fbacbcb2a43b8de1782d3fb2 Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Fri, 10 Nov 2023 01:27:44 +0800 Subject: [PATCH 2/8] Update JavaDoc in Decoder --- src/main/java/seedu/cafectrl/storage/Decoder.java | 7 +++++++ src/main/java/seedu/cafectrl/storage/Encoder.java | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/cafectrl/storage/Decoder.java b/src/main/java/seedu/cafectrl/storage/Decoder.java index ce1d661ab3..2066dab83a 100644 --- a/src/main/java/seedu/cafectrl/storage/Decoder.java +++ b/src/main/java/seedu/cafectrl/storage/Decoder.java @@ -145,6 +145,13 @@ public static Sales decodeSales(ArrayList textLines, Menu menu) { return new Sales(orderLists); } + /** + * 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()); diff --git a/src/main/java/seedu/cafectrl/storage/Encoder.java b/src/main/java/seedu/cafectrl/storage/Encoder.java index cbee961893..8d5636caf8 100644 --- a/src/main/java/seedu/cafectrl/storage/Encoder.java +++ b/src/main/java/seedu/cafectrl/storage/Encoder.java @@ -118,7 +118,7 @@ private static ArrayList encodeLastSalesDay(ArrayList encodedLis StringBuilder orderString = new StringBuilder(); //day of each orderList is index + 1 orderString.append((day + 1) + DIVIDER); - orderString.append(NULL_ORDER_DAY + DIVIDER); + orderString.append(NULL_ORDER_DAY); orderString.append(System.lineSeparator()); encodedList.add(String.valueOf(orderString)); } From 3bc6c3d40c4adbac6c0754770f57069689efde5c Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Fri, 10 Nov 2023 02:05:59 +0800 Subject: [PATCH 3/8] Removed use of "Magic String" --- .../command/ViewTotalStockCommandTest.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/test/java/seedu/cafectrl/command/ViewTotalStockCommandTest.java b/src/test/java/seedu/cafectrl/command/ViewTotalStockCommandTest.java index 68feeba696..46749920ae 100644 --- a/src/test/java/seedu/cafectrl/command/ViewTotalStockCommandTest.java +++ b/src/test/java/seedu/cafectrl/command/ViewTotalStockCommandTest.java @@ -1,8 +1,10 @@ package seedu.cafectrl.command; import org.junit.jupiter.api.Test; +import seedu.cafectrl.data.Menu; 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 +34,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+", "")); } } From a505483ec37a50d069e5340788765844d601e08d Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Fri, 10 Nov 2023 02:31:43 +0800 Subject: [PATCH 4/8] Improve Error handling of the add feature --- .../java/seedu/cafectrl/storage/Decoder.java | 43 +++++++++++-------- src/main/java/seedu/cafectrl/ui/Messages.java | 8 ++++ 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/main/java/seedu/cafectrl/storage/Decoder.java b/src/main/java/seedu/cafectrl/storage/Decoder.java index 2066dab83a..3116dda55b 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; @@ -121,26 +122,32 @@ public static Sales decodeSales(ArrayList textLines, Menu menu) { } //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(); - if (dishName.equals(Encoder.NULL_ORDER_DAY)) { + 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 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; + orderLists.get(day).addOrder(orderedDish); + } catch (IndexOutOfBoundsException e) { + ui.showToUser(Messages.SALES_LAST_DAY_TEXT_TAMPERED, System.lineSeparator()); + } catch (NumberFormatException e) { + ui.showToUser(Messages.SALES_ORDER_TEXT_TAMPERED, System.lineSeparator()); } - 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 - orderLists = fillOrderListSize(orderLists, day); - orderLists.get(day).addOrder(orderedDish); } return new Sales(orderLists); } diff --git a/src/main/java/seedu/cafectrl/ui/Messages.java b/src/main/java/seedu/cafectrl/ui/Messages.java index 6fcd252ece..ea9b2dd839 100644 --- a/src/main/java/seedu/cafectrl/ui/Messages.java +++ b/src/main/java/seedu/cafectrl/ui/Messages.java @@ -83,4 +83,12 @@ 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 from 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!" ; + } From ced96f61eb3e03bc3bc2f2c566524dfa77799fee Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Fri, 10 Nov 2023 02:43:04 +0800 Subject: [PATCH 5/8] Add JavaDocs for added features --- src/main/java/seedu/cafectrl/storage/Decoder.java | 1 + src/main/java/seedu/cafectrl/storage/Encoder.java | 9 +++++++++ src/main/java/seedu/cafectrl/ui/Messages.java | 3 ++- .../cafectrl/command/ViewTotalStockCommandTest.java | 1 - 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/cafectrl/storage/Decoder.java b/src/main/java/seedu/cafectrl/storage/Decoder.java index 3116dda55b..2eb2386012 100644 --- a/src/main/java/seedu/cafectrl/storage/Decoder.java +++ b/src/main/java/seedu/cafectrl/storage/Decoder.java @@ -152,6 +152,7 @@ public static Sales decodeSales(ArrayList textLines, Menu menu) { return new Sales(orderLists); } + //@@author Cazh1 /** * Increases the size of the orderlist when there is gap between the previous order and the next * diff --git a/src/main/java/seedu/cafectrl/storage/Encoder.java b/src/main/java/seedu/cafectrl/storage/Encoder.java index 8d5636caf8..95cb3b29ec 100644 --- a/src/main/java/seedu/cafectrl/storage/Encoder.java +++ b/src/main/java/seedu/cafectrl/storage/Encoder.java @@ -113,6 +113,15 @@ public static ArrayList encodeSales(Sales sales) { 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(); diff --git a/src/main/java/seedu/cafectrl/ui/Messages.java b/src/main/java/seedu/cafectrl/ui/Messages.java index ea9b2dd839..8293647374 100644 --- a/src/main/java/seedu/cafectrl/ui/Messages.java +++ b/src/main/java/seedu/cafectrl/ui/Messages.java @@ -85,7 +85,8 @@ public class Messages { 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 from the latest valid order have been wiped from my memory banks. Poof!" ; + + "Unfortunately, any empty order lists from 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" diff --git a/src/test/java/seedu/cafectrl/command/ViewTotalStockCommandTest.java b/src/test/java/seedu/cafectrl/command/ViewTotalStockCommandTest.java index 46749920ae..788fcbf27c 100644 --- a/src/test/java/seedu/cafectrl/command/ViewTotalStockCommandTest.java +++ b/src/test/java/seedu/cafectrl/command/ViewTotalStockCommandTest.java @@ -1,7 +1,6 @@ package seedu.cafectrl.command; import org.junit.jupiter.api.Test; -import seedu.cafectrl.data.Menu; import seedu.cafectrl.data.Pantry; import seedu.cafectrl.data.dish.Ingredient; import seedu.cafectrl.ui.Messages; From 28af0d0433ba2f6cb453205b108683883fa65e2f Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Fri, 10 Nov 2023 14:04:59 +0800 Subject: [PATCH 6/8] Update error handling to prevent double printing --- src/main/java/seedu/cafectrl/storage/Decoder.java | 6 +++++- src/main/java/seedu/cafectrl/ui/Messages.java | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/cafectrl/storage/Decoder.java b/src/main/java/seedu/cafectrl/storage/Decoder.java index 2eb2386012..f4dfd8e6af 100644 --- a/src/main/java/seedu/cafectrl/storage/Decoder.java +++ b/src/main/java/seedu/cafectrl/storage/Decoder.java @@ -116,6 +116,7 @@ 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(); @@ -146,7 +147,10 @@ public static Sales decodeSales(ArrayList textLines, Menu menu) { } catch (IndexOutOfBoundsException e) { ui.showToUser(Messages.SALES_LAST_DAY_TEXT_TAMPERED, System.lineSeparator()); } catch (NumberFormatException e) { - ui.showToUser(Messages.SALES_ORDER_TEXT_TAMPERED, System.lineSeparator()); + if (!salesOrderTextTamperDetectionMessagePrinted) { + ui.showToUser(Messages.SALES_ORDER_TEXT_TAMPERED, System.lineSeparator()); + salesOrderTextTamperDetectionMessagePrinted = true; + } } } return new Sales(orderLists); diff --git a/src/main/java/seedu/cafectrl/ui/Messages.java b/src/main/java/seedu/cafectrl/ui/Messages.java index 8293647374..9c43852aa9 100644 --- a/src/main/java/seedu/cafectrl/ui/Messages.java +++ b/src/main/java/seedu/cafectrl/ui/Messages.java @@ -85,7 +85,7 @@ public class Messages { 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 from the latest valid order have been wiped from my memory banks." + + "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! " From 725540ffe46caff7cabb0c75c4b8595bef0c374f Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Fri, 10 Nov 2023 14:55:35 +0800 Subject: [PATCH 7/8] Update printing format --- src/main/java/seedu/cafectrl/data/Sales.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/cafectrl/data/Sales.java b/src/main/java/seedu/cafectrl/data/Sales.java index a3e1c28f58..c382bb7293 100644 --- a/src/main/java/seedu/cafectrl/data/Sales.java +++ b/src/main/java/seedu/cafectrl/data/Sales.java @@ -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; } From 78933205c068df9789497e5cba4c6c21a7a25490 Mon Sep 17 00:00:00 2001 From: Cazh1 Date: Fri, 10 Nov 2023 15:02:19 +0800 Subject: [PATCH 8/8] Update Junit for ListTotalSalesCommandCommandTest --- .../seedu/cafectrl/command/ListTotalSalesCommandCommandTest.java | 1 - 1 file changed, 1 deletion(-) 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