Skip to content

Commit

Permalink
Merge pull request #80 from woojiahao/fix/get-total-expenditure-categ…
Browse files Browse the repository at this point in the history
…ory-bug

Fix GetTotalExpenditure for expense w/ no category
  • Loading branch information
woojiahao authored Oct 18, 2023
2 parents 76666ed + 48e51e2 commit 79050ac
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ public CommandResult execute(Model model) throws CommandException {
model.updateFilteredTransactionList(transaction -> {
boolean isExpense = transaction.getType().type.equals(TransactionType.EXPENSE);
boolean isSameMonth = transaction.getDateTime().getDateTime().getMonthValue() == month;
boolean hasCategory = transaction.getCategories().stream().anyMatch(cat -> {
if (categoryFilter == null) {
return true;
}
return cat.equals(categoryFilter);
});
if (categoryFilter == null) {
// No category filter so just get all expenses of the month
return isExpense && isSameMonth;
}

// If category filter exists and expense contains no category, it will not have the category
// Note: If the stream is empty then false is returned and the predicate is not evaluated.
boolean hasCategory = transaction.getCategories().stream().anyMatch(cat -> cat.equals(categoryFilter));
return isExpense && isSameMonth && hasCategory;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,41 @@ public void execute_multipleMonthsOnly_filtersOnlySelectedMonths() throws Comman
}
}

@Test
public void execute_expenseWithoutCategoryWithCategoryFilter_notIncludedInFilter() throws CommandException {
var model = getModel();
// This transaction does not contain any categories even if it's in August
model.addTransaction(new TransactionBuilder().withCategories().withType("expense").build());
model.addTransaction(new TransactionBuilder().withType("expense").withDateTime("18-07-2001 00:00").build());
model.addTransaction(new TransactionBuilder().withType("expense").withCategories("Food").build());
var command = new GetTotalExpenditureCommand(8, new Category("Food"));
command.execute(model);
var filteredResult = model.getFilteredTransactionList();
assertEquals(1, filteredResult.size());
for (var result : filteredResult) {
assertEquals(TransactionType.EXPENSE, result.getType().type);
assertEquals(8, result.getDateTime().getDateTime().getMonthValue());
}
}

@Test
public void execute_expenseWithoutCategoryWithoutCategoryFilter_includedInFilter() throws CommandException {
var model = getModel();
// This transaction does not contain any categories even if it's in August
// This should be included this round as no category filter is in place
model.addTransaction(new TransactionBuilder().withCategories().withType("expense").build());
model.addTransaction(new TransactionBuilder().withType("expense").withDateTime("18-07-2001 00:00").build());
model.addTransaction(new TransactionBuilder().withType("expense").withCategories("Food").build());
var command = new GetTotalExpenditureCommand(8, null);
command.execute(model);
var filteredResult = model.getFilteredTransactionList();
assertEquals(2, filteredResult.size());
for (var result : filteredResult) {
assertEquals(TransactionType.EXPENSE, result.getType().type);
assertEquals(8, result.getDateTime().getDateTime().getMonthValue());
}
}

@Test
public void execute_multipleCategoriesOnly_filtersOnlySelectedCategory() throws CommandException {
var model = getModel();
Expand Down

0 comments on commit 79050ac

Please sign in to comment.