Skip to content

Commit

Permalink
Merge pull request #20 from vibes-863/add-assertions
Browse files Browse the repository at this point in the history
Add assertions to Income and Expense class
  • Loading branch information
Vavinan committed Mar 20, 2024
2 parents 57d0951 + fd96185 commit 9d17d25
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 15 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ checkstyle {
}

run{
// enableAssertions = true
standardInput = System.in
}
5 changes: 1 addition & 4 deletions src/main/java/budgetbuddy/BudgetBuddy.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ public class BudgetBuddy {
public static void main(String[] args) {
String logo = "BUDGET BUDDY";
System.out.println("Hello from\n" + logo);
System.out.println("What is your name?");

System.out.println("What can I do for you?");
Scanner in = new Scanner(System.in);
System.out.println("Hello " + in.nextLine());


TransactionList transactions = null;
try {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/budgetbuddy/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public Transaction parseTransaction(String input, Account account)
description = parseData[i + 1].trim();
break;
case "$":
// Checks that input is an Integer
if (!TransactionList.isInteger(parseData[i+1].trim())) {
if (TransactionList.isNotDouble(parseData[i + 1].trim())) {
throw new NumberFormatException(parseData[i+1].trim());
} else {
amount = parseData[i + 1].trim();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/budgetbuddy/storage/DataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private Transaction processData(String s, Account account) {
return new Income(transactionInfo[0], Double.parseDouble(transactionInfo[4]),
transactionInfo[1], transactionInfo[3], account);
case "Expense":
return new Expense(transactionInfo[0], Double.parseDouble(transactionInfo[4]),
return new Expense(transactionInfo[0], -Double.parseDouble(transactionInfo[4]),
transactionInfo[1], transactionInfo[3], account);
default:
return null;
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/budgetbuddy/transaction/TransactionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void removeTransaction(String input, Account account) throws EmptyArgumen
throw new EmptyArgumentException("delete index");
}
String data = input.substring(DELETE_BEGIN_INDEX).trim();
if (!isInteger(data)) {
if (isNotInteger(data)) {
throw new NumberFormatException(data);
}
int id = Integer.parseInt(data) - INDEX_OFFSET;
Expand All @@ -60,13 +60,21 @@ public void removeTransaction(String input, Account account) throws EmptyArgumen
}
}

// Checks whether the input index is an Integer
public static boolean isInteger(String data) {
public static boolean isNotInteger(String data) {
try {
Integer.parseInt(data);
return true;
return false;
} catch (NumberFormatException e) {
return true;
}
}

public static boolean isNotDouble(String data) {
try {
Double.parseDouble(data);
return false;
} catch (NumberFormatException e) {
return true;
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/budgetbuddy/transaction/type/Expense.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ public class Expense extends Transaction {

public Expense(String description, double amount, String category, String date, Account account) {
super(description, -amount, category, date);
assert this.getAmount() < 0: "Expense amount must be positive";
assert description != null && !description.isEmpty() : "Description cannot be null or empty";
assert category != null && !category.isEmpty() : "Category cannot be null or empty";
assert date != null : "Date cannot be null";
assert account != null : "Account cannot be null";

account.setBalance(account.getBalance() + this.getAmount());
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/budgetbuddy/transaction/type/Income.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ public class Income extends Transaction {

public Income(String description, double amount, String category, String date, Account account) {
super(description, amount, category, date);
assert this.getAmount() > 0: "Income amount must be positive";
assert description != null && !description.isEmpty() : "Description cannot be null or empty";
assert category != null && !category.isEmpty() : "Category cannot be null or empty";
assert date != null : "Date cannot be null";
assert account != null : "Account cannot be null";

account.setBalance(account.getBalance() + this.getAmount());
}

Expand Down
3 changes: 1 addition & 2 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Hello from
BUDGET BUDDY
What is your name?
Hello James Gosling
What can I do for you?
----------------------------------------------------------------------------------------------------------------------------------------
Bye... Don't forget to keep track of your future transactions
----------------------------------------------------------------------------------------------------------------------------------------
3 changes: 1 addition & 2 deletions text-ui-test/input.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
James Gosling
bye
bye

0 comments on commit 9d17d25

Please sign in to comment.