Skip to content

Commit

Permalink
Merge pull request #192 from itsmejr257/junRong-BugFixes-Find
Browse files Browse the repository at this point in the history
Add Bug-Fixes for Find Feature
  • Loading branch information
itsmejr257 authored Apr 9, 2024
2 parents 18c48de + e5282e4 commit 8d4ef57
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public FindExpensesCommand(ExpenseList expenses, String description, Double minA
ui = new Ui();
this.expenses = expenses;

if(description == null) {
if(description == null || description.isEmpty()) {
this.description = "";
} else {
this.description = description;
Expand All @@ -39,7 +39,7 @@ private void printInitializationMessage() {
System.out.println("Looking for Expenses with the following parameters : ");

System.out.println("Description : ");
if (description == null) {
if (description == null || description.isEmpty()) {
System.out.println("N.A");
} else {
System.out.println(description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ public FindExpensesCommandCreator(String input, ExpenseList expenses) {
this.expenses = expenses;
}

private void checkForOutOfOrderParameters(String input) throws BudgetBuddyException {
int indexOfDescriptionPrefix = input.indexOf(DESCRIPTION_PREFIX);
int indexOfMinAmountPrefix = input.indexOf(MINAMOUNT_PREFIX);
int indexOfMaxAmountPrefix = input.indexOf(MAXAMOUNT_PREFIX);

if (indexOfDescriptionPrefix > indexOfMinAmountPrefix) {
throw new BudgetBuddyException("Please ensure that your parameters are in the right order.");
}

if (indexOfMinAmountPrefix > indexOfMaxAmountPrefix) {
throw new BudgetBuddyException("Please ensure that your parameters are in the right order.");
}

}

private static void checkForInvalidParameters(String input) {
if (!input.contains("d/") || !input.contains("morethan/") || !input.contains("lessthan/")) {
throw new IllegalArgumentException("Please Ensure that you include d/, morethan/ and lessthan/");
Expand All @@ -37,7 +52,7 @@ private Double parseMaxAmount(String input) throws NumberFormatException{

int endIndexOfMaxAmount = input.length();

String maxAmountAsString = input.substring(startIndexOfMaxAmount, endIndexOfMaxAmount);
String maxAmountAsString = input.substring(startIndexOfMaxAmount, endIndexOfMaxAmount).trim();

if (maxAmountAsString.trim().isEmpty()) {
return null;
Expand All @@ -53,9 +68,9 @@ private Double parseMinAmount(String input) {
int startIndexOfMinAmount = indexOfMinAmountPrefix + MINAMOUNT_PREFIX.length();

int indexOfMaxAmountPrefix = input.indexOf(MAXAMOUNT_PREFIX);
int endIndexOfMinAmount = indexOfMaxAmountPrefix - 1;
int endIndexOfMinAmount = indexOfMaxAmountPrefix;

String minAmountAsString = input.substring(startIndexOfMinAmount, endIndexOfMinAmount);
String minAmountAsString = input.substring(startIndexOfMinAmount, endIndexOfMinAmount).trim();

if (minAmountAsString.trim().isEmpty()) {
return null;
Expand All @@ -71,7 +86,7 @@ private String parseDescription(String input) {
int startIndexOfDescription = indexOfDescriptionPrefix + DESCRIPTION_PREFIX.length();

int indexOfMinAmountPrefix = input.indexOf(MINAMOUNT_PREFIX);
int endIndexOfDescription = indexOfMinAmountPrefix - 1;
int endIndexOfDescription = indexOfMinAmountPrefix;

String description = input.substring(startIndexOfDescription, endIndexOfDescription).trim();

Expand Down Expand Up @@ -102,7 +117,7 @@ private static void checkForDuplicateParameters(String input, String parameter)
private static void compareMinAndMaxAmount(Double minAmount, Double maxAmount) throws BudgetBuddyException{

if (minAmount != null && maxAmount != null) {
if (minAmount >= maxAmount) {
if (minAmount > maxAmount) {
throw new BudgetBuddyException("Ensure minimum amount is smaller than maximum amount");
}
}
Expand All @@ -125,11 +140,12 @@ private Command handleFindExpensesCommand(String input, ExpenseList expenses) {

try {
checkForInvalidParameters(input);
checkForOutOfOrderParameters(input);
checkForDuplicateParameters(input, "d/");
checkForDuplicateParameters(input, "morethan/");
checkForDuplicateParameters(input, "lessthan/");

} catch (IllegalArgumentException e) {
} catch (IllegalArgumentException | BudgetBuddyException e) {
System.out.println(e.getMessage());
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/budgetbuddy/commons/ExpenseList.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public ArrayList<Expense> filterExpenses(String description, Double minAmount, D
ArrayList<Expense> filteredExpenses = new ArrayList<>(this.expenses.stream()
.filter(expense -> (expense.getDescription()
.toLowerCase().contains(descriptionInLowerCase)))
.filter(expense -> (minAmount == null || expense.getAmount() > minAmount))
.filter(expense -> (maxAmount == null || expense.getAmount() < maxAmount))
.filter(expense -> (minAmount == null || expense.getAmount() >= minAmount))
.filter(expense -> (maxAmount == null || expense.getAmount() <= maxAmount))
.collect(Collectors.toList()));

LOGGER.log(Level.INFO, "Ending filtering and returning filtered expenses");
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/seedu/budgetbuddy/ExpenseListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void filterExpenses_filterByDescription_returnsTwoMatches() throws Budget
public void filterExpenses_filterByMinAmount_returnsThreeMatches() throws BudgetBuddyException {
ExpenseList expenses = new ExpenseList();
expenses.addExpense("Groceries", "100", "Apples");
expenses.addExpense("Transport", "50", "Bus fare");
expenses.addExpense("Transport", "40", "Bus fare");
expenses.addExpense("Entertainment", "75", "Movie");
expenses.addExpense("Groceries", "100", "apple");
ArrayList<Expense> filteredExpenses = expenses.filterExpenses(""
Expand All @@ -143,7 +143,7 @@ public void filterExpenses_filterByMaxAmount_returnsOneMatches() throws BudgetBu
ArrayList<Expense> filteredExpenses = expenses.filterExpenses(""
, null, 75.00);

assertEquals(1, filteredExpenses.size());
assertEquals(2, filteredExpenses.size());

}

Expand Down

0 comments on commit 8d4ef57

Please sign in to comment.