diff --git a/src/main/java/seedu/budgetbuddy/command/FindExpensesCommand.java b/src/main/java/seedu/budgetbuddy/command/FindExpensesCommand.java index 28450a835a..0832be5f6c 100644 --- a/src/main/java/seedu/budgetbuddy/command/FindExpensesCommand.java +++ b/src/main/java/seedu/budgetbuddy/command/FindExpensesCommand.java @@ -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; @@ -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); diff --git a/src/main/java/seedu/budgetbuddy/commandcreator/FindExpensesCommandCreator.java b/src/main/java/seedu/budgetbuddy/commandcreator/FindExpensesCommandCreator.java index 11bb934c8b..8b22cdc7b6 100644 --- a/src/main/java/seedu/budgetbuddy/commandcreator/FindExpensesCommandCreator.java +++ b/src/main/java/seedu/budgetbuddy/commandcreator/FindExpensesCommandCreator.java @@ -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/"); @@ -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; @@ -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; @@ -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(); @@ -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"); } } @@ -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; } diff --git a/src/main/java/seedu/budgetbuddy/commons/ExpenseList.java b/src/main/java/seedu/budgetbuddy/commons/ExpenseList.java index 9c02710cf8..dedca99ea6 100644 --- a/src/main/java/seedu/budgetbuddy/commons/ExpenseList.java +++ b/src/main/java/seedu/budgetbuddy/commons/ExpenseList.java @@ -77,8 +77,8 @@ public ArrayList filterExpenses(String description, Double minAmount, D ArrayList 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"); diff --git a/src/test/java/seedu/budgetbuddy/ExpenseListTest.java b/src/test/java/seedu/budgetbuddy/ExpenseListTest.java index dfe1dd492a..164f14e949 100644 --- a/src/test/java/seedu/budgetbuddy/ExpenseListTest.java +++ b/src/test/java/seedu/budgetbuddy/ExpenseListTest.java @@ -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 filteredExpenses = expenses.filterExpenses("" @@ -143,7 +143,7 @@ public void filterExpenses_filterByMaxAmount_returnsOneMatches() throws BudgetBu ArrayList filteredExpenses = expenses.filterExpenses("" , null, 75.00); - assertEquals(1, filteredExpenses.size()); + assertEquals(2, filteredExpenses.size()); }