Skip to content

Commit

Permalink
Merge pull request #23 from Vavinan/fix-bug
Browse files Browse the repository at this point in the history
Fix: Empty arguments and invalid index
  • Loading branch information
ShyamKrishna33 committed Mar 25, 2024
2 parents 9d17d25 + de80922 commit 283a884
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 42 deletions.
42 changes: 23 additions & 19 deletions src/main/java/budgetbuddy/BudgetBuddy.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -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());
}

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package budgetbuddy.exception;
package budgetbuddy.exceptions;

public class EmptyArgumentException extends Exception {
public EmptyArgumentException(String message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package budgetbuddy.exception;
package budgetbuddy.exceptions;

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

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

public class InvalidTransactionTypeException extends Exception{
public InvalidTransactionTypeException (String message) {
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/budgetbuddy/parser/Parser.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,15 +13,15 @@ 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;
String description = null;
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();
Expand All @@ -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);
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/budgetbuddy/transaction/TransactionList.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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");
}
Expand All @@ -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));
}
}

Expand All @@ -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) {
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/budgetbuddy/ui/UserInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions 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.exception.InvalidTransactionTypeException;
import budgetbuddy.exceptions.EmptyArgumentException;
import budgetbuddy.exceptions.InvalidTransactionTypeException;
import org.junit.jupiter.api.Test;
import budgetbuddy.transaction.type.Transaction;

Expand All @@ -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 " +
Expand Down
13 changes: 7 additions & 6 deletions src/test/java/budgetbuddy/transaction/TransactionListTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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",
Expand All @@ -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));
}

Expand Down

0 comments on commit 283a884

Please sign in to comment.