Skip to content

Commit

Permalink
Merge pull request #56 from dannytayjy/branch-find-feature
Browse files Browse the repository at this point in the history
Modify existing feature: finding a member (find)
  • Loading branch information
ryanongra authored Oct 12, 2021
2 parents 4663fbd + 7ce8936 commit ea2a7ce
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 71 deletions.
6 changes: 3 additions & 3 deletions src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import seedu.address.commons.core.Messages;
import seedu.address.model.Model;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.MatchesKeywordsPredicate;

/**
* Finds and lists all persons in address book whose name contains any of the argument keywords.
Expand All @@ -19,9 +19,9 @@ public class FindCommand extends Command {
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n"
+ "Example: " + COMMAND_WORD + " alice bob charlie";

private final NameContainsKeywordsPredicate predicate;
private final MatchesKeywordsPredicate predicate;

public FindCommand(NameContainsKeywordsPredicate predicate) {
public FindCommand(MatchesKeywordsPredicate predicate) {
this.predicate = predicate;
}

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

import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.MatchesKeywordsPredicate;

/**
* Parses input arguments and creates a new FindCommand object
Expand All @@ -25,9 +25,9 @@ public FindCommand parse(String args) throws ParseException {
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}

String[] nameKeywords = trimmedArgs.split("\\s+");
String[] keywords = trimmedArgs.split("\\s+");

return new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
return new FindCommand(new MatchesKeywordsPredicate(Arrays.asList(keywords)));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package seedu.address.model.person;

import java.util.List;
import java.util.function.Predicate;

import seedu.address.commons.util.StringUtil;

/**
* Tests that a {@code Person}'s {@code Name}, {@code Phone}, {@code Email} or
* {@code Telegram Handle} matches any of the keywords given.
*/
public class MatchesKeywordsPredicate implements Predicate<Person> {
private final List<String> keywords;

public MatchesKeywordsPredicate(List<String> keywords) {
this.keywords = keywords;
}

@Override
public boolean test(Person person) {
return keywords.stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getName().toString(), keyword)
|| StringUtil.containsWordIgnoreCase(person.getPhone().toString(), keyword)
|| StringUtil.containsWordIgnoreCase(person.getEmail().toString(), keyword)
|| StringUtil.containsWordIgnoreCase(person.getTelegram().toString(), keyword));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof MatchesKeywordsPredicate // instanceof handles nulls
&& keywords.equals(((MatchesKeywordsPredicate) other).keywords)); // state check
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.MatchesKeywordsPredicate;
import seedu.address.model.person.Person;
import seedu.address.testutil.EditPersonDescriptorBuilder;

Expand Down Expand Up @@ -120,7 +120,7 @@ public static void showPersonAtIndex(Model model, Index targetIndex) {

Person person = model.getFilteredPersonList().get(targetIndex.getZeroBased());
final String[] splitName = person.getName().fullName.split("\\s+");
model.updateFilteredPersonList(new NameContainsKeywordsPredicate(Arrays.asList(splitName[0])));
model.updateFilteredPersonList(new MatchesKeywordsPredicate(Arrays.asList(splitName[0])));

assertEquals(1, model.getFilteredPersonList().size());
}
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/seedu/address/logic/commands/FindCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.MatchesKeywordsPredicate;

