Skip to content

Commit

Permalink
Merge pull request #241 from telferang/branch-bug
Browse files Browse the repository at this point in the history
Branch coding standard and Junit
  • Loading branch information
telferang authored Nov 12, 2024
2 parents 4e843f9 + 75bb41a commit c66a0e3
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
16 changes: 9 additions & 7 deletions src/main/java/seedu/budgetbuddy/transaction/expense/Expense.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class ExpenseManager {
* @param expenses is the content to be instantiated
*/
public ExpenseManager(ArrayList<Expense> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void processCommand_invalidDate_returnsIncorrectCommand() {
// Check if result is an IncorrectCommand and compare the message
assertInstanceOf(IncorrectCommand.class, result);
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

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());
}
}
2 changes: 1 addition & 1 deletion text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit c66a0e3

Please sign in to comment.