Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle negative amount for add transaction #135

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/main/java/budgetbuddy/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import budgetbuddy.categories.Category;

import budgetbuddy.exceptions.EmptyArgumentException;
import budgetbuddy.exceptions.InvalidAddTransactionSyntax;
import budgetbuddy.exceptions.InvalidArgumentSyntaxException;
import budgetbuddy.exceptions.InvalidCategoryException;
import budgetbuddy.exceptions.InvalidEditTransactionData;
Expand Down Expand Up @@ -33,7 +34,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 +79,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
3 changes: 2 additions & 1 deletion src/test/java/budgetbuddy/parser/ParserTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package budgetbuddy.parser;
import budgetbuddy.account.Account;
import budgetbuddy.exceptions.EmptyArgumentException;
import budgetbuddy.exceptions.InvalidAddTransactionSyntax;
import budgetbuddy.exceptions.InvalidCategoryException;
import budgetbuddy.exceptions.InvalidEditTransactionData;
import budgetbuddy.exceptions.InvalidTransactionTypeException;
Expand All @@ -16,7 +17,7 @@ public class ParserTest {

@Test
public void testParseTransaction() throws InvalidTransactionTypeException, EmptyArgumentException,
InvalidCategoryException {
InvalidCategoryException, InvalidAddTransactionSyntax {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good handling of exceptions

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
Loading