/**
* Contains integration tests (interaction with the Model) for {@code FindCommand}.
Expand All @@ -29,10 +29,10 @@ public class FindCommandTest {

@Test
public void equals() {
NameContainsKeywordsPredicate firstPredicate =
new NameContainsKeywordsPredicate(Collections.singletonList("first"));
NameContainsKeywordsPredicate secondPredicate =
new NameContainsKeywordsPredicate(Collections.singletonList("second"));
MatchesKeywordsPredicate firstPredicate =
new MatchesKeywordsPredicate(Collections.singletonList("first"));
MatchesKeywordsPredicate secondPredicate =
new MatchesKeywordsPredicate(Collections.singletonList("second"));

FindCommand findFirstCommand = new FindCommand(firstPredicate);
FindCommand findSecondCommand = new FindCommand(secondPredicate);
Expand All @@ -57,7 +57,7 @@ public void equals() {
@Test
public void execute_zeroKeywords_noPersonFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0);
NameContainsKeywordsPredicate predicate = preparePredicate(" ");
MatchesKeywordsPredicate predicate = preparePredicate(" ");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
Expand All @@ -67,7 +67,7 @@ public void execute_zeroKeywords_noPersonFound() {
@Test
public void execute_multipleKeywords_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3);
NameContainsKeywordsPredicate predicate = preparePredicate("Kurz Elle Kunz");
MatchesKeywordsPredicate predicate = preparePredicate("Kurz Elle Kunz");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
Expand All @@ -77,7 +77,7 @@ public void execute_multipleKeywords_multiplePersonsFound() {
/**
* Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}.
*/
private NameContainsKeywordsPredicate preparePredicate(String userInput) {
return new NameContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+")));
private MatchesKeywordsPredicate preparePredicate(String userInput) {
return new MatchesKeywordsPredicate(Arrays.asList(userInput.split("\\s+")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.MatchesKeywordsPredicate;
import seedu.address.model.person.NameEqualKeywordPredicate;
import seedu.address.model.person.Person;
import seedu.address.testutil.EditPersonDescriptorBuilder;
Expand Down Expand Up @@ -83,7 +83,7 @@ public void parseCommand_find() throws Exception {
List<String> keywords = Arrays.asList("foo", "bar", "baz");
FindCommand command = (FindCommand) parser.parseCommand(
FindCommand.COMMAND_WORD + " " + keywords.stream().collect(Collectors.joining(" ")));
assertEquals(new FindCommand(new NameContainsKeywordsPredicate(keywords)), command);
assertEquals(new FindCommand(new MatchesKeywordsPredicate(keywords)), command);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.FindCommand;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.MatchesKeywordsPredicate;

public class FindCommandParserTest {

Expand All @@ -24,7 +24,7 @@ public void parse_emptyArg_throwsParseException() {
public void parse_validArgs_returnsFindCommand() {
// no leading and trailing whitespaces
FindCommand expectedFindCommand =
new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob")));
new FindCommand(new MatchesKeywordsPredicate(Arrays.asList("Alice", "Bob")));
assertParseSuccess(parser, "Alice Bob", expectedFindCommand);

// multiple whitespaces between keywords
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/seedu/address/model/ModelManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.junit.jupiter.api.Test;

import seedu.address.commons.core.GuiSettings;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.MatchesKeywordsPredicate;
import seedu.address.testutil.AddressBookBuilder;

public class ModelManagerTest {
Expand Down Expand Up @@ -118,7 +118,7 @@ public void equals() {

// different filteredList -> returns false
String[] keywords = ALICE.getName().fullName.split("\\s+");
modelManager.updateFilteredPersonList(new NameContainsKeywordsPredicate(Arrays.asList(keywords)));
modelManager.updateFilteredPersonList(new MatchesKeywordsPredicate(Arrays.asList(keywords)));
assertFalse(modelManager.equals(new ModelManager(addressBook, userPrefs)));

// resets modelManager to initial state for upcoming tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@

import seedu.address.testutil.PersonBuilder;

public class NameContainsKeywordsPredicateTest {
public class MatchesKeywordsPredicateTest {

@Test
public void equals() {
List<String> firstPredicateKeywordList = Collections.singletonList("first");
List<String> secondPredicateKeywordList = Arrays.asList("first", "second");

NameContainsKeywordsPredicate firstPredicate = new NameContainsKeywordsPredicate(firstPredicateKeywordList);
NameContainsKeywordsPredicate secondPredicate = new NameContainsKeywordsPredicate(secondPredicateKeywordList);
MatchesKeywordsPredicate firstPredicate = new MatchesKeywordsPredicate(firstPredicateKeywordList);
MatchesKeywordsPredicate secondPredicate = new MatchesKeywordsPredicate(secondPredicateKeywordList);

// same object -> returns true
assertTrue(firstPredicate.equals(firstPredicate));

// same values -> returns true
NameContainsKeywordsPredicate firstPredicateCopy = new NameContainsKeywordsPredicate(firstPredicateKeywordList);
MatchesKeywordsPredicate firstPredicateCopy = new MatchesKeywordsPredicate(firstPredicateKeywordList);
assertTrue(firstPredicate.equals(firstPredicateCopy));

// different types -> returns false
Expand All @@ -39,37 +39,43 @@ public void equals() {
}

@Test
public void test_nameContainsKeywords_returnsTrue() {
public void test_matchesKeywords_returnsTrue() {
// One keyword
NameContainsKeywordsPredicate predicate = new NameContainsKeywordsPredicate(Collections.singletonList("Alice"));
MatchesKeywordsPredicate predicate = new MatchesKeywordsPredicate(Collections.singletonList("Alice"));
assertTrue(predicate.test(new PersonBuilder().withName("Alice Bob").build()));

// Multiple keywords
predicate = new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob"));
predicate = new MatchesKeywordsPredicate(Arrays.asList("Alice", "Bob"));
assertTrue(predicate.test(new PersonBuilder().withName("Alice Bob").build()));

// Only one matching keyword
predicate = new NameContainsKeywordsPredicate(Arrays.asList("Bob", "Carol"));
predicate = new MatchesKeywordsPredicate(Arrays.asList("Bob", "Carol"));
assertTrue(predicate.test(new PersonBuilder().withName("Alice Carol").build()));

// Mixed-case keywords
predicate = new NameContainsKeywordsPredicate(Arrays.asList("aLIce", "bOB"));
predicate = new MatchesKeywordsPredicate(Arrays.asList("aLIce", "bOB"));
assertTrue(predicate.test(new PersonBuilder().withName("Alice Bob").build()));

// Multiple keywords (name, phone, email, telegram)
predicate = new MatchesKeywordsPredicate(Arrays.asList("Alice", "54321", "carol@email.com", "@david"));
assertTrue(predicate.test(new PersonBuilder().withName("Alice").withPhone("12345")
.withEmail("alice@email.com").withTelegram("@alice").build()));
assertTrue(predicate.test(new PersonBuilder().withName("Bob").withPhone("54321")
.withEmail("bob@email.com").withTelegram("@bob").build()));
assertTrue(predicate.test(new PersonBuilder().withName("Carol").withPhone("67890")
.withEmail("carol@email.com").withTelegram("@carol").build()));
assertTrue(predicate.test(new PersonBuilder().withName("David").withPhone("09876")
.withEmail("david@email.com").withTelegram("@david").build()));
}

@Test
public void test_nameDoesNotContainKeywords_returnsFalse() {
public void test_doesNotMatchKeywords_returnsFalse() {
// Zero keywords
NameContainsKeywordsPredicate predicate = new NameContainsKeywordsPredicate(Collections.emptyList());
MatchesKeywordsPredicate predicate = new MatchesKeywordsPredicate(Collections.emptyList());
assertFalse(predicate.test(new PersonBuilder().withName("Alice").build()));

// Non-matching keyword
predicate = new NameContainsKeywordsPredicate(Arrays.asList("Carol"));
predicate = new MatchesKeywordsPredicate(Arrays.asList("Carol"));
assertFalse(predicate.test(new PersonBuilder().withName("Alice Bob").build()));

// Keywords match phone, email and address, but does not match name
predicate = new NameContainsKeywordsPredicate(Arrays.asList("12345", "alice@email.com", "Main", "Street"));
assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("12345")
.withEmail("alice@email.com").withTelegram("@aliceinwonderland").build()));
}
}

0 comments on commit ea2a7ce

Please sign in to comment.