diff --git a/src/main/java/budgetbuddy/BudgetBuddy.java b/src/main/java/budgetbuddy/BudgetBuddy.java index 79d57211bf..791f57eb56 100644 --- a/src/main/java/budgetbuddy/BudgetBuddy.java +++ b/src/main/java/budgetbuddy/BudgetBuddy.java @@ -1,9 +1,10 @@ package budgetbuddy; import budgetbuddy.account.Account; -import budgetbuddy.exception.EmptyArgumentException; -import budgetbuddy.exception.InvalidAddTransactionSyntax; -import budgetbuddy.exception.InvalidTransactionTypeException; +import budgetbuddy.exceptions.EmptyArgumentException; +import budgetbuddy.exceptions.InvalidAddTransactionSyntax; +import budgetbuddy.exceptions.InvalidIndexException; +import budgetbuddy.exceptions.InvalidTransactionTypeException; import budgetbuddy.transaction.TransactionList; import budgetbuddy.ui.UserInterface; @@ -31,10 +32,10 @@ public static void main(String[] args) { transactions.updateBalance(account); boolean isRunning = true; - try { - while (isRunning) { - String input = in.nextLine(); + while (isRunning) { + String input = in.nextLine(); + try { switch (input.split(" ")[0]) { case "bye": UserInterface.printGoodBye(); @@ -53,20 +54,23 @@ public static void main(String[] args) { UserInterface.printNoCommandExists(); } transactions.saveTransactionList(); + } catch (InvalidAddTransactionSyntax e) { + UserInterface.printInvalidAddSyntax(e.getMessage()); + } catch (NumberFormatException e) { + UserInterface.printNumberFormatError(e.getMessage()); + } catch (InvalidTransactionTypeException e) { + UserInterface.printTransactionTypeError(e.getMessage()); + } catch (EmptyArgumentException e) { + UserInterface.printEmptyArgumentError(e.getMessage()); + } catch (InvalidIndexException e) { + UserInterface.printInvalidIndex("Given index id is out of bound", + Integer.parseInt(e.getMessage())); + } catch (IndexOutOfBoundsException ignored){ + UserInterface.printInvalidInput("Please check your command syntax"); + } catch (Exception e) { + UserInterface.printUnknownError(e.getMessage()); } - } catch (InvalidAddTransactionSyntax e) { - UserInterface.printInvalidAddSyntax(e.getMessage()); - } catch (NumberFormatException e) { - UserInterface.printNumberFormatError(e.getMessage()); - } catch (InvalidTransactionTypeException e) { - UserInterface.printTransactionTypeError(e.getMessage()); - } catch(EmptyArgumentException e) { - UserInterface.printEmptyArgumentError(e.getMessage()); - } catch(IndexOutOfBoundsException e){ - UserInterface.printIndexOutOfBounds("Given index id is out of bound", - Integer.parseInt(e.getMessage())); - } catch (Exception e) { - UserInterface.printUnknownError(e.getMessage()); } + } } diff --git a/src/main/java/budgetbuddy/exception/EmptyArgumentException.java b/src/main/java/budgetbuddy/exceptions/EmptyArgumentException.java similarity index 81% rename from src/main/java/budgetbuddy/exception/EmptyArgumentException.java rename to src/main/java/budgetbuddy/exceptions/EmptyArgumentException.java index fc4770f0e0..9d2582a60c 100644 --- a/src/main/java/budgetbuddy/exception/EmptyArgumentException.java +++ b/src/main/java/budgetbuddy/exceptions/EmptyArgumentException.java @@ -1,4 +1,4 @@ -package budgetbuddy.exception; +package budgetbuddy.exceptions; public class EmptyArgumentException extends Exception { public EmptyArgumentException(String message) { diff --git a/src/main/java/budgetbuddy/exception/InvalidAddTransactionSyntax.java b/src/main/java/budgetbuddy/exceptions/InvalidAddTransactionSyntax.java similarity index 82% rename from src/main/java/budgetbuddy/exception/InvalidAddTransactionSyntax.java rename to src/main/java/budgetbuddy/exceptions/InvalidAddTransactionSyntax.java index 49d016c2a4..1c870a3211 100644 --- a/src/main/java/budgetbuddy/exception/InvalidAddTransactionSyntax.java +++ b/src/main/java/budgetbuddy/exceptions/InvalidAddTransactionSyntax.java @@ -1,4 +1,4 @@ -package budgetbuddy.exception; +package budgetbuddy.exceptions; public class InvalidAddTransactionSyntax extends Exception{ public InvalidAddTransactionSyntax(String message) { diff --git a/src/main/java/budgetbuddy/exceptions/InvalidIndexException.java b/src/main/java/budgetbuddy/exceptions/InvalidIndexException.java new file mode 100644 index 0000000000..fde96f40bd --- /dev/null +++ b/src/main/java/budgetbuddy/exceptions/InvalidIndexException.java @@ -0,0 +1,7 @@ +package budgetbuddy.exceptions; + +public class InvalidIndexException extends Exception { + public InvalidIndexException (String message) { + super(message); + } +} diff --git a/src/main/java/budgetbuddy/exception/InvalidTransactionTypeException.java b/src/main/java/budgetbuddy/exceptions/InvalidTransactionTypeException.java similarity index 83% rename from src/main/java/budgetbuddy/exception/InvalidTransactionTypeException.java rename to src/main/java/budgetbuddy/exceptions/InvalidTransactionTypeException.java index e3a01dd5b3..40f6960432 100644 --- a/src/main/java/budgetbuddy/exception/InvalidTransactionTypeException.java +++ b/src/main/java/budgetbuddy/exceptions/InvalidTransactionTypeException.java @@ -1,4 +1,4 @@ -package budgetbuddy.exception; +package budgetbuddy.exceptions; public class InvalidTransactionTypeException extends Exception{ public InvalidTransactionTypeException (String message) { diff --git a/src/main/java/budgetbuddy/parser/Parser.java b/src/main/java/budgetbuddy/parser/Parser.java index 6b75a99408..7ba488805e 100644 --- a/src/main/java/budgetbuddy/parser/Parser.java +++ b/src/main/java/budgetbuddy/parser/Parser.java @@ -1,7 +1,8 @@ package budgetbuddy.parser; import budgetbuddy.account.Account; -import budgetbuddy.exception.InvalidTransactionTypeException; +import budgetbuddy.exceptions.EmptyArgumentException; +import budgetbuddy.exceptions.InvalidTransactionTypeException; import budgetbuddy.transaction.TransactionList; import budgetbuddy.transaction.type.Expense; import budgetbuddy.transaction.type.Income; @@ -12,7 +13,7 @@ public class Parser { public static final int ADD_COMMAND_INDEX = 3; public Transaction parseTransaction(String input, Account account) - throws InvalidTransactionTypeException, NumberFormatException { + throws InvalidTransactionTypeException, NumberFormatException, EmptyArgumentException { String data = input.substring(ADD_COMMAND_INDEX + 1); String[] parseData = data.split("/"); String type = null; @@ -20,7 +21,7 @@ public Transaction parseTransaction(String input, Account account) String date = null; String amount = null; String category = null; - for(int i = 0; i < parseData.length; i++) { + for(int i = 0; i < parseData.length-1; i++) { switch (parseData[i].trim()) { case "t": type = parseData[i + 1].trim(); @@ -47,7 +48,9 @@ public Transaction parseTransaction(String input, Account account) } assert amount != null; assert type != null; - if (type.equalsIgnoreCase("income")) { + if(description.trim().isEmpty() || category.trim().isEmpty() || type.trim().isEmpty()){ + throw new EmptyArgumentException("data for the arguments "); + } else if (type.equalsIgnoreCase("income")) { return new Income(description, Double.parseDouble(amount), category, date, account); } else if (type.equalsIgnoreCase("expense")) { return new Expense(description, Double.parseDouble(amount), category, date, account); diff --git a/src/main/java/budgetbuddy/transaction/TransactionList.java b/src/main/java/budgetbuddy/transaction/TransactionList.java index 2e0ad9cab4..0e1d35f5df 100644 --- a/src/main/java/budgetbuddy/transaction/TransactionList.java +++ b/src/main/java/budgetbuddy/transaction/TransactionList.java @@ -1,9 +1,10 @@ package budgetbuddy.transaction; import budgetbuddy.account.Account; -import budgetbuddy.exception.EmptyArgumentException; -import budgetbuddy.exception.InvalidAddTransactionSyntax; -import budgetbuddy.exception.InvalidTransactionTypeException; +import budgetbuddy.exceptions.EmptyArgumentException; +import budgetbuddy.exceptions.InvalidAddTransactionSyntax; +import budgetbuddy.exceptions.InvalidIndexException; +import budgetbuddy.exceptions.InvalidTransactionTypeException; import budgetbuddy.parser.Parser; import budgetbuddy.storage.DataStorage; import budgetbuddy.transaction.type.Transaction; @@ -38,7 +39,8 @@ public void printTransactions(Account account){ UserInterface.printAllTransactions(transactions, account.getBalance()); } - public void removeTransaction(String input, Account account) throws EmptyArgumentException, NumberFormatException { + public void removeTransaction(String input, Account account) throws EmptyArgumentException, + NumberFormatException, InvalidIndexException { if (input.trim().length() < DELETE_BEGIN_INDEX) { throw new EmptyArgumentException("delete index"); } @@ -56,7 +58,7 @@ public void removeTransaction(String input, Account account) throws EmptyArgumen assert transactions.size() == size - 1 : "Transaction list size did not decrease after removal"; UserInterface.printDeleteMessage(itemRemoved, account.getBalance()); } else { - throw new IndexOutOfBoundsException(size); + throw new InvalidIndexException(String.valueOf(size)); } } @@ -83,7 +85,7 @@ void addTransaction(Transaction t) { } public void processTransaction(String input, Account account) - throws InvalidTransactionTypeException, InvalidAddTransactionSyntax { + throws InvalidTransactionTypeException, InvalidAddTransactionSyntax, EmptyArgumentException { // Check for syntax for add transaction String[] arguments = {"/t/", "/n/", "/$/", "/d/", "/c/"}; for (String argument : arguments) { diff --git a/src/main/java/budgetbuddy/ui/UserInterface.java b/src/main/java/budgetbuddy/ui/UserInterface.java index b6b4b28524..342735d31f 100644 --- a/src/main/java/budgetbuddy/ui/UserInterface.java +++ b/src/main/java/budgetbuddy/ui/UserInterface.java @@ -39,10 +39,10 @@ public static void printAddMessage(String transaction, double balance){ System.out.println(LINE); } - public static void printIndexOutOfBounds(String message,int id){ + public static void printInvalidIndex(String message, int id){ System.out.println(LINE); System.out.println(TAB_SPACE + message); - System.out.println( TAB_SPACE + "Please use index within the range of: 0 to " + id); + System.out.println( TAB_SPACE + "Please use index within the range of: 1 to " + id); System.out.println(LINE); } @@ -52,6 +52,12 @@ public static void printUnknownError(String message){ System.out.println(LINE); } + public static void printInvalidInput(String message){ + System.out.println(LINE); + System.out.println(TAB_SPACE + "Error occurred with message: " + message); + System.out.println(LINE); + } + public static void printInvalidAddSyntax(String message) { System.out.println(LINE); System.out.println(TAB_SPACE + message); diff --git a/src/test/java/budgetbuddy/parser/ParserTest.java b/src/test/java/budgetbuddy/parser/ParserTest.java index 7cd2ec0845..95831bd2fe 100644 --- a/src/test/java/budgetbuddy/parser/ParserTest.java +++ b/src/test/java/budgetbuddy/parser/ParserTest.java @@ -1,6 +1,7 @@ package budgetbuddy.parser; import budgetbuddy.account.Account; -import budgetbuddy.exception.InvalidTransactionTypeException; +import budgetbuddy.exceptions.EmptyArgumentException; +import budgetbuddy.exceptions.InvalidTransactionTypeException; import org.junit.jupiter.api.Test; import budgetbuddy.transaction.type.Transaction; @@ -12,7 +13,7 @@ public class ParserTest { @Test - public void testParseTransaction() throws InvalidTransactionTypeException { + public void testParseTransaction() throws InvalidTransactionTypeException, EmptyArgumentException { Parser parser = new Parser(); Account account = new Account(); Transaction transaction = parser.parseTransaction("add /t/Income /n/Shopping /$/50 /d/14-03-2024 " + diff --git a/src/test/java/budgetbuddy/transaction/TransactionListTest.java b/src/test/java/budgetbuddy/transaction/TransactionListTest.java index 83e3a5b9af..337203989b 100644 --- a/src/test/java/budgetbuddy/transaction/TransactionListTest.java +++ b/src/test/java/budgetbuddy/transaction/TransactionListTest.java @@ -1,9 +1,10 @@ package budgetbuddy.transaction; import budgetbuddy.account.Account; -import budgetbuddy.exception.EmptyArgumentException; -import budgetbuddy.exception.InvalidAddTransactionSyntax; -import budgetbuddy.exception.InvalidTransactionTypeException; +import budgetbuddy.exceptions.EmptyArgumentException; +import budgetbuddy.exceptions.InvalidAddTransactionSyntax; +import budgetbuddy.exceptions.InvalidIndexException; +import budgetbuddy.exceptions.InvalidTransactionTypeException; import budgetbuddy.transaction.type.Income; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.BeforeEach; @@ -32,7 +33,7 @@ public void getTransactions_initiallyEmpty() { @Test public void processTransaction_addsTransaction() - throws InvalidTransactionTypeException, InvalidAddTransactionSyntax { + throws InvalidTransactionTypeException, InvalidAddTransactionSyntax, EmptyArgumentException { Transaction testTransaction = new Income("Test", 200,"Personal", "14-03-2024", account); transactionList.processTransaction("add /t/Income /n/Test /$/200 /d/14-03-2024 /c/Personal", account); @@ -67,7 +68,7 @@ public void processTransaction_withInvalidTransactionType_throwsTransactionTypeE } @Test - public void removeTransaction_removesCorrectTransaction() throws EmptyArgumentException { + public void removeTransaction_removesCorrectTransaction() throws EmptyArgumentException, InvalidIndexException { Transaction testTransaction1 = new Income("Test1", 100, "Category1", "14-03-2024", account); Transaction testTransaction2 = new Income("Test2", 200, "Category2", @@ -87,7 +88,7 @@ public void removeTransaction_withInvalidIndex_throwsIndexOutOfBoundsException() "14-03-2024", account); transactionList.addTransaction(testTransaction); - assertThrows(IndexOutOfBoundsException.class, () -> transactionList.removeTransaction( + assertThrows(InvalidIndexException.class, () -> transactionList.removeTransaction( "delete 2", account)); }