diff --git a/src/main/java/seedu/pill/Pill.java b/src/main/java/seedu/pill/Pill.java index 924b7ae47c..d0ae66a1fb 100644 --- a/src/main/java/seedu/pill/Pill.java +++ b/src/main/java/seedu/pill/Pill.java @@ -16,9 +16,9 @@ public final class Pill { /** * Runs the main loop of the Pill chatbot. */ - public void run() throws PillException { - Printer.printInitMessage(); + public void run() { items = storage.loadData(); + Printer.printInitMessage(); parser = new Parser(items, storage); while (!parser.getExitFlag()) { String line = ui.getInput(); diff --git a/src/main/java/seedu/pill/command/HelpCommand.java b/src/main/java/seedu/pill/command/HelpCommand.java index 12e18f7bfd..5cc97f99e6 100644 --- a/src/main/java/seedu/pill/command/HelpCommand.java +++ b/src/main/java/seedu/pill/command/HelpCommand.java @@ -55,7 +55,7 @@ private void showGeneralHelp() { System.out.println(" list - Lists all items"); System.out.println(" exit - Exits the program"); System.out.println("Type 'help ' for more information on a specific command."); - System.out.println("Add '-v' after the command for verbose output with examples."); + System.out.println("Type 'help -v' for verbose output with examples."); } /** diff --git a/src/main/java/seedu/pill/exceptions/ExceptionMessages.java b/src/main/java/seedu/pill/exceptions/ExceptionMessages.java index 25beba0098..ae9d2e5106 100644 --- a/src/main/java/seedu/pill/exceptions/ExceptionMessages.java +++ b/src/main/java/seedu/pill/exceptions/ExceptionMessages.java @@ -4,9 +4,13 @@ public enum ExceptionMessages { INVALID_COMMAND ("Invalid command, please try again."), SAVE_ERROR ("Error saving to file, please try again."), LOAD_ERROR ("Error loading saved data"), - INVALID_LINE_FORMAT ("Invalid line format, file corrupted"), + INVALID_LINE_FORMAT ("File corrupted. Ignoring invalid line format..."), INVALID_QUANTITY ("Quantity is invalid, please try again"), - INVALID_QUANTITY_FORMAT ("Quantity provided is not a number, please try again."); + INVALID_QUANTITY_FORMAT ("Quantity provided is not a number, please try again."), + TOO_MANY_ARGUMENTS ("Too many arguments. Accepted command formats are as follows:" + + "\n- [command] e.g. help" + + "\n- [command] e.g. delete JackDaniels" + + "\n- [command] e.g. add JackDaniels 1231"); private final String message; diff --git a/src/main/java/seedu/pill/util/Parser.java b/src/main/java/seedu/pill/util/Parser.java index 1214bad4e9..9747ce7c6b 100644 --- a/src/main/java/seedu/pill/util/Parser.java +++ b/src/main/java/seedu/pill/util/Parser.java @@ -27,15 +27,18 @@ public Parser(ItemMap items, Storage storage) { * Processes the user's command. * * @param input The user's input command from the scanner. - * @throws PillException If command is invalid */ - public void parseCommand(String input) throws PillException { - String[] splitInput = input.split(" ", 3); - String commandString = splitInput[0].toLowerCase(); - String argument = splitInput.length > 1 ? splitInput[1].toLowerCase() : null; - String quantityStr = splitInput.length > 2 ? splitInput[2] : "1"; // default quantity is 1 - + public void parseCommand(String input) { try { + String[] splitInput = input.split(" "); + if (splitInput.length > 3) { + throw new PillException(ExceptionMessages.TOO_MANY_ARGUMENTS); + } + assert(splitInput.length <= 3); + String commandString = splitInput[0].toLowerCase(); + String argument = splitInput.length > 1 ? splitInput[1].toLowerCase() : null; + String quantityStr = splitInput.length > 2 ? splitInput[2] : "1"; // default quantity is 1 + switch (commandString) { case "exit": this.exitFlag = true; diff --git a/src/main/java/seedu/pill/util/Storage.java b/src/main/java/seedu/pill/util/Storage.java index 3ef2e8e46c..1bdd6de886 100644 --- a/src/main/java/seedu/pill/util/Storage.java +++ b/src/main/java/seedu/pill/util/Storage.java @@ -4,6 +4,7 @@ import seedu.pill.exceptions.PillException; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; @@ -84,9 +85,8 @@ public void saveItem(Item item) throws PillException { * Loads saved CSV data into an ItemMap * * @return The ItemMap containing saved items - * @throws PillException if an error occurs during the loading process */ - public ItemMap loadData() throws PillException { + public ItemMap loadData() { ItemMap loadedItems = new ItemMap(); try { File file = initializeFile(); @@ -97,12 +97,12 @@ public ItemMap loadData() throws PillException { Item item = loadLine(line); loadedItems.addItemSilent(item.getName(), item.getQuantity()); } catch (PillException e) { - throw new PillException(ExceptionMessages.LOAD_ERROR); + PillException.printException(e); } } scanner.close(); } catch (IOException e) { - throw new PillException(ExceptionMessages.LOAD_ERROR); + throw new RuntimeException(e); } return loadedItems; }