From 9248a4f71d2837f02e9ff4b2e1d994811a5c39f3 Mon Sep 17 00:00:00 2001 From: telferang <94974226+telferang@users.noreply.github.com> Date: Tue, 12 Nov 2024 10:25:22 +0800 Subject: [PATCH 1/3] Refactor code according to coding standard --- .../budgetbuddy/transaction/expense/Expense.java | 16 +++++++++------- .../transaction/expense/ExpenseManager.java | 2 +- .../validators/expense/AddExpenseValidator.java | 2 +- .../expense/DeleteExpenseValidator.java | 3 +++ .../validators/expense/EditExpenseValidator.java | 4 ++-- .../validators/income/DeleteIncomeValidator.java | 3 +++ .../expense/AddExpenseValidatorTest.java | 2 +- text-ui-test/EXPECTED.TXT | 2 +- 8 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/main/java/seedu/budgetbuddy/transaction/expense/Expense.java b/src/main/java/seedu/budgetbuddy/transaction/expense/Expense.java index 42a81b1f8c..47c0a5db03 100644 --- a/src/main/java/seedu/budgetbuddy/transaction/expense/Expense.java +++ b/src/main/java/seedu/budgetbuddy/transaction/expense/Expense.java @@ -11,7 +11,7 @@ */ public class Expense extends Transaction { /** The category of the expense. */ - Category category; + private Category category; /** * Constructs an Expense object with the specified details. @@ -31,13 +31,10 @@ public Expense(String description, double amount, LocalDate date, Category categ * * @return a string describing the expense */ + @Override public String toString() { - String output = ""; - output += "Description: " + description; - output += " Amount: " + String.format("%.2f", amount); - output += " Date: " + date; - output += " Category: " + category; - return output; + return String.format("Description: %s Amount: %.2f Date: %s Category: %s", + description, amount, date, category); } /** @@ -77,6 +74,11 @@ public Category getCategory() { } + /** + * Updates the category of the expense. + * + * @param changeCategory The new category for the expense. + */ public void editCategory(Category changeCategory) { this.category = changeCategory; } diff --git a/src/main/java/seedu/budgetbuddy/transaction/expense/ExpenseManager.java b/src/main/java/seedu/budgetbuddy/transaction/expense/ExpenseManager.java index fb196a3408..372d5c5f5f 100644 --- a/src/main/java/seedu/budgetbuddy/transaction/expense/ExpenseManager.java +++ b/src/main/java/seedu/budgetbuddy/transaction/expense/ExpenseManager.java @@ -32,7 +32,7 @@ public class ExpenseManager { * @param expenses is the content to be instantiated */ public ExpenseManager(ArrayList expenses, int numberOfExpenses) { - assert numberOfExpenses >= 0 : "numberOfExpenses should be greater than 0"; + assert numberOfExpenses >= 0 : "numberOfExpenses should be greater than or equal to 0"; ExpenseManager.expenses = expenses; ExpenseManager.numberOfExpenses = numberOfExpenses; } diff --git a/src/main/java/seedu/budgetbuddy/validators/expense/AddExpenseValidator.java b/src/main/java/seedu/budgetbuddy/validators/expense/AddExpenseValidator.java index b3316ff39b..6ec8eaacd1 100644 --- a/src/main/java/seedu/budgetbuddy/validators/expense/AddExpenseValidator.java +++ b/src/main/java/seedu/budgetbuddy/validators/expense/AddExpenseValidator.java @@ -52,7 +52,7 @@ public static Command processCommand(String command) { case "d/": date = validateDate(part); if (date == null) { - return new IncorrectCommand("Invalid date format. Use d/dd/MM/yyyy."); + return new IncorrectCommand("Invalid date format. Use d/DD/MM/YYYY."); } break; diff --git a/src/main/java/seedu/budgetbuddy/validators/expense/DeleteExpenseValidator.java b/src/main/java/seedu/budgetbuddy/validators/expense/DeleteExpenseValidator.java index edbf4f417d..2726acab26 100644 --- a/src/main/java/seedu/budgetbuddy/validators/expense/DeleteExpenseValidator.java +++ b/src/main/java/seedu/budgetbuddy/validators/expense/DeleteExpenseValidator.java @@ -18,12 +18,15 @@ public class DeleteExpenseValidator { */ public static Command processCommand(String command) { String trimmedCommand; + try { trimmedCommand = command.substring("delete expense ".length()); } catch (IndexOutOfBoundsException e) { return new IncorrectCommand("Index not given"); } + int index = IndexValidator.validateExpenseIndex(trimmedCommand); + if (index == -1) { return new IncorrectCommand("Invalid Index"); } diff --git a/src/main/java/seedu/budgetbuddy/validators/expense/EditExpenseValidator.java b/src/main/java/seedu/budgetbuddy/validators/expense/EditExpenseValidator.java index 7b4484f90c..874f55871f 100644 --- a/src/main/java/seedu/budgetbuddy/validators/expense/EditExpenseValidator.java +++ b/src/main/java/seedu/budgetbuddy/validators/expense/EditExpenseValidator.java @@ -88,8 +88,8 @@ public static boolean processSecondCommand(String command){ } } else if (part.startsWith("a/")) { amount = validateAmount(part); - if (amount < 0.0) { - Ui.displayToUser("Invalid amount format. Amount should be a positive number."); + if (amount <= 0.0) { + Ui.displayToUser("Amount should be a positive number with up to 2 decimal places."); return false; } } diff --git a/src/main/java/seedu/budgetbuddy/validators/income/DeleteIncomeValidator.java b/src/main/java/seedu/budgetbuddy/validators/income/DeleteIncomeValidator.java index 6393157738..6b3496b31f 100644 --- a/src/main/java/seedu/budgetbuddy/validators/income/DeleteIncomeValidator.java +++ b/src/main/java/seedu/budgetbuddy/validators/income/DeleteIncomeValidator.java @@ -18,12 +18,15 @@ public class DeleteIncomeValidator { */ public static Command processCommand(String command) { String trimmedCommand; + try { trimmedCommand = command.substring("delete income ".length()); } catch (IndexOutOfBoundsException e) { return new IncorrectCommand("Index not given"); } + int index = IndexValidator.validateIncomeIndex(trimmedCommand); + if (index == -1) { return new IncorrectCommand("Invalid Index"); } diff --git a/src/test/java/seedu/budgetbuddy/validators/expense/AddExpenseValidatorTest.java b/src/test/java/seedu/budgetbuddy/validators/expense/AddExpenseValidatorTest.java index 048a18e737..0a32cc7cd3 100644 --- a/src/test/java/seedu/budgetbuddy/validators/expense/AddExpenseValidatorTest.java +++ b/src/test/java/seedu/budgetbuddy/validators/expense/AddExpenseValidatorTest.java @@ -40,7 +40,7 @@ void processCommand_invalidDate_returnsIncorrectCommand() { // Check if result is an IncorrectCommand and compare the message assertTrue(result instanceof IncorrectCommand); String message = ((IncorrectCommand) result).getFeedbackToUser(); - assertEquals("Invalid date format. Use d/dd/MM/yyyy.", message); + assertEquals("Invalid date format. Use d/DD/MM/YYYY.", message); } @Test diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index b15fcdc576..30d282223d 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -11,7 +11,7 @@ Enter commands: ======================================================== Amount should be a positive number with up to 2 decimal places. ======================================================== Enter commands: ======================================================== -Invalid date format. Use d/dd/MM/yyyy. +Invalid date format. Use d/DD/MM/YYYY. ======================================================== Enter commands: ======================================================== The following expense transaction has been added: From ba16eea327305169302f25d9505273d8734495d3 Mon Sep 17 00:00:00 2001 From: telferang <94974226+telferang@users.noreply.github.com> Date: Tue, 12 Nov 2024 10:25:44 +0800 Subject: [PATCH 2/3] Add DeleteIncomeValidator Junit test --- .../commands/income/DeleteIncomeCommand.java | 7 ++ .../income/DeleteIncomeValidatorTest.java | 77 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/test/java/seedu/budgetbuddy/validators/income/DeleteIncomeValidatorTest.java diff --git a/src/main/java/seedu/budgetbuddy/commands/income/DeleteIncomeCommand.java b/src/main/java/seedu/budgetbuddy/commands/income/DeleteIncomeCommand.java index 182f086b52..8bef6422c3 100644 --- a/src/main/java/seedu/budgetbuddy/commands/income/DeleteIncomeCommand.java +++ b/src/main/java/seedu/budgetbuddy/commands/income/DeleteIncomeCommand.java @@ -37,4 +37,11 @@ public static boolean isCommand(String command) { public void execute(){ IncomeManager.deleteIncome(index); } + + /** + * Returns the index of income to delete. + */ + public int getIndex() { + return index; + } } diff --git a/src/test/java/seedu/budgetbuddy/validators/income/DeleteIncomeValidatorTest.java b/src/test/java/seedu/budgetbuddy/validators/income/DeleteIncomeValidatorTest.java new file mode 100644 index 0000000000..34ad496d11 --- /dev/null +++ b/src/test/java/seedu/budgetbuddy/validators/income/DeleteIncomeValidatorTest.java @@ -0,0 +1,77 @@ +package seedu.budgetbuddy.validators.income; + +import org.junit.jupiter.api.Test; +import seedu.budgetbuddy.commands.Command; +import seedu.budgetbuddy.commands.income.DeleteIncomeCommand; +import seedu.budgetbuddy.commands.IncorrectCommand; +import seedu.budgetbuddy.transaction.income.Income; +import seedu.budgetbuddy.transaction.income.IncomeManager; + +import java.time.LocalDate; + +import static org.junit.jupiter.api.Assertions.*; + +class DeleteIncomeValidatorTest { + + @Test + void processCommand_outOfRangeIndex_returnsIncorrectIncomeCommand() { + String command = "delete income 10"; + Command result = DeleteIncomeValidator.processCommand(command.trim()); + + assertTrue(result instanceof IncorrectCommand); + assertEquals("Invalid Index", ((IncorrectCommand) result).getFeedbackToUser()); + } + + @Test + void processCommand_noIndexProvided_returnsIncorrectCommandIndexNotGiven() { + String command = "delete income"; + Command result = DeleteIncomeValidator.processCommand(command); + + assertTrue(result instanceof IncorrectCommand); + assertEquals("Index not given", ((IncorrectCommand) result).getFeedbackToUser()); + } + + @Test + void processCommand_invalidIndex_returnsIncorrectCommandInvalidIndex() { + String command = "delete income abc"; + Command result = DeleteIncomeValidator.processCommand(command); + + assertTrue(result instanceof IncorrectCommand); + assertEquals("Invalid Index", ((IncorrectCommand) result).getFeedbackToUser()); + } + + @Test + void processCommand_validIndex_returnsDeleteIncomeCommand() { + IncomeManager.addIncome(new Income("Salary", 5000.00, LocalDate.of(2024, 11, 1))); + IncomeManager.addIncome(new Income("Freelance", 1500.00, LocalDate.of(2024, 11, 5))); + IncomeManager.addIncome(new Income("Bonus", 2000.00, LocalDate.of(2024, 11, 10))); + + String command = "delete income 3"; + Command result = DeleteIncomeValidator.processCommand(command); + + assertTrue(result instanceof DeleteIncomeCommand); + assertEquals(2, ((DeleteIncomeCommand) result).getIndex()); // Expecting index to be 2 due to 0-based indexing + } + + @Test + void processCommand_negativeIndex_returnsIncorrectCommandInvalidIndex() { + String command = "delete income -1"; + Command result = DeleteIncomeValidator.processCommand(command); + + assertTrue(result instanceof IncorrectCommand); + assertEquals("Invalid Index", ((IncorrectCommand) result).getFeedbackToUser()); + } + + @Test + void processCommand_extraSpacesAfterIndex_returnsDeleteIncomeCommand() { + IncomeManager.addIncome(new Income("Salary", 5000.00, LocalDate.of(2024, 11, 1))); + IncomeManager.addIncome(new Income("Freelance", 1500.00, LocalDate.of(2024, 11, 5))); + IncomeManager.addIncome(new Income("Bonus", 2000.00, LocalDate.of(2024, 11, 10))); + + String command = "delete income 3 "; + Command result = DeleteIncomeValidator.processCommand(command.trim()); + + assertTrue(result instanceof DeleteIncomeCommand); + assertEquals(2, ((DeleteIncomeCommand) result).getIndex()); + } +} From 75bb41a529ab32cdd491732bd7e72e3d520225f9 Mon Sep 17 00:00:00 2001 From: telferang <94974226+telferang@users.noreply.github.com> Date: Tue, 12 Nov 2024 10:28:34 +0800 Subject: [PATCH 3/3] Remove wildcard import --- .../validators/income/DeleteIncomeValidatorTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/seedu/budgetbuddy/validators/income/DeleteIncomeValidatorTest.java b/src/test/java/seedu/budgetbuddy/validators/income/DeleteIncomeValidatorTest.java index 34ad496d11..a05c78ef27 100644 --- a/src/test/java/seedu/budgetbuddy/validators/income/DeleteIncomeValidatorTest.java +++ b/src/test/java/seedu/budgetbuddy/validators/income/DeleteIncomeValidatorTest.java @@ -9,7 +9,8 @@ import java.time.LocalDate; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; class DeleteIncomeValidatorTest {