diff --git a/savedWallet.txt b/savedWallet.txt index f2f224a583..ba66466c2a 100644 --- a/savedWallet.txt +++ b/savedWallet.txt @@ -1,8 +1 @@ - -1000.0 -out $250.0 /date 2019-11-07 /tags -out $100.0 /date 2019-11-07 /tags -in $100.0 /date 2019-11-07 /tags -out $5.0 /date 2019-11-07 /tags food -out $-0.0 /date 2019-11-07 /tags - +0.0 diff --git a/src/main/java/executor/command/CommandDateList.java b/src/main/java/executor/command/CommandDateList.java index 180713406f..51268b26f3 100644 --- a/src/main/java/executor/command/CommandDateList.java +++ b/src/main/java/executor/command/CommandDateList.java @@ -3,13 +3,16 @@ import duke.exception.DukeException; import interpreter.Parser; import storage.StorageManager; -import java.time.format.DateTimeParseException; +import java.text.ParseException; +import java.text.SimpleDateFormat; + public class CommandDateList extends Command { private String date; /** * Constructor for CommandDateList subCommand Class. + * * @param userInput String is the user input from the CLI */ public CommandDateList(String userInput) { @@ -24,6 +27,11 @@ public CommandDateList(String userInput) { @Override public void execute(StorageManager storageManager) { String outputStr = "You have the following receipts for" + " " + date + "\n"; + if (!isDate(this.date)) { + this.infoCapsule.setCodeError(); + this.infoCapsule.setOutputStr("Invalid date input. FORMAT : datelist yyyy-mm-dd"); + return; + } try { outputStr += storageManager.getReceiptsByDate(this.date).getPrintableReceipts(); } catch (DukeException e) { @@ -34,5 +42,20 @@ public void execute(StorageManager storageManager) { this.infoCapsule.setCodeCli(); this.infoCapsule.setOutputStr(outputStr); } + + /** + * Boolean function for checking date format of input. + * + * @param dateString String is date input from the CLI + */ + public boolean isDate(String dateString) { + try { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd"); + return dateFormat.parse(dateString) != null; + } catch (ParseException e) { + return false; + } + } + } diff --git a/src/main/java/executor/command/CommandDelete.java b/src/main/java/executor/command/CommandDelete.java index 1d42b1ec15..ff8d63c902 100644 --- a/src/main/java/executor/command/CommandDelete.java +++ b/src/main/java/executor/command/CommandDelete.java @@ -37,4 +37,4 @@ public void execute(StorageManager storageManager) { this.infoCapsule.setCodeToast(); this.infoCapsule.setOutputStr(outputStr); } -} \ No newline at end of file +} diff --git a/src/main/java/executor/command/CommandReceiptDelete.java b/src/main/java/executor/command/CommandDeleteReceipt.java similarity index 72% rename from src/main/java/executor/command/CommandReceiptDelete.java rename to src/main/java/executor/command/CommandDeleteReceipt.java index 778decc9be..5621f7f8bc 100644 --- a/src/main/java/executor/command/CommandReceiptDelete.java +++ b/src/main/java/executor/command/CommandDeleteReceipt.java @@ -4,19 +4,21 @@ import interpreter.Parser; import storage.StorageManager; -public class CommandReceiptDelete extends Command { +import java.lang.reflect.InvocationTargetException; + +public class CommandDeleteReceipt extends Command { protected String userInput; /** * Constructor for CommandDeleteReceipt subCommand Class. * @param userInput The user input from the CLI */ - public CommandReceiptDelete(String userInput) { + public CommandDeleteReceipt(String userInput) { super(); this.userInput = userInput; this.description = "Deletes the specific entry that the user wants to remove. \n" - + "FORMAT: receiptdelete "; - this.commandType = CommandType.RECEIPTDELETE; + + "FORMAT: deletereceipt "; + this.commandType = CommandType.DELETERECEIPT; } @@ -26,7 +28,7 @@ public void execute(StorageManager storageManager) { String integer = Parser.parseForPrimaryInput(this.commandType, userInput); if (integer.isEmpty()) { this.infoCapsule.setCodeError(); - this.infoCapsule.setOutputStr("Index input is missing. FORMAT : receiptdelete "); + this.infoCapsule.setOutputStr("Index input is missing. FORMAT : deletereceipt "); return; } try { @@ -41,8 +43,14 @@ public void execute(StorageManager storageManager) { this.infoCapsule.setCodeError(); this.infoCapsule.setOutputStr(e.getMessage()); return; + } catch (NumberFormatException e) { + this.infoCapsule.setCodeError(); + this.infoCapsule.setOutputStr("Invalid index input. Please enter an integer"); + return; } this.infoCapsule.setCodeToast(); this.infoCapsule.setOutputStr(outputStr); } + + } \ No newline at end of file diff --git a/src/main/java/executor/command/CommandPercent.java b/src/main/java/executor/command/CommandPercent.java index dbab2beded..963ad292ad 100644 --- a/src/main/java/executor/command/CommandPercent.java +++ b/src/main/java/executor/command/CommandPercent.java @@ -29,6 +29,11 @@ public void execute(StorageManager storageManager) { this.infoCapsule.setOutputStr("Tag input is missing. FORMAT : percent "); return; } + if (storageManager.getWallet().getReceipts().getPrintableReceipts().isEmpty()) { + this.infoCapsule.setCodeError(); + this.infoCapsule.setOutputStr("No receipts found in the list"); + return; + } try { Double totalTag = storageManager.getReceiptsByTag(this.tag).getTotalCashSpent(); Double totalSpent = storageManager.getWalletExpenses(); diff --git a/src/main/java/executor/command/CommandType.java b/src/main/java/executor/command/CommandType.java index 5d5a96c847..10c153019e 100644 --- a/src/main/java/executor/command/CommandType.java +++ b/src/main/java/executor/command/CommandType.java @@ -7,7 +7,7 @@ public enum CommandType { BLANK(CommandBlank.class), FIND(CommandFind.class), DELETE(CommandDelete.class), - RECEIPTDELETE(CommandReceiptDelete.class), + DELETERECEIPT(CommandDeleteReceipt.class), DONE(CommandMarkDone.class), QUEUE(CommandQueue.class), VIEWSCHEDULE(CommandSchedule.class), diff --git a/src/test/java/CommandDateListTest.java b/src/test/java/CommandDateListTest.java index 38ba932233..bb2684cd00 100644 --- a/src/test/java/CommandDateListTest.java +++ b/src/test/java/CommandDateListTest.java @@ -20,9 +20,13 @@ void execute() { CommandDateList dateOne = new CommandDateList("datelist 2019-02-01"); dateOne.execute(storageManager); String output = dateOne.getInfoCapsule().getOutputStr(); - assertEquals("You have the following receipts for 2019-02-01\n1. [transport] 3.0 2019-02-01\n", output); + CommandDateList d1 = new CommandDateList("datelist 123cwv"); + d1.execute(storageManager); + String result = d1.getInfoCapsule().getOutputStr(); + assertEquals("Invalid date input. FORMAT : datelist yyyy-mm-dd", result); + } } diff --git a/src/test/java/CommandReceiptDeleteTest.java b/src/test/java/CommandDeleteReceiptTest.java similarity index 55% rename from src/test/java/CommandReceiptDeleteTest.java rename to src/test/java/CommandDeleteReceiptTest.java index 65e561b418..cd155348ae 100644 --- a/src/test/java/CommandReceiptDeleteTest.java +++ b/src/test/java/CommandDeleteReceiptTest.java @@ -1,5 +1,6 @@ +import executor.command.CommandDelete; +import executor.command.CommandDeleteReceipt; import executor.command.CommandPercent; -import executor.command.CommandReceiptDelete; import org.junit.jupiter.api.Test; import storage.StorageManager; import ui.Receipt; @@ -8,7 +9,7 @@ import java.time.LocalDate; -public class CommandReceiptDeleteTest { +public class CommandDeleteReceiptTest { @Test void execute() { StorageManager storageManager = new StorageManager(); @@ -18,15 +19,20 @@ void execute() { receiptOne.setDate(LocalDate.parse("2019-02-01")); storageManager.getWallet().addReceipt(receiptOne); - CommandReceiptDelete d1 = new CommandReceiptDelete("receiptdelete 1"); + CommandDeleteReceipt d1 = new CommandDeleteReceipt("deletereceipt 1"); d1.execute(storageManager); String output = d1.getInfoCapsule().getOutputStr(); assertEquals("Receipt 1 has been deleted\n", output); - CommandReceiptDelete d2 = new CommandReceiptDelete("receiptdelete"); + CommandDeleteReceipt d2 = new CommandDeleteReceipt("deletereceipt"); d2.execute(storageManager); String result = d2.getInfoCapsule().getOutputStr(); - assertEquals("Index input is missing. FORMAT : receiptdelete ", result); + assertEquals("Index input is missing. FORMAT : deletereceipt ", result); + + CommandDeleteReceipt d3 = new CommandDeleteReceipt("deletereceipt 2.0f"); + d3.execute(storageManager); + String out = d3.getInfoCapsule().getOutputStr(); + assertEquals("Invalid index input. Please enter an integer", out); } diff --git a/src/test/java/CommandPercentTest.java b/src/test/java/CommandPercentTest.java index f04f282ce9..53c364b631 100644 --- a/src/test/java/CommandPercentTest.java +++ b/src/test/java/CommandPercentTest.java @@ -32,6 +32,7 @@ void execute() { percentTwo.execute(storageManager); String result = percentTwo.getInfoCapsule().getOutputStr(); assertEquals("Tag input is missing. FORMAT : percent ", result); + } } diff --git a/src/test/java/CommandTagListTest.java b/src/test/java/CommandTagListTest.java index caceecb2c3..9f0d11d856 100644 --- a/src/test/java/CommandTagListTest.java +++ b/src/test/java/CommandTagListTest.java @@ -20,8 +20,12 @@ void execute() { CommandTagList dateOne = new CommandTagList("taglist food"); dateOne.execute(storageManager); String output = dateOne.getInfoCapsule().getOutputStr(); - assertEquals("You spent a total of $3.00 on food\n" + "1. [food] 3.0 2019-02-01\n\n", output); + + CommandTagList d1 = new CommandTagList("taglist"); + d1.execute(storageManager); + String result = d1.getInfoCapsule().getOutputStr(); + assertEquals("Tag input is missing. FORMAT : taglist ", result); } }