Skip to content

Commit

Permalink
Merge branch 'master' into add-data-storage
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/budgetbuddy/BudgetBuddy.java
  • Loading branch information
ShyamKrishna33 committed Mar 20, 2024
2 parents c208a4a + 463cb08 commit 5053607
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 12 deletions.
13 changes: 12 additions & 1 deletion src/main/java/budgetbuddy/BudgetBuddy.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package budgetbuddy;

import budgetbuddy.account.Account;
import budgetbuddy.exception.EmptyArgumentException;
import budgetbuddy.exception.InvalidAddTransactionSyntax;
import budgetbuddy.exception.InvalidTransactionTypeException;
import budgetbuddy.transaction.TransactionList;
import budgetbuddy.ui.UserInterface;

Expand Down Expand Up @@ -54,7 +57,15 @@ public static void main(String[] args) {
}
transactions.saveTransactionList();
}
} catch (IndexOutOfBoundsException e) {
} 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package budgetbuddy.exception;

public class EmptyArgumentException extends Exception {
public EmptyArgumentException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package budgetbuddy.exception;

public class InvalidAddTransactionSyntax extends Exception{
public InvalidAddTransactionSyntax(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package budgetbuddy.exception;

public class InvalidTransactionTypeException extends Exception{
public InvalidTransactionTypeException (String message) {
super(message);
}
}
20 changes: 15 additions & 5 deletions src/main/java/budgetbuddy/parser/Parser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package budgetbuddy.parser;

import budgetbuddy.account.Account;
import budgetbuddy.exception.InvalidTransactionTypeException;
import budgetbuddy.transaction.TransactionList;
import budgetbuddy.transaction.type.Expense;
import budgetbuddy.transaction.type.Income;
import budgetbuddy.transaction.type.Transaction;
Expand All @@ -9,7 +11,8 @@ public class Parser {

public static final int ADD_COMMAND_INDEX = 3;

public Transaction parseTransaction(String input, Account account) {
public Transaction parseTransaction(String input, Account account)
throws InvalidTransactionTypeException, NumberFormatException {
String data = input.substring(ADD_COMMAND_INDEX + 1);
String[] parseData = data.split("/");
String type = null;
Expand All @@ -26,8 +29,13 @@ public Transaction parseTransaction(String input, Account account) {
description = parseData[i + 1].trim();
break;
case "$":
amount = parseData[i + 1].trim();
break;
// Checks that input is an Integer
if (!TransactionList.isInteger(parseData[i+1].trim())) {
throw new NumberFormatException(parseData[i+1].trim());
} else {
amount = parseData[i + 1].trim();
break;
}
case "d":
date = parseData[i + 1].trim();
break;
Expand All @@ -39,11 +47,13 @@ public Transaction parseTransaction(String input, Account account) {
}
}
assert amount != null;
assert type != null;
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);
return new Expense(description, Double.parseDouble(amount), category, date, account);
} else {
throw new InvalidTransactionTypeException(type);
}
return null;
}
}
35 changes: 32 additions & 3 deletions src/main/java/budgetbuddy/transaction/TransactionList.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package budgetbuddy.transaction;

import budgetbuddy.account.Account;
import budgetbuddy.exception.EmptyArgumentException;
import budgetbuddy.exception.InvalidAddTransactionSyntax;
import budgetbuddy.exception.InvalidTransactionTypeException;
import budgetbuddy.parser.Parser;
import budgetbuddy.storage.DataStorage;
import budgetbuddy.transaction.type.Transaction;
Expand Down Expand Up @@ -34,8 +37,15 @@ public void printTransactions(Account account){
UserInterface.printAllTransactions(transactions, account.getBalance());
}

public void removeTransaction(String input, Account account){
int id = Integer.parseInt(input.substring(DELETE_BEGIN_INDEX).trim()) - INDEX_OFFSET;
public void removeTransaction(String input, Account account) throws EmptyArgumentException, NumberFormatException {
if (input.trim().length() < DELETE_BEGIN_INDEX) {
throw new EmptyArgumentException("delete index");
}
String data = input.substring(DELETE_BEGIN_INDEX).trim();
if (!isInteger(data)) {
throw new NumberFormatException(data);
}
int id = Integer.parseInt(data) - INDEX_OFFSET;
int size = transactions.size();
if (id >= LOWER_BOUND && id < size) {
String itemRemoved = transactions.get(id).toString();
Expand All @@ -46,12 +56,31 @@ public void removeTransaction(String input, Account account){
throw new IndexOutOfBoundsException(size);
}
}

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

void addTransaction(Transaction t) {
transactions.add(t);
}

public void processTransaction(String input, Account account) {
public void processTransaction(String input, Account account)
throws InvalidTransactionTypeException, InvalidAddTransactionSyntax {
// Check for syntax for add transaction
String[] arguments = {"/t/", "/n/", "/$/", "/d/", "/c/"};
for (String argument : arguments) {
if (!input.contains(argument)) {
throw new InvalidAddTransactionSyntax("Invalid add syntax.");
}
}

Transaction t = parser.parseTransaction(input, account);
addTransaction(t);
String fetchData = String.valueOf(transactions.get(transactions.size() - 1));
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/budgetbuddy/ui/UserInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,33 @@ public static void printUnknownError(String message){
System.out.println(LINE);
}

public static void printInvalidAddSyntax(String message) {
System.out.println(LINE);
System.out.println(TAB_SPACE + message);
System.out.println(TAB_SPACE + "Please ensure that you have entered all the arguments correctly.");
System.out.println(LINE);
}

public static void printTransactionTypeError(String message) {
System.out.println(LINE);
System.out.println(TAB_SPACE + "Invalid transaction type: " + message);
System.out.println(TAB_SPACE + "Please enter Expense or Income only.");
System.out.println(LINE);
}

public static void printNumberFormatError(String message) {
System.out.println(LINE);
System.out.println(TAB_SPACE + "Error occurred with the input: " + message);
System.out.println(TAB_SPACE + "Please enter an integer.");
System.out.println(LINE);
}

public static void printEmptyArgumentError(String message){
System.out.println(LINE);
System.out.println(TAB_SPACE + "Please include the " + message + "in the command.");
System.out.println(LINE);
}

public static void printAllTransactions(ArrayList<Transaction> transactions, double balance) {
int index = transactions.size();
System.out.println(LINE);
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,5 +1,6 @@
package budgetbuddy.parser;
import budgetbuddy.account.Account;
import budgetbuddy.exception.InvalidTransactionTypeException;
import org.junit.jupiter.api.Test;
import budgetbuddy.transaction.type.Transaction;

Expand All @@ -11,7 +12,7 @@
public class ParserTest {

@Test
public void testParseTransaction() {
public void testParseTransaction() throws InvalidTransactionTypeException {
Parser parser = new Parser();
Account account = new Account();
Transaction transaction = parser.parseTransaction("add /t/Income /n/Shopping /$/50 /d/14-03-2024 " +
Expand Down
50 changes: 48 additions & 2 deletions src/test/java/budgetbuddy/transaction/TransactionListTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package budgetbuddy.transaction;

import budgetbuddy.account.Account;
import budgetbuddy.exception.EmptyArgumentException;
import budgetbuddy.exception.InvalidAddTransactionSyntax;
import budgetbuddy.exception.InvalidTransactionTypeException;
import budgetbuddy.transaction.type.Income;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -28,7 +31,8 @@ public void getTransactions_initiallyEmpty() {
}

@Test
public void processTransaction_addsTransaction() {
public void processTransaction_addsTransaction()
throws InvalidTransactionTypeException, InvalidAddTransactionSyntax {
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);
Expand All @@ -41,7 +45,29 @@ public void processTransaction_addsTransaction() {
}

@Test
public void removeTransaction_removesCorrectTransaction() {
public void processTransaction_withInvalidAddSyntax_throwsInvalidAddTransactionSyntax() {

assertThrows(InvalidAddTransactionSyntax.class, () -> transactionList.processTransaction(
"add Expense /n/Shopping /$/50 /d/14-03-2024 /c/Personal", account));
assertThrows(InvalidAddTransactionSyntax.class, () -> transactionList.processTransaction(
"add /t/Expense Shopping /$/50 /d/14-03-2024 /c/Personal", account));
assertThrows(InvalidAddTransactionSyntax.class, () -> transactionList.processTransaction(
"add /t/Expense /n/Shopping 50 /d/14-03-2024 /c/Personal", account));
assertThrows(InvalidAddTransactionSyntax.class, () -> transactionList.processTransaction(
"add /t/Expense /n/Shopping /$/50 14-03-2024 /c/Personal", account));
assertThrows(InvalidAddTransactionSyntax.class, () -> transactionList.processTransaction(
"add /t/Expense /n/Shopping /$/50 /d/14-03-2024 Personal", account));
}

@Test
public void processTransaction_withInvalidTransactionType_throwsTransactionTypeException() {

assertThrows(InvalidTransactionTypeException.class, () -> transactionList.processTransaction(
"add /t/Donation /n/Test /$/200 /d/14-03-2024 /c/Personal", account));
}

@Test
public void removeTransaction_removesCorrectTransaction() throws EmptyArgumentException {
Transaction testTransaction1 = new Income("Test1", 100, "Category1",
"14-03-2024", account);
Transaction testTransaction2 = new Income("Test2", 200, "Category2",
Expand All @@ -64,4 +90,24 @@ public void removeTransaction_withInvalidIndex_throwsIndexOutOfBoundsException()
assertThrows(IndexOutOfBoundsException.class, () -> transactionList.removeTransaction(
"delete 2", account));
}

@Test
public void removeTransaction_withMissingIndex_throwsEmptyArgumentException() {
Transaction testTransaction = new Income("Test", 100, "Personal",
"14-03-2024", account);
transactionList.addTransaction(testTransaction);

assertThrows(EmptyArgumentException.class, () -> transactionList.removeTransaction(
"delete", account));
}

@Test
public void removeTransaction_withInvalidIndex_throwsNumberFormatException() {
Transaction testTransaction = new Income("Test", 100, "Personal",
"14-03-2024", account);
transactionList.addTransaction(testTransaction);

assertThrows(NumberFormatException.class, () -> transactionList.removeTransaction(
"delete one", account));
}
}

0 comments on commit 5053607

Please sign in to comment.