Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find contacts by name, asset or tag #75

Merged
merged 21 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,16 @@ Example: `edita L293D L293E` edits the asset `L293D`, changing its name to `L293

--------------------------------------------------------------------------------------------------------------------

### Search : `search`
### Locating persons by name or asset: `find`

Search contacts or assets by any of their metadata.
Finds persons whose names, tags or assets contain any of the given keywords.

Format: `search <string>`
Format: `find <keyword> [<keyword>]...`

Example: `search John` searches all contacts and assets for the term `John`.
Example: `find John` searches all contact names, tags and assets for the keyword `John`.

* At least one keyword must be provided.
* Keywords are case-insensitive.

--------------------------------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -198,5 +201,5 @@ Action | Format | Examples
**Delete** | `delete <id>` | `delete 1`
**Edit contact** | `edit <id> [--email <email>] [--phone <phone>] [--asset <asset>]...` | `edit 1 --email newemail@example.com`
**Edit asset** | `edita <asset> <new_asset_name>` | `edita L293D L293E`
**Search** | `search <string>` | `search John`
**Find** | ``find <keyword> [<keyword>]...`` | `find John`
**Exit** | `exit` | `exit`
12 changes: 7 additions & 5 deletions src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.model.Model;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.PersonSearchPredicate;

/**
* Finds and lists all persons in address book whose name contains any of the argument keywords.
Expand All @@ -18,14 +18,15 @@ public class FindCommand extends Command {

public static final String COMMAND_WORD = "find";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names contain any of "
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Finds all persons whose names, assets or tags contain any of "
+ "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n"
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n"
+ "Example: " + COMMAND_WORD + " alice bob charlie";

private final NameContainsKeywordsPredicate predicate;
private final PersonSearchPredicate predicate;

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

Expand All @@ -50,7 +51,7 @@ public static FindCommand of(String args) throws IllegalArgumentException {

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

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

@Override
Expand All @@ -74,4 +75,5 @@ public String toString() {
.add("predicate", predicate)
.toString();
}

}

This file was deleted.

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

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

import seedu.address.commons.util.StringUtil;
import seedu.address.commons.util.ToStringBuilder;

/**
* Tests that a {@code Person}'s {@code Name}, {@code Tags} or {@code Assets} matches any of the keywords given.
*/
public class PersonSearchPredicate implements Predicate<Person> {

private final List<String> keywords;

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

@Override
public boolean test(Person person) {
return keywords.stream()
.anyMatch(keyword -> {
boolean nameContainsKeyword =
StringUtil.containsWordIgnoreCase(person.getName().toString(), keyword);
boolean tagsContainKeyword = person.getTags()
.stream()
.anyMatch(tag -> StringUtil.containsWordIgnoreCase(tag.get(), keyword));
boolean assetsContainKeyword = person.getAssets()
.stream()
.anyMatch(asset -> StringUtil.containsWordIgnoreCase(asset.get(), keyword));
return nameContainsKeyword || tagsContainKeyword || assetsContainKeyword;
});
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof PersonSearchPredicate)) {
return false;
}

PersonSearchPredicate otherPersonSearchPredicate = (PersonSearchPredicate) other;
return keywords.equals(otherPersonSearchPredicate.keywords);
}

@Override
public String toString() {
return new ToStringBuilder(this).add("keywords", keywords).toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
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.Person;
import seedu.address.model.person.PersonSearchPredicate;
import seedu.address.testutil.EditPersonDescriptorBuilder;

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

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

assertEquals(1, model.getFilteredPersonList().size());
}
Expand Down
40 changes: 21 additions & 19 deletions src/test/java/seedu/address/logic/commands/FindCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
Expand All @@ -13,39 +13,40 @@
import static seedu.address.testutil.TypicalPersons.FIONA;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.Test;

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.PersonSearchPredicate;

/**
* Contains integration tests (interaction with the Model) for {@code FindCommand}.
*/
public class FindCommandTest {
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());

private final Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private final Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());

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

FindCommand findFirstCommand = new FindCommand(firstPredicate);
FindCommand findSecondCommand = new FindCommand(secondPredicate);

// same object -> returns true
assertTrue(findFirstCommand.equals(findFirstCommand));
assertEquals(findFirstCommand, findFirstCommand);

// same values -> returns true
FindCommand findFirstCommandCopy = new FindCommand(firstPredicate);
assertTrue(findFirstCommand.equals(findFirstCommandCopy));
assertEquals(findFirstCommand, findFirstCommandCopy);

// different types -> returns false
assertFalse(findFirstCommand.equals(1));
Expand All @@ -54,13 +55,13 @@ public void equals() {
assertFalse(findFirstCommand.equals(null));

// different person -> returns false
assertFalse(findFirstCommand.equals(findSecondCommand));
assertNotEquals(findFirstCommand, findSecondCommand);
}

@Test
public void execute_zeroKeywords_noPersonFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0);
NameContainsKeywordsPredicate predicate = preparePredicate(" ");
PersonSearchPredicate predicate = preparePredicate(" ");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
Expand All @@ -70,11 +71,11 @@ 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");
PersonSearchPredicate predicate = preparePredicate("Kurz Elle Kunz");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(CARL, ELLE, FIONA), model.getFilteredPersonList());
assertEquals(List.of(CARL, ELLE, FIONA), model.getFilteredPersonList());
}

@Test
Expand All @@ -87,7 +88,7 @@ public void of_emptyArg_throwsParseException() {
public void of_validArgs_returnsFindCommand() {
// no leading and trailing whitespaces
FindCommand expectedFindCommand =
new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob")));
new FindCommand(new PersonSearchPredicate(List.of("Alice", "Bob")));
assertParseSuccess(FindCommand::of, "Alice Bob", expectedFindCommand);

// multiple whitespaces between keywords
Expand All @@ -96,7 +97,7 @@ public void of_validArgs_returnsFindCommand() {

@Test
public void toStringMethod() {
NameContainsKeywordsPredicate predicate = new NameContainsKeywordsPredicate(Arrays.asList("keyword"));
PersonSearchPredicate predicate = new PersonSearchPredicate(List.of("keyword"));
FindCommand findCommand = new FindCommand(predicate);
String expected = FindCommand.class.getCanonicalName() + "{predicate=" + predicate + "}";
assertEquals(expected, findCommand.toString());
Expand All @@ -105,7 +106,8 @@ public void toStringMethod() {
/**
* Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}.
*/
private NameContainsKeywordsPredicate preparePredicate(String userInput) {
return new NameContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+")));
private PersonSearchPredicate preparePredicate(String userInput) {
return new PersonSearchPredicate(List.of(userInput.split("\\s+")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.util.exceptions.ParseException;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.Person;
import seedu.address.model.person.PersonSearchPredicate;
import seedu.address.testutil.EditPersonDescriptorBuilder;
import seedu.address.testutil.PersonBuilder;
import seedu.address.testutil.PersonUtil;
Expand Down Expand Up @@ -73,7 +73,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 PersonSearchPredicate(keywords)), command);
}

@Test
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.PersonSearchPredicate;
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().toString().split("\\s+");
modelManager.updateFilteredPersonList(new NameContainsKeywordsPredicate(Arrays.asList(keywords)));
modelManager.updateFilteredPersonList(new PersonSearchPredicate(Arrays.asList(keywords)));
assertFalse(modelManager.equals(new ModelManager(addressBook, userPrefs)));

// resets modelManager to initial state for upcoming tests
Expand Down
Loading