diff --git a/StockMasterData.txt b/StockMasterData.txt deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/docs/Diagrams/Images/Cashier/CashierSequenceDiagram.png b/docs/Diagrams/Images/Cashier/CashierSequenceDiagram.png index 8c8c83c5e2..cc31a04a14 100644 Binary files a/docs/Diagrams/Images/Cashier/CashierSequenceDiagram.png and b/docs/Diagrams/Images/Cashier/CashierSequenceDiagram.png differ diff --git a/docs/Diagrams/Images/Cashier/TotalProfitCommand_SequenceDiagram.png b/docs/Diagrams/Images/Cashier/TotalProfitCommand_SequenceDiagram.png index ddb80ea787..4c8910f65d 100644 Binary files a/docs/Diagrams/Images/Cashier/TotalProfitCommand_SequenceDiagram.png and b/docs/Diagrams/Images/Cashier/TotalProfitCommand_SequenceDiagram.png differ diff --git a/docs/Diagrams/Images/Storage/Storage_sequenceDiagram.png b/docs/Diagrams/Images/Storage/Storage_sequenceDiagram.png index 93d2c6bf21..96cb82c9c4 100644 Binary files a/docs/Diagrams/Images/Storage/Storage_sequenceDiagram.png and b/docs/Diagrams/Images/Storage/Storage_sequenceDiagram.png differ diff --git a/docs/Diagrams/PUML files/Cashier/CashierSequenceDiagram.puml b/docs/Diagrams/PUML files/Cashier/CashierSequenceDiagram.puml index 7b5052702c..80f701f224 100644 --- a/docs/Diagrams/PUML files/Cashier/CashierSequenceDiagram.puml +++ b/docs/Diagrams/PUML files/Cashier/CashierSequenceDiagram.puml @@ -10,7 +10,7 @@ Command -> Cashier: addItem(transaction) activate Cashier Cashier -> Cashier: transactions.add(transaction) return -Command --> TextUi: showSellMessage() +Command -> TextUi: showSellMessage() activate TextUi return alt if itemName is not given diff --git a/docs/Diagrams/PUML files/Cashier/TotalProfitCommand_SequenceDiagram.puml b/docs/Diagrams/PUML files/Cashier/TotalProfitCommand_SequenceDiagram.puml index 79f89dc5b7..1e53df17ea 100644 --- a/docs/Diagrams/PUML files/Cashier/TotalProfitCommand_SequenceDiagram.puml +++ b/docs/Diagrams/PUML files/Cashier/TotalProfitCommand_SequenceDiagram.puml @@ -5,6 +5,8 @@ participant ":TotalProfitCommand" as TotalProfitCommand participant ":Cashier" as Cashier participant ":TextUi" as TextUi + +mainframe sd alt get total revenue TotalProfitCommand -> Cashier: getTotalRevenue() activate Cashier @@ -21,5 +23,4 @@ else get total profit TotalProfitCommand -> TextUi: replyToUser("You have earned {totalProfit} in profits so far.") end - @enduml \ No newline at end of file diff --git a/docs/Diagrams/PUML files/Storage/Storage_sequenceDiagram.puml b/docs/Diagrams/PUML files/Storage/Storage_sequenceDiagram.puml index d41e1ecff8..4e46708d64 100644 --- a/docs/Diagrams/PUML files/Storage/Storage_sequenceDiagram.puml +++ b/docs/Diagrams/PUML files/Storage/Storage_sequenceDiagram.puml @@ -1,37 +1,40 @@ @startuml !include ../Style.puml -participant Client -participant Storage -participant Itemlist -participant Item -participant Scanner -participant File +participant ":Client" +participant ":Storage" +participant ":Itemlist" +participant ":Item" +participant ":Scanner" +participant ":File" -Client -> Storage: readFromFile(fileName) -activate Storage -Storage -> File: new File(fileName) -activate File -Storage -> Scanner: new Scanner(File) -activate Scanner -Scanner --> File: interpretLines(scanner) -File --> Storage: Close Scanner -deactivate Scanner -deactivate File -deactivate Storage +":Client" -> ":Storage": readFromFile(fileName) +activate ":Storage" +":Storage" -> ":File": new File(fileName) +activate ":File" +":Storage" -> ":Scanner": new Scanner(File) +activate ":Scanner" +":Scanner" -> ":File": interpretLines(scanner) +":File" --> ":Storage": Close Scanner +deactivate ":Scanner" +deactivate ":File" +":Storage" --> ":Client" +deactivate ":Storage" -Client -> Storage: addToFile(items) -activate Storage -Storage -> Itemlist: addItem(toAdd) -activate Itemlist -Itemlist --> Storage -deactivate Itemlist -Storage -> Storage: updateFile(descriptionAdded, true) -deactivate Storage +":Client" -> ":Storage": addToFile(items) +activate ":Storage" +":Storage" -> ":Itemlist": addItem(toAdd) +activate ":Itemlist" +":Itemlist" --> ":Storage" +deactivate ":Itemlist" +":Storage" -> ":Storage": updateFile(descriptionAdded, true) +":Storage" --> ":Client" +deactivate ":Storage" -Client -> Storage: overwriteFile(items) -activate Storage +":Client" -> ":Storage": overwriteFile(items) +activate ":Storage" loop for each item in items - Storage -> Storage: updateFile(descriptionAdded, ifAppend) + ":Storage" -> ":Storage": updateFile(descriptionAdded, ifAppend) end -deactivate Storage +":Storage" --> ":Client" +deactivate ":Storage" @enduml \ No newline at end of file diff --git a/docs/team/fureimi.md b/docs/team/fureimi.md index 03989d2079..81a867f9a5 100644 --- a/docs/team/fureimi.md +++ b/docs/team/fureimi.md @@ -38,7 +38,7 @@ only want to see their marked items. The mark feature is explained below. - **Documentation**: - User Guide: - Added documentation for the features `list_items`, `mark`, `unmark` and `edit` - - Update the command summary + - Update the command summary. - Developer Guide: - Added class diagram of the `EditCommand` class. - Added implementation details and sequence diagram of both the `list_items` and `edit` features. diff --git a/docs/team/lowtl.md b/docs/team/lowtl.md index 5fd9b02b1a..ea75b5ca05 100644 --- a/docs/team/lowtl.md +++ b/docs/team/lowtl.md @@ -7,7 +7,7 @@ manage and operate their business. **Code Contributed:** [Reposense Link](https://nus-cs2113-ay2324s2.github.io/tp-dashboard/?search=LowTL&sort=groupTitle&sortWithin=title&timeframe=commit&mergegroup=&groupSelect=groupByRepos&breakdown=true&checkedFileTypes=docs~functional-code~test-code~other&since=2024-02-23&tabOpen=true&tabType=authorship&tabAuthor=LowTL&tabRepo=AY2324S2-CS2113-T15-4%2Ftp%5Bmaster%5D&authorshipIsMergeGroup=false&authorshipFileTypes=functional-code&authorshipIsBinaryFileTypeChecked=false&authorshipIsIgnoredFilesChecked=false) -### Features implemented +### Enhancements implemented * `Cashier` class. * Allows users to create and store `Transactions`, in an extension of the `item` class. * Contains the `getTotalProfit`, `getTotalRevenue`, and `getBestseller` methods, for basic business analysis. diff --git a/src/main/java/command/BestsellerCommand.java b/src/main/java/command/BestsellerCommand.java index d1f0aebd9d..6b9ba2d764 100644 --- a/src/main/java/command/BestsellerCommand.java +++ b/src/main/java/command/BestsellerCommand.java @@ -13,7 +13,7 @@ public void execute() throws EmptyListException { String bs = Cashier.getBestseller(); try { if (bs == null) { - throw new EmptyListException("Transaction"); + throw new EmptyListException("Bestseller"); } } catch (EmptyListException e) { LOGGER.warning("No transaction found."); diff --git a/src/main/java/command/ListCommand.java b/src/main/java/command/ListCommand.java index 6166b52e1c..18effc13e6 100644 --- a/src/main/java/command/ListCommand.java +++ b/src/main/java/command/ListCommand.java @@ -24,13 +24,25 @@ public class ListCommand extends Command{ protected boolean isListMarked = false; /** - * Instantiates a ListCommand with the ArrayList arrayList - * and any other modifiers available to that type of ArrayList. - */ + * Instantiates a ListCommand with the ArrayList arrayList + * and any other modifiers available to that type of ArrayList. + */ public ListCommand(ArrayList arrayList, String category, boolean isListMarked) { this.itemList= arrayList; this.category = category; this.isListMarked = isListMarked; + LOGGER.info("List items command generated."); + try { + if (arrayList == null || arrayList.isEmpty()) { + if (category.equals("NA") && !isListMarked) { + throw new EmptyListException("Item"); + } else { + throw new EmptyListException("Filter Item"); + } + } + } catch (EmptyListException e) { + LOGGER.warning("Empty item list."); + } } /** @@ -43,6 +55,17 @@ public ListCommand(ArrayList arrayList, String itemName) { } else { this.transactionList = Cashier.getTransactions(itemName); } + try { + if (transactionList == null || transactionList.isEmpty()) { + if (itemName.equals("NA")) { + throw new EmptyListException("Transaction"); + } else { + throw new EmptyListException("Filter Transaction"); + } + } + } catch (EmptyListException e) { + LOGGER.warning("Empty list detected."); + } } /** @@ -50,6 +73,13 @@ public ListCommand(ArrayList arrayList, String itemName) { * and any other modifiers available to that type of ArrayList. */ public ListCommand(ArrayList arrayList) { + try { + if (arrayList.isEmpty()) { + throw new EmptyListException("Promotion"); + } + } catch (EmptyListException e) { + LOGGER.warning("Empty list detected."); + } this.promotionList = arrayList; } @@ -60,10 +90,9 @@ public String getCategory() { /** * Runs the list command with 3 cases * Depending on which ArrayList is not empty, it prints that list. - * @throws EmptyListException if all Lists are empty. * */ //@@author Fureimi - public void execute() throws EmptyListException { + public void execute() { if (containsTransactions(transactionList)) { showTransactionList(); @@ -71,15 +100,12 @@ public void execute() throws EmptyListException { } else if (containsPromotions(promotionList)) { showPromotionList(); LOGGER.info("Promotions listed."); - } else if (category.equals("NA") && !isListMarked) { - TextUi.showList(itemList); - LOGGER.info("All items listed."); - } else if (containsItems(itemList)) { + } else if (containsItems(itemList) && (!category.equals("NA") || isListMarked)) { showCustomizedItemList(); - LOGGER.info("Items listed."); - } else { - LOGGER.warning("No results found."); - throw new EmptyListException("Empty List"); + LOGGER.info("Customised item listed."); + } else if (containsItems(itemList) && category.equals("NA") && !isListMarked) { + TextUi.showList(itemList); + LOGGER.info("All item listed."); } } diff --git a/src/main/java/command/TotalProfitCommand.java b/src/main/java/command/TotalProfitCommand.java index 4991028cda..9c5cd163c6 100644 --- a/src/main/java/command/TotalProfitCommand.java +++ b/src/main/java/command/TotalProfitCommand.java @@ -13,6 +13,9 @@ public TotalProfitCommand(CommandType command) { public void execute() { float totalProfit = command.equals(CommandType.TOTAL_PROFIT) ? Cashier.getTotalProfit() : Cashier.getTotalRevenue(); + if (totalProfit == 0) { + return; + } TextUi.replyToUser("You have earned " + totalProfit + " in "+ (command.equals(CommandType.TOTAL_PROFIT) ? "profits" : "revenue") + " so far."); } diff --git a/src/main/java/common/HelpMessages.java b/src/main/java/common/HelpMessages.java index e89921110e..c46e320f3f 100644 --- a/src/main/java/common/HelpMessages.java +++ b/src/main/java/common/HelpMessages.java @@ -10,6 +10,9 @@ public class HelpMessages { "|------------|------------------------------------------------------------------------------|\n" + "| list items | list_items [marked] [cat/CATEGORY] |\n" + "|------------|------------------------------------------------------------------------------|\n" + + "| list | list_transactions [item/ITEM_NAME] |\n" + + "| transaction| |\n" + + "|------------|------------------------------------------------------------------------------|\n" + "| add | add ITEM_NAME qty/QUANTITY_OF_ITEM /UNIT_OF_MEASUREMENT [cat/CATEGORY] |\n" + "| | buy/BUY_PRICE sell/SELL_PRICE |\n" + "|------------|------------------------------------------------------------------------------|\n" + @@ -233,7 +236,7 @@ public class HelpMessages { "|-------------------|-----------------------------------------------------------------------|\n" + "| list_transactions | list_transaction [item/ITEM_NAME] |\n" + "|-------------------|-----------------------------------------------------------------------|\n" + - "| example: | list_transaction item/apple |\n" + + "| example: | list_transactions item/apple |\n" + "|___________________|_______________________________________________________________________|\n"; public static final String INVALID_HELP_COMMAND = "Please input a valid command to inquire about."; diff --git a/src/main/java/common/Messages.java b/src/main/java/common/Messages.java index 8a0892c08e..e4f64061b1 100644 --- a/src/main/java/common/Messages.java +++ b/src/main/java/common/Messages.java @@ -26,8 +26,10 @@ public class Messages { public static final String UNABLE_TO_DELETE = "There is a promotion that exists for this item. Please remove the " + "promotion before deleting the item."; - public static final String EMPTY_ITEM_LIST = "There are no items with your search query."; - public static final String EMPTY_TRANSACTION_LIST = "There are no transactions with your search query."; + public static final String EMPTY_ITEM_LIST = "There are no items at the moment."; + public static final String EMPTY_TRANSACTION_LIST = "There are no transactions at the moment."; + public static final String EMPTY_FILTERED_ITEM_LIST = "There are no items with your search query."; + public static final String EMPTY_FILTERED_TRANSACTION_LIST = "There are no transactions with your search query."; public static final String EMPTY_LIST = "There is nothing here! Time to spend some money and stock em up!"; public static final String WELCOME_MESSAGE = "Welcome to StockMaster, where you can master the knowledge on your " + "Stock!"; @@ -96,5 +98,8 @@ public class Messages { public static final String INVALID_VALUE = "Please input a valid amount."; + public static final String NO_BESTSELLER = "There are no transactions. Please add a transaction " + + "before retrying the command."; + public static final String EMPTY_PROMOTION_LIST = "There are no promotions at the moment."; } diff --git a/src/main/java/exceptions/EmptyListException.java b/src/main/java/exceptions/EmptyListException.java index 4ff0f223c7..06b9a0c2fe 100644 --- a/src/main/java/exceptions/EmptyListException.java +++ b/src/main/java/exceptions/EmptyListException.java @@ -6,15 +6,27 @@ public class EmptyListException extends Exception { public EmptyListException(String error) { switch (error) { + case "Filter Item": + System.out.println(Messages.EMPTY_FILTERED_ITEM_LIST); + break; case "Item": System.out.println(Messages.EMPTY_ITEM_LIST); break; + case "Filter Transaction": + System.out.println(Messages.EMPTY_FILTERED_TRANSACTION_LIST); + break; case "Transaction": System.out.println(Messages.EMPTY_TRANSACTION_LIST); break; + case "Promotion": + System.out.println(Messages.EMPTY_PROMOTION_LIST); + break; case "Empty List": System.out.println("No results found."); break; + case "Bestseller": + System.out.println(Messages.NO_BESTSELLER); + break; default: System.out.println(error); } diff --git a/src/main/java/itemlist/Cashier.java b/src/main/java/itemlist/Cashier.java index 56a1681697..7e1c572dea 100644 --- a/src/main/java/itemlist/Cashier.java +++ b/src/main/java/itemlist/Cashier.java @@ -31,12 +31,8 @@ public static ArrayList getTransactions(String itemName) { results.add(t); } } - LOGGER.info("Transactions filtered."); - return results; - } else { - LOGGER.warning("No transactions found."); - return null; } + return results; } public static float getTotalRevenue() { @@ -44,7 +40,7 @@ public static float getTotalRevenue() { try { ArrayList allTransactions = getTransactions(); if (allTransactions.isEmpty()) { - throw new EmptyListException("Transaction"); + throw new EmptyListException("Bestseller"); } for (Transaction t : allTransactions) { totalRevenue += t.getTotalPrice(); @@ -60,7 +56,7 @@ public static float getTotalProfit() { float totalProfit = 0; try { if (transactions.isEmpty()) { - throw new EmptyListException("Transaction"); + throw new EmptyListException("Bestseller"); } for (Transaction t : transactions) { totalProfit += t.getProfit(); diff --git a/src/main/java/seedu/duke/StockMaster.java b/src/main/java/seedu/duke/StockMaster.java index cacdf490af..dadd2a115e 100644 --- a/src/main/java/seedu/duke/StockMaster.java +++ b/src/main/java/seedu/duke/StockMaster.java @@ -84,7 +84,7 @@ private static void initLogger() { } } - private void normalOperation() throws IOException, CommandFormatException, + private void normalOperation() throws CommandFormatException, InvalidDateException, EmptyListException { String userInput; do { diff --git a/src/main/java/ui/TextUi.java b/src/main/java/ui/TextUi.java index f67a00a9fc..242cbcb781 100644 --- a/src/main/java/ui/TextUi.java +++ b/src/main/java/ui/TextUi.java @@ -95,19 +95,20 @@ public static void replyToUser(String... message) { * @param arrayList The arraylist to show to the user. */ public static void showList(ArrayList arrayList) { - if (arrayList.isEmpty()) { + if (arrayList == null || arrayList.isEmpty()) { replyToUser(Messages.EMPTY_LIST); return; - } - replyToUser("List: "); - int index = 1; - for (T item : arrayList) { - if (item == null) { - break; + } else { + replyToUser("List: "); + int index = 1; + for (T item : arrayList) { + if (item == null) { + break; + } + String listItem = index + ". " + item; + replyToUser(listItem); + index++; } - String listItem = index + ". " + item; - replyToUser(listItem); - index++; } } @@ -136,10 +137,8 @@ public static void showSellMessage(String item, int sellQuantity, int remainingQ * @param isListMarked Whether the items listed are marked items. */ public static void showCustomizedList(ArrayList arrayList, String category, boolean isListMarked) { - if (arrayList.isEmpty()) { - replyToUser(Messages.EMPTY_LIST); - // case 1: user wants to list all items of a certain category - } else if (!category.equals("NA") && !isListMarked) { + // case 1: user wants to list all items of a certain category + if (!category.equals("NA") && !isListMarked) { int flag = 0; int counter = 1; for (Item item : arrayList) { diff --git a/src/test/java/command/BestsellerCommandTest.java b/src/test/java/command/BestsellerCommandTest.java index c69b6fa2e1..776ed3ac3b 100644 --- a/src/test/java/command/BestsellerCommandTest.java +++ b/src/test/java/command/BestsellerCommandTest.java @@ -36,7 +36,7 @@ public void testExecuteWithEmptyBestseller() throws EmptyListException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(outputStream)); bestsellerCommand.execute(); - String expected = Messages.EMPTY_TRANSACTION_LIST + System.lineSeparator(); + String expected = Messages.NO_BESTSELLER + System.lineSeparator(); assertEquals(expected, outputStream.toString()); } catch (EmptyListException e) { throw new RuntimeException(e); diff --git a/src/test/java/command/ListCommandTest.java b/src/test/java/command/ListCommandTest.java index ec00d753f8..6b1108f5e3 100644 --- a/src/test/java/command/ListCommandTest.java +++ b/src/test/java/command/ListCommandTest.java @@ -1,6 +1,5 @@ package command; -import common.Messages; import exceptions.CommandFormatException; import exceptions.EmptyListException; import exceptions.InvalidDateException; @@ -25,7 +24,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -public class ListCommandTest { +public class ListCommandTest extends Cashier { //clears all the lists @BeforeEach @@ -37,7 +36,7 @@ public void reset() { Itemlist.deleteItem(0); } while (Cashier.getTransaction(0) != null) { - Cashier.transactions = new ArrayList<>(); + transactions = new ArrayList<>(); } Storage.updateFile("", false); PromotionStorage.updateFile("", false); @@ -47,165 +46,104 @@ public void reset() { //happy case: has item @Test public void listCommandTest_itemList_correct() { - Item test = new Item("testItem", 1, "ea", "NA", - 1.00F, 2.00F); + Item test = new Item("testItem", 1, "ea", "NA", 1.00F, 2.00F); Itemlist.addItem(test); ListCommand listCommand1 = new ListCommand(Itemlist.getItems(), "NA", false); - try { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outputStream)); - listCommand1.execute(); - String expected = "List: " + System.lineSeparator() + "1. " + test + System.lineSeparator(); - assertEquals(expected, outputStream.toString()); - } catch (EmptyListException e) { - Assertions.fail("Unexpected EmptyListException thrown."); - throw new RuntimeException(e); - } + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + listCommand1.execute(); + String expected = "List: " + System.lineSeparator() + "1. " + test + System.lineSeparator(); + assertEquals(expected, outputStream.toString()); } - //happy case: has item, filtered corrrectly + //happy case: has item, filtered correctly @Test public void listCommandTest_itemList_correct2() { - Item test = new Item("testItem", 1, "ea", "test", - 1.00F, 2.00F); + Item test = new Item("testItem", 1, "ea", "test", 1.00F, 2.00F); Itemlist.addItem(test); ListCommand listCommand1 = new ListCommand(Itemlist.getItems(), "test", false); - try { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outputStream)); - listCommand1.execute(); - String expected = "1. Item Index: 1. " + test + System.lineSeparator(); - assertEquals(expected, outputStream.toString()); - } catch (EmptyListException e) { - Assertions.fail("Unexpected EmptyListException thrown."); - throw new RuntimeException(e); - } + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + listCommand1.execute(); + String expected = "1. Item Index: 1. " + test + System.lineSeparator(); + assertEquals(expected, outputStream.toString()); } //happy case: has item, filtered wrongly @Test public void listCommandTest_itemList_correct3() { - Item test = new Item("testItem", 1, "ea", "noCat", - 1.00F, 2.00F); + Item test = new Item("testItem", 1, "ea", "noCat", 1.00F, 2.00F); Itemlist.addItem(test); ListCommand listCommand1 = new ListCommand(Itemlist.getItems(), "test", false); - try { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outputStream)); - listCommand1.execute(); - String expected = "No items were found within the category test." + System.lineSeparator(); - assertEquals(expected, outputStream.toString()); - } catch (EmptyListException e) { - Assertions.fail("Unexpected EmptyListException thrown."); - throw new RuntimeException(e); - } + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + listCommand1.execute(); + String expected = "No items were found within the category test." + System.lineSeparator(); + assertEquals(expected, outputStream.toString()); } //happy case: has item, search for marked and is marked @Test public void listCommandTest_itemList_correct4() { - Item test = new Item("testItem", 1, "ea", "NA", - 1.00F, 2.00F); + Item test = new Item("testItem", 1, "ea", "NA", 1.00F, 2.00F); Itemlist.addItem(test); Objects.requireNonNull(Itemlist.getItem(0)).mark(); ListCommand listCommand1 = new ListCommand(Itemlist.getItems(), "NA", true); - try { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outputStream)); - listCommand1.execute(); - String expected = "1. Item Index: 1. " + test + System.lineSeparator(); - assertEquals(expected, outputStream.toString()); - } catch (EmptyListException e) { - Assertions.fail("Unexpected EmptyListException thrown."); - throw new RuntimeException(e); - } + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + listCommand1.execute(); + String expected = "1. Item Index: 1. " + test + System.lineSeparator(); + assertEquals(expected, outputStream.toString()); } //Error: Filter for marked, but no items are marked @Test public void listCommandTest_itemList_correct5() { - Item test = new Item("testItem", 1, "ea", "NA", - 1.00F, 2.00F); + Item test = new Item("testItem", 1, "ea", "NA", 1.00F, 2.00F); Itemlist.addItem(test); ListCommand listCommand1 = new ListCommand(Itemlist.getItems(), "NA", true); - try { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outputStream)); - listCommand1.execute(); - String expected = "There are no marked items in your inventory list!" + System.lineSeparator(); - assertEquals(expected, outputStream.toString()); - } catch (EmptyListException e) { - Assertions.fail("Unexpected EmptyListException thrown."); - throw new RuntimeException(e); - } + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + listCommand1.execute(); + String expected = "There are no marked items in your inventory list!" + System.lineSeparator(); + assertEquals(expected, outputStream.toString()); } //Error: Filter for marked + cat, have result @Test public void listCommandTest_itemList_correct6() { - Item test = new Item("testItem", 1, "ea", "test", - 1.00F, 2.00F); + Item test = new Item("testItem", 1, "ea", "test", 1.00F, 2.00F); Itemlist.addItem(test); Objects.requireNonNull(Itemlist.getItem(0)).mark(); ListCommand listCommand1 = new ListCommand(Itemlist.getItems(), "test", true); - try { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outputStream)); - listCommand1.execute(); - String expected = "1. Item Index: 1. " + test + System.lineSeparator(); - assertEquals(expected, outputStream.toString()); - } catch (EmptyListException e) { - Assertions.fail("Unexpected EmptyListException thrown."); - throw new RuntimeException(e); - } - - } - - //Error: Filter for marked + cat, have no result - @Test - public void listCommandTest_itemList_correct7() { - Item test = new Item("testItem", 1, "ea", "test", - 1.00F, 2.00F); - Itemlist.addItem(test); - ListCommand listCommand1 = new ListCommand(Itemlist.getItems(), "test", true); - try { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outputStream)); - listCommand1.execute(); - String expected = "There are no marked items of category '" + "test"+ - "' in your inventory list!" + System.lineSeparator(); - assertEquals(expected, outputStream.toString()); - } catch (EmptyListException e) { - Assertions.fail("Unexpected EmptyListException thrown."); - throw new RuntimeException(e); - } + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + listCommand1.execute(); + String expected = "1. Item Index: 1. " + test + System.lineSeparator(); + assertEquals(expected, outputStream.toString()); } //Happy case: has 1 transaction @Test public void listCommandTest_transactionList_correct() { - AddCommand addCommand = new AddCommand("testItem", 1, "ea", - "NA", 1.00F, 2.00F); + AddCommand addCommand = new AddCommand("testItem", 1, "ea", "NA", 1.00F, 2.00F); addCommand.execute(); SellCommand sellCommand = new SellCommand("testItem", 1, 0); - ListCommand listCommand1 = new ListCommand(Cashier.transactions, "NA"); + ListCommand listCommand1 = new ListCommand(transactions, "NA"); try { sellCommand.execute(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(outputStream)); listCommand1.execute(); - String expected = "1. " + Cashier.transactions.get(0) + System.lineSeparator(); + String expected = "1. " + transactions.get(0) + System.lineSeparator(); assertEquals(expected, outputStream.toString()); - } catch (EmptyListException e) { - Assertions.fail("Unexpected EmptyListException thrown."); - throw new RuntimeException(e); } catch (CommandFormatException e) { Assertions.fail("Unexpected CommandFormatException thrown"); throw new RuntimeException(e); @@ -216,21 +154,17 @@ public void listCommandTest_transactionList_correct() { //happy case: filtered item successfully @Test public void listCommandTest_transactionList_correct2() { - AddCommand addCommand = new AddCommand("testItem", 1, "ea", - "NA", 1.00F, 2.00F); + AddCommand addCommand = new AddCommand("testItem", 1, "ea", "NA", 1.00F, 2.00F); addCommand.execute(); SellCommand sellCommand = new SellCommand("testItem", 1, 0); try { sellCommand.execute(); - ListCommand listCommand1 = new ListCommand(Cashier.transactions, "testItem"); + ListCommand listCommand1 = new ListCommand(transactions, "testItem"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(outputStream)); listCommand1.execute(); - String expected = "1. " + Cashier.transactions.get(0) + System.lineSeparator(); + String expected = "1. " + transactions.get(0) + System.lineSeparator(); assertEquals(expected, outputStream.toString()); - } catch (EmptyListException e) { - Assertions.fail("Unexpected EmptyListException thrown."); - throw new RuntimeException(e); } catch (CommandFormatException e) { Assertions.fail("Unexpected CommandFormatException thrown"); throw new RuntimeException(e); @@ -240,21 +174,17 @@ public void listCommandTest_transactionList_correct2() { @Test public void listCommandTest_transactionList_correct3() { - AddCommand addCommand = new AddCommand("testItem", 1, "ea", - "NA", 1.00F, 2.00F); + AddCommand addCommand = new AddCommand("testItem", 1, "ea", "NA", 1.00F, 2.00F); addCommand.execute(); SellCommand sellCommand = new SellCommand("testItem", 1, 0); - ListCommand listCommand1 = new ListCommand(Cashier.transactions, "failTest"); + ListCommand listCommand1 = new ListCommand(transactions, "failTest"); try { sellCommand.execute(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(outputStream)); listCommand1.execute(); - String expected = Messages.EMPTY_LIST + System.lineSeparator(); + String expected = ""; assertEquals(expected, outputStream.toString()); - } catch (EmptyListException e) { - Assertions.fail("Unexpected EmptyListException thrown."); - throw new RuntimeException(e); } catch (CommandFormatException e) { Assertions.fail("Unexpected CommandFormatException thrown"); throw new RuntimeException(e); @@ -264,12 +194,10 @@ public void listCommandTest_transactionList_correct3() { @Test public void listCommandTest_promotionList_correct() throws InvalidDateException, CommandFormatException { - AddCommand addCommand = new AddCommand("testItem", 1, "ea", - "NA", 1.00F, 2.00F); + AddCommand addCommand = new AddCommand("testItem", 1, "ea", "NA", 1.00F, 2.00F); addCommand.execute(); - Command testPromo = new AddPromotionCommand("testItem", 0.30F, 2, - Month.valueOf("APR"), 2024, 4, Month.valueOf("APR"), - 2024, 0000, 1100); + Command testPromo = new AddPromotionCommand("testItem", 0.30F, 2, Month.valueOf("APR"), + 2024, 4, Month.valueOf("APR"), 2024, 0000, 1100); try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(outputStream)); @@ -291,45 +219,33 @@ public void listCommandTest_promotionList_correct() throws InvalidDateException, @Test public void listCommandTest_itemList_empty() { ListCommand listCommand1 = new ListCommand(new ArrayList(), "NA", false); - try { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outputStream)); - listCommand1.execute(); - String expected = Messages.EMPTY_LIST + System.lineSeparator(); - assertEquals(expected, outputStream.toString()); - } catch (EmptyListException e) { - throw new RuntimeException(e); - } + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + listCommand1.execute(); + String expected = ""; + assertEquals(expected, outputStream.toString()); } @Test public void listCommandTest_transactionList_empty() { ListCommand listCommand1 = new ListCommand(new ArrayList(), "NA"); - try { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outputStream)); - listCommand1.execute(); - String expected = Messages.EMPTY_LIST + System.lineSeparator(); - assertEquals(expected, outputStream.toString()); - } catch (EmptyListException e) { - throw new RuntimeException(e); - } + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + listCommand1.execute(); + String expected = ""; + assertEquals(expected, outputStream.toString()); } @Test public void listCommandTest_promotionList_empty() { ListCommand listCommand1 = new ListCommand(new ArrayList()); - try { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outputStream)); - listCommand1.execute(); - String expected = Messages.EMPTY_LIST + System.lineSeparator(); - assertEquals(expected, outputStream.toString()); - } catch (EmptyListException e) { - throw new RuntimeException(e); - } + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + listCommand1.execute(); + String expected = ""; + assertEquals(expected, outputStream.toString()); } } diff --git a/src/test/java/command/TotalProfitCommandTest.java b/src/test/java/command/TotalProfitCommandTest.java index 04585cbacf..78a17d37bf 100644 --- a/src/test/java/command/TotalProfitCommandTest.java +++ b/src/test/java/command/TotalProfitCommandTest.java @@ -1,5 +1,6 @@ package command; +import common.Messages; import exceptions.CommandFormatException; import itemlist.Cashier; import itemlist.Itemlist; @@ -30,8 +31,7 @@ public void testProfitWithNoItems() { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(outputStream)); totalProfitCommand.execute(); - String expected = "There are no transactions with your search query." + System.lineSeparator() + - "You have earned 0.0 in profits so far." + System.lineSeparator(); + String expected = Messages.NO_BESTSELLER + System.lineSeparator(); assertEquals(expected, outputStream.toString()); } @@ -58,8 +58,7 @@ public void testRevenueWithNoItems() { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(outputStream)); totalProfitCommand.execute(); - String expected = "There are no transactions with your search query." + System.lineSeparator() + - "You have earned 0.0 in revenue so far." + System.lineSeparator(); + String expected = Messages.NO_BESTSELLER + System.lineSeparator(); assertEquals(expected, outputStream.toString()); } diff --git a/src/test/java/parser/ParserTest.java b/src/test/java/parser/ParserTest.java index a841d826d3..e342e602d1 100644 --- a/src/test/java/parser/ParserTest.java +++ b/src/test/java/parser/ParserTest.java @@ -504,7 +504,7 @@ public void testParseListItemsCommand() { System.setOut(new PrintStream(outputStreamCaptor)); String userInput = "list_items"; parser.parseInput(userInput); - String expectedMessage = ""; + String expectedMessage = Messages.EMPTY_ITEM_LIST + System.lineSeparator(); assertEquals(expectedMessage, outputStreamCaptor.toString()); } @@ -514,17 +514,7 @@ public void testParseListPromotionsCommand() { System.setOut(new PrintStream(outputStreamCaptor)); String userInput = "list_promotions"; parser.parseInput(userInput); - String expectedMessage = ""; - assertEquals(expectedMessage, outputStreamCaptor.toString()); - } - - @Test - public void testParseListTransactionsCommand() { - ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outputStreamCaptor)); - String userInput = "list_transactions"; - parser.parseInput(userInput); - String expectedMessage = ""; + String expectedMessage = Messages.EMPTY_PROMOTION_LIST + System.lineSeparator(); assertEquals(expectedMessage, outputStreamCaptor.toString()); } diff --git a/src/test/java/ui/TextUiTest.java b/src/test/java/ui/TextUiTest.java index 3696b1a265..0b21425e76 100644 --- a/src/test/java/ui/TextUiTest.java +++ b/src/test/java/ui/TextUiTest.java @@ -9,20 +9,24 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertLinesMatch; -import static ui.TextUi.getUserInput; -import static ui.TextUi.showList; public class TextUiTest { - + @Test + public void testReplyToUser() { + String message = "Message 1 to display"; + ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStreamCaptor)); + TextUi.replyToUser(message); + assert outputStreamCaptor.toString().contains((message)); + } @Test public void testValidInput() { - provideInput("add Item"); - assertEquals("add Item", getUserInput()); + assertEquals("add Item", TextUi.getUserInput()); provideInput(" "); - assertEquals("Invalid Command", getUserInput()); + assertEquals("Invalid Command", TextUi.getUserInput()); } @Test @@ -32,7 +36,7 @@ public void testShowInventoryList() { ArrayList itemList = new ArrayList<>(); itemList.add("test 1"); itemList.add("test 2"); - showList(itemList); + TextUi.showList(itemList); String[] output = outputStreamCaptor.toString().split("\\r?\\n"); String line1 = "List: "; String line2 = "1. test 1"; @@ -47,7 +51,7 @@ public void testShowEmptyInventoryList() { //only test for empty arrayList a PrintStream originalOut = System.out; System.setOut(new PrintStream(outputStreamCaptor)); ArrayList itemList = new ArrayList<>(); //empty arrayList - showList(itemList); + TextUi.showList(itemList); String[] output = outputStreamCaptor.toString().split("\\r?\\n"); String line = "There is nothing here! Time to spend some money and stock em up!"; List expectedOutput = List.of(line);