Skip to content

Commit

Permalink
Throw exception when negative amount
Browse files Browse the repository at this point in the history
  • Loading branch information
ShyamKrishna33 committed Apr 10, 2024
1 parent 5249a66 commit f9c1ce4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
11 changes: 5 additions & 6 deletions src/main/java/budgetbuddy/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import budgetbuddy.account.Account;
import budgetbuddy.categories.Category;

import budgetbuddy.exceptions.EmptyArgumentException;
import budgetbuddy.exceptions.InvalidArgumentSyntaxException;
import budgetbuddy.exceptions.InvalidCategoryException;
import budgetbuddy.exceptions.InvalidEditTransactionData;
import budgetbuddy.exceptions.InvalidTransactionTypeException;
import budgetbuddy.exceptions.*;
import budgetbuddy.transaction.TransactionList;
import budgetbuddy.transaction.type.Expense;
import budgetbuddy.transaction.type.Income;
Expand All @@ -33,7 +29,7 @@ public static int parseAccountNumber(String input) throws InvalidArgumentSyntaxE

public Transaction parseUserInputToTransaction(String input, Account account)
throws InvalidTransactionTypeException, NumberFormatException,
EmptyArgumentException, InvalidCategoryException {
EmptyArgumentException, InvalidCategoryException, InvalidAddTransactionSyntax {
String data = input.substring(ADD_COMMAND_INDEX + 1);
String[] parseData = data.split("/");
String type = null;
Expand Down Expand Up @@ -78,6 +74,9 @@ public Transaction parseUserInputToTransaction(String input, Account account)
throw new InvalidCategoryException("Category Index out of bounds");
}

if (Double.parseDouble(amount) < 0) {
throw new InvalidAddTransactionSyntax("Amount cannot be negative");
}

if (description.trim().isEmpty() || type.trim().isEmpty()) {
throw new EmptyArgumentException("data for the arguments ");
Expand Down
7 changes: 2 additions & 5 deletions src/test/java/budgetbuddy/parser/ParserTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package budgetbuddy.parser;
import budgetbuddy.account.Account;
import budgetbuddy.exceptions.EmptyArgumentException;
import budgetbuddy.exceptions.InvalidCategoryException;
import budgetbuddy.exceptions.InvalidEditTransactionData;
import budgetbuddy.exceptions.InvalidTransactionTypeException;
import budgetbuddy.exceptions.*;
import org.junit.jupiter.api.Test;
import budgetbuddy.transaction.type.Transaction;

Expand All @@ -16,7 +13,7 @@ public class ParserTest {

@Test
public void testParseTransaction() throws InvalidTransactionTypeException, EmptyArgumentException,
InvalidCategoryException {
InvalidCategoryException, InvalidAddTransactionSyntax {
Parser parser = new Parser();
Account account = new Account(1);
String input = "add /t/Income /n/Shopping /$/50 /d/14-03-2024 /c/1";
Expand Down

0 comments on commit f9c1ce4

Please sign in to comment.