Skip to content

Commit

Permalink
Merge pull request #373 from RichDom2185/week11/fix-find-algorithm
Browse files Browse the repository at this point in the history
Fix find command algorithm
  • Loading branch information
tingkai-mai authored Oct 28, 2022
2 parents 46adb0f + 10ac470 commit 4b8c3df
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public NameContainsKeywordsPredicate(List<String> keywords) {
@Override
public boolean test(Item item) {
return keywords.stream()
.anyMatch(keyword -> StringUtil.containsSubstringIgnoreCase(item.getName().toString(), keyword));
.allMatch(keyword -> StringUtil.containsSubstringIgnoreCase(item.getName().toString(), keyword));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,22 @@ public void equals() {

@Test
public void execute_zeroKeywords_noItemsFound() {
String expectedMessage = String.format(MESSAGE_ITEMS_LISTED_OVERVIEW, 0);
String expectedMessage = String.format(MESSAGE_ITEMS_LISTED_OVERVIEW, 2);
NameContainsKeywordsPredicate predicate = preparePredicate(" ");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredItemList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getCurrentList());
assertEquals(Arrays.asList(POTATOES, CUCUMBERS), model.getCurrentList());
}

@Test
public void execute_multipleKeywords_multipleItemsFound() {
String expectedMessage = String.format(MESSAGE_ITEMS_LISTED_OVERVIEW, 2);
String expectedMessage = String.format(MESSAGE_ITEMS_LISTED_OVERVIEW, 0);
NameContainsKeywordsPredicate predicate = preparePredicate("Potatoes Cucumbers Carrots");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredItemList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(POTATOES, CUCUMBERS), model.getCurrentList());
assertEquals(Collections.emptyList(), model.getCurrentList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import seedu.foodrem.testutil.ItemBuilder;

// TODO: Properly test this
public class NameContainsKeywordsPredicateTest {
@Test
public void equals() {
Expand Down Expand Up @@ -52,7 +53,7 @@ public void test_nameContainsKeywords_returnsTrue() {

// Only one matching keyword
predicate = new NameContainsKeywordsPredicate(Arrays.asList("Potato", "Cuc"));
assertTrue(predicate.test(new ItemBuilder().withItemName("Potato Carol").build()));
assertFalse(predicate.test(new ItemBuilder().withItemName("Potato Carol").build()));

// Mixed-case keywords
predicate = new NameContainsKeywordsPredicate(Arrays.asList("PoTAto", "CuCUmber"));
Expand All @@ -63,16 +64,15 @@ public void test_nameContainsKeywords_returnsTrue() {
public void test_nameDoesNotContainKeywords_returnsFalse() {
// Zero keywords
NameContainsKeywordsPredicate predicate = new NameContainsKeywordsPredicate(Collections.emptyList());
assertFalse(predicate.test(new ItemBuilder().withItemName("Potato").build()));
assertTrue(predicate.test(new ItemBuilder().withItemName("Potato").build()));

// Non-matching keyword
predicate = new NameContainsKeywordsPredicate(List.of("Carrots"));
assertFalse(predicate.test(new ItemBuilder().withItemName("Potato Cucumber").build()));

// Keywords match quantity, bought date and expiry date, and name as it is a substring
predicate = new NameContainsKeywordsPredicate(Arrays.asList("Potato", "12345", "11-11-2022", "12-12-2022"));
// TODO: Move this to the correct test method
assertTrue(predicate.test(new ItemBuilder()
assertFalse(predicate.test(new ItemBuilder()
.withItemName("Potatoes")
.withItemQuantity("12345")
.withItemBoughtDate("11-11-2022")
Expand Down

0 comments on commit 4b8c3df

Please sign in to comment.