Skip to content

Commit

Permalink
Merge pull request AY2425S1-CS2113-W14-4#81 from yijiano/fix/Exceptions
Browse files Browse the repository at this point in the history
Fix loadData exception handling, Parser exceptions, and help command …
  • Loading branch information
yijiano authored Oct 15, 2024
2 parents d638f58 + 25efc5e commit 02ce799
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/main/java/seedu/pill/Pill.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/pill/command/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <command>' 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 <command> -v' for verbose output with examples.");
}

/**
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/seedu/pill/exceptions/ExceptionMessages.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package seedu.pill.exceptions;

public enum ExceptionMessages {
INVALID_COMMAND ("Invalid command, please try again."),
INVALID_COMMAND ("Invalid command, please try again." +
"\nType the 'help' command for a list of valid commands."),
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] <name> e.g. delete JackDaniels" +
"\n- [command] <name> <quantity> e.g. add JackDaniels 1231");

private final String message;

Expand Down
17 changes: 10 additions & 7 deletions src/main/java/seedu/pill/util/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("\\s+");
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;
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/seedu/pill/util/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,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();
Expand All @@ -97,12 +96,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;
}
Expand Down

0 comments on commit 02ce799

Please sign in to comment.