Skip to content

Commit 0f45a22

Browse files
authored
Merge pull request nus-cs2103-AY2021S1#79 from chrystalquek/update-find-command-person
Allow for search of contacts by both Name and Description
2 parents 125daf0 + ff26e39 commit 0f45a22

File tree

18 files changed

+220
-179
lines changed

18 files changed

+220
-179
lines changed

src/main/java/seedu/address/logic/commands/person/FindCommand.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import seedu.address.commons.core.Messages;
66
import seedu.address.logic.commands.CommandResult;
77
import seedu.address.model.person.ModelPerson;
8-
import seedu.address.model.person.person.NameContainsKeywordsPredicate;
8+
import seedu.address.model.person.person.NameDescriptionContainsKeywordsPredicate;
99

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

1616
public static final String COMMAND_WORD = "find";
1717

18-
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names contain any of "
18+
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names and/or "
19+
+ "descriptions contain any of "
1920
+ "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n"
2021
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n"
21-
+ "Example: " + COMMAND_WORD + " alice bob charlie";
22+
+ "Example: " + COMMAND_WORD + " alice bob user";
2223

23-
private final NameContainsKeywordsPredicate predicate;
24+
private final NameDescriptionContainsKeywordsPredicate predicate;
2425

25-
public FindCommand(NameContainsKeywordsPredicate predicate) {
26+
public FindCommand(NameDescriptionContainsKeywordsPredicate predicate) {
2627
this.predicate = predicate;
2728
}
2829

src/main/java/seedu/address/logic/parser/person/FindCommandParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import seedu.address.logic.commands.person.FindCommand;
88
import seedu.address.logic.parser.Parser;
99
import seedu.address.logic.parser.exceptions.ParseException;
10-
import seedu.address.model.person.person.NameContainsKeywordsPredicate;
10+
import seedu.address.model.person.person.NameDescriptionContainsKeywordsPredicate;
1111

1212
/**
1313
* Parses input arguments and creates a new FindCommand object
@@ -28,7 +28,7 @@ public FindCommand parse(String args) throws ParseException {
2828

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

31-
return new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
31+
return new FindCommand(new NameDescriptionContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
3232
}
3333

3434
}

src/main/java/seedu/address/logic/parser/person/ParserUtil.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,6 @@ public static Phone parsePhone(Optional<String> phone) throws ParseException {
6565
return new Phone(trimmedPhone);
6666
}
6767

68-
/**
69-
* Parses a {@code String phone} into a {@code Phone}.
70-
* Leading and trailing whitespaces will be trimmed.
71-
*
72-
* @throws ParseException if the given {@code phone} is invalid.
73-
*/
74-
public static Phone parsePhone(String phone) throws ParseException {
75-
return parsePhone(Optional.of(phone));
76-
}
77-
7868
/**
7969
* Parses a {@code String email} into an {@code Email}.
8070
* Leading and trailing whitespaces will be trimmed.

src/main/java/seedu/address/model/person/person/NameContainsKeywordsPredicate.java renamed to src/main/java/seedu/address/model/person/person/NameDescriptionContainsKeywordsPredicate.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,25 @@
88
/**
99
* Tests that a {@code Person}'s {@code Name} matches any of the keywords given.
1010
*/
11-
public class NameContainsKeywordsPredicate implements Predicate<Person> {
11+
public class NameDescriptionContainsKeywordsPredicate implements Predicate<Person> {
1212
private final List<String> keywords;
1313

14-
public NameContainsKeywordsPredicate(List<String> keywords) {
14+
public NameDescriptionContainsKeywordsPredicate(List<String> keywords) {
1515
this.keywords = keywords;
1616
}
1717

1818
@Override
1919
public boolean test(Person person) {
2020
return keywords.stream()
21-
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getName().fullName, keyword));
21+
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getName().fullName, keyword)
22+
|| StringUtil.containsWordIgnoreCase(person.getDescription().value.orElse(""), keyword));
2223
}
2324

2425
@Override
2526
public boolean equals(Object other) {
2627
return other == this // short circuit if same object
27-
|| (other instanceof NameContainsKeywordsPredicate // instanceof handles nulls
28-
&& keywords.equals(((NameContainsKeywordsPredicate) other).keywords)); // state check
28+
|| (other instanceof NameDescriptionContainsKeywordsPredicate // instanceof handles nulls
29+
&& keywords.equals(((NameDescriptionContainsKeywordsPredicate) other).keywords)); // state check
2930
}
3031

3132
}

src/main/java/seedu/address/model/util/SampleDataUtil.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package seedu.address.model.util;
22

3+
import java.util.Optional;
4+
35
import seedu.address.model.deliverable.DeliverableBook;
46
import seedu.address.model.deliverable.ReadOnlyDeliverableBook;
57
import seedu.address.model.deliverable.deliverable.Deadline;
@@ -28,13 +30,13 @@ public static Person[] getSamplePersons() {
2830
new Person(new Name("Alex Yeoh"), new Phone("87438807"), new Email("[email protected]"),
2931
Role.getRole("stk"), new OptionalDescription("End user")),
3032
new Person(new Name("Bernice Yu"), new Phone("99272758"), new Email("[email protected]"),
31-
Role.getRole("stk"), new OptionalDescription("End user")),
33+
Role.getRole("dev"), new OptionalDescription("Frontend Engineer")),
3234
new Person(new Name("Charlotte Oliveiro"), new Phone("93210283"), new Email("[email protected]"),
33-
Role.getRole("stk"), new OptionalDescription("End user")),
34-
new Person(new Name("David Li"), new Phone("91031282"), new Email("[email protected]"),
35-
Role.getRole("stk"), new OptionalDescription("End user")),
36-
new Person(new Name("Irfan Ibrahim"), new Phone("92492021"), new Email("[email protected]"),
37-
Role.getRole("stk"), new OptionalDescription("End user")),
35+
Role.getRole("stk"), new OptionalDescription("Business analyst")),
36+
new Person(new Name("David Li"), new Phone(Optional.empty()), new Email("[email protected]"),
37+
Role.getRole("dev"), new OptionalDescription("Backend Engineer")),
38+
new Person(new Name("Irfan Ibrahim"), new Phone(Optional.empty()), new Email("[email protected]"),
39+
Role.getRole("stk"), new OptionalDescription(Optional.empty())),
3840
new Person(new Name("Roy Balakrishnan"), new Phone("92624417"), new Email("[email protected]"),
3941
Role.getRole("stk"), new OptionalDescription("End user"))
4042
};

src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
"value": "98765432"
1717
},
1818
"email" : "[email protected]",
19-
"role": "stk",
19+
"role": "dev",
2020
"description": {
21-
"value" : "Admin personnel"
21+
"value" : "Frontend engineer"
2222
}
2323
}, {
2424
"name" : "Carl Kurz",
@@ -28,22 +28,22 @@
2828
"email" : "[email protected]",
2929
"role": "stk",
3030
"description": {
31-
"value" : null
31+
"value" : "Business analyst"
3232
}
3333
}, {
3434
"name" : "Daniel Meier",
3535
"phone" : {
36-
"value": "87652533"
36+
"value": null
3737
},
3838
"email" : "[email protected]",
39-
"role": "stk",
39+
"role": "dev",
4040
"description": {
41-
"value" : null
41+
"value" : "Backend engineer"
4242
}
4343
}, {
4444
"name" : "Elle Meyer",
4545
"phone" : {
46-
"value": "9482224"
46+
"value": null
4747
},
4848
"email" : "[email protected]",
4949
"role": "stk",
@@ -58,17 +58,17 @@
5858
"email" : "[email protected]",
5959
"role": "stk",
6060
"description": {
61-
"value" : null
61+
"value" : "Product designer, Kurz work partner"
6262
}
6363
}, {
6464
"name" : "George Best",
6565
"phone" : {
66-
"value": "9482442"
66+
"value": null
6767
},
6868
"email" : "[email protected]",
6969
"role": "stk",
7070
"description": {
71-
"value" : null
71+
"value" : "Secondary user"
7272
}
7373
} ]
7474
}

src/test/java/seedu/address/logic/LogicPersonManagerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import static seedu.address.logic.commands.person.CommandTestUtil.EMAIL_DESC_AMY;
88
import static seedu.address.logic.commands.person.CommandTestUtil.NAME_DESC_AMY;
99
import static seedu.address.logic.commands.person.CommandTestUtil.PHONE_DESC_AMY;
10-
import static seedu.address.logic.commands.person.CommandTestUtil.ROLE_DESC_STK;
10+
import static seedu.address.logic.commands.person.CommandTestUtil.ROLE_DESC_AMY;
1111
import static seedu.address.testutil.Assert.assertThrows;
1212
import static seedu.address.testutil.TypicalPersons.AMY;
1313

@@ -81,7 +81,7 @@ public void execute_storageThrowsIoException_throwsCommandException() {
8181

8282
// Execute add command
8383
String addCommand =
84-
AddCommand.COMMAND_WORD + ROLE_DESC_STK + NAME_DESC_AMY + PHONE_DESC_AMY + EMAIL_DESC_AMY
84+
AddCommand.COMMAND_WORD + ROLE_DESC_AMY + NAME_DESC_AMY + PHONE_DESC_AMY + EMAIL_DESC_AMY
8585
+ DESCRIPTION_DESC_AMY;
8686
Person expectedPerson = new PersonBuilder(AMY).build();
8787
ModelPersonManager expectedModel = new ModelPersonManager();

src/test/java/seedu/address/logic/commands/person/CommandTestUtil.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import seedu.address.logic.commands.exceptions.CommandException;
1919
import seedu.address.model.person.AddressBook;
2020
import seedu.address.model.person.ModelPerson;
21-
import seedu.address.model.person.person.NameContainsKeywordsPredicate;
21+
import seedu.address.model.person.person.NameDescriptionContainsKeywordsPredicate;
2222
import seedu.address.model.person.person.Person;
2323
import seedu.address.testutil.EditPersonDescriptorBuilder;
2424

@@ -33,19 +33,19 @@ public class CommandTestUtil {
3333
public static final String VALID_PHONE_BOB = "22222222";
3434
public static final String VALID_EMAIL_AMY = "[email protected]";
3535
public static final String VALID_EMAIL_BOB = "[email protected]";
36-
public static final String VALID_ROLE_DEV = "dev";
37-
public static final String VALID_ROLE_STK = "stk";
36+
public static final String VALID_ROLE_AMY = "stk";
37+
public static final String VALID_ROLE_BOB = "dev";
3838
public static final String VALID_DESCRIPTION_AMY = "End user";
39-
public static final String VALID_DESCRIPTION_BOB = "Admin personnel";
39+
public static final String VALID_DESCRIPTION_BOB = "Frontend engineer";
4040

4141
public static final String NAME_DESC_AMY = " " + PREFIX_NAME + VALID_NAME_AMY;
4242
public static final String NAME_DESC_BOB = " " + PREFIX_NAME + VALID_NAME_BOB;
4343
public static final String PHONE_DESC_AMY = " " + PREFIX_PHONE + VALID_PHONE_AMY;
4444
public static final String PHONE_DESC_BOB = " " + PREFIX_PHONE + VALID_PHONE_BOB;
4545
public static final String EMAIL_DESC_AMY = " " + PREFIX_EMAIL + VALID_EMAIL_AMY;
4646
public static final String EMAIL_DESC_BOB = " " + PREFIX_EMAIL + VALID_EMAIL_BOB;
47-
public static final String ROLE_DESC_DEV = " " + PREFIX_ROLE + VALID_ROLE_DEV;
48-
public static final String ROLE_DESC_STK = " " + PREFIX_ROLE + VALID_ROLE_STK;
47+
public static final String ROLE_DESC_AMY = " " + PREFIX_ROLE + VALID_ROLE_AMY;
48+
public static final String ROLE_DESC_BOB = " " + PREFIX_ROLE + VALID_ROLE_BOB;
4949
public static final String DESCRIPTION_DESC_AMY = " " + PREFIX_DESCRIPTION + VALID_DESCRIPTION_AMY;
5050
public static final String DESCRIPTION_DESC_BOB = " " + PREFIX_DESCRIPTION + VALID_DESCRIPTION_BOB;
5151

@@ -121,7 +121,7 @@ public static void showPersonAtIndex(ModelPerson modelPerson, Index targetIndex)
121121

122122
Person person = modelPerson.getFilteredPersonList().get(targetIndex.getZeroBased());
123123
final String[] splitName = person.getName().fullName.split("\\s+");
124-
modelPerson.updateFilteredPersonList(new NameContainsKeywordsPredicate(Arrays.asList(splitName[0])));
124+
modelPerson.updateFilteredPersonList(new NameDescriptionContainsKeywordsPredicate(Arrays.asList(splitName[0])));
125125

126126
assertEquals(1, modelPerson.getFilteredPersonList().size());
127127
}

src/test/java/seedu/address/logic/commands/person/FindCommandTest.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import static org.junit.jupiter.api.Assertions.assertTrue;
66
import static seedu.address.commons.core.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW;
77
import static seedu.address.logic.commands.person.CommandTestUtil.assertCommandSuccess;
8+
import static seedu.address.testutil.TypicalPersons.ALICE;
89
import static seedu.address.testutil.TypicalPersons.CARL;
910
import static seedu.address.testutil.TypicalPersons.ELLE;
1011
import static seedu.address.testutil.TypicalPersons.FIONA;
12+
import static seedu.address.testutil.TypicalPersons.GEORGE;
1113
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;
1214

1315
import java.util.Arrays;
@@ -18,7 +20,7 @@
1820
import seedu.address.model.UserPrefs;
1921
import seedu.address.model.person.ModelPerson;
2022
import seedu.address.model.person.ModelPersonManager;
21-
import seedu.address.model.person.person.NameContainsKeywordsPredicate;
23+
import seedu.address.model.person.person.NameDescriptionContainsKeywordsPredicate;
2224

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

3032
@Test
3133
public void equals() {
32-
NameContainsKeywordsPredicate firstPredicate =
33-
new NameContainsKeywordsPredicate(Collections.singletonList("first"));
34-
NameContainsKeywordsPredicate secondPredicate =
35-
new NameContainsKeywordsPredicate(Collections.singletonList("second"));
34+
NameDescriptionContainsKeywordsPredicate firstPredicate =
35+
new NameDescriptionContainsKeywordsPredicate(Collections.singletonList("first"));
36+
NameDescriptionContainsKeywordsPredicate secondPredicate =
37+
new NameDescriptionContainsKeywordsPredicate(Collections.singletonList("second"));
3638

3739
FindCommand findFirstCommand = new FindCommand(firstPredicate);
3840
FindCommand findSecondCommand = new FindCommand(secondPredicate);
@@ -57,27 +59,48 @@ public void equals() {
5759
@Test
5860
public void execute_zeroKeywords_noPersonFound() {
5961
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0);
60-
NameContainsKeywordsPredicate predicate = preparePredicate(" ");
62+
NameDescriptionContainsKeywordsPredicate predicate = preparePredicate(" ");
6163
FindCommand command = new FindCommand(predicate);
6264
expectedModelPerson.updateFilteredPersonList(predicate);
6365
assertCommandSuccess(command, modelPerson, expectedMessage, expectedModelPerson);
6466
assertEquals(Collections.emptyList(), modelPerson.getFilteredPersonList());
6567
}
6668

6769
@Test
68-
public void execute_multipleKeywords_multiplePersonsFound() {
70+
public void execute_multipleNameKeywords_multiplePersonsFound() {
6971
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3);
70-
NameContainsKeywordsPredicate predicate = preparePredicate("Kurz Elle Kunz");
72+
NameDescriptionContainsKeywordsPredicate predicate = preparePredicate("Kurz Elle Kunz");
7173
FindCommand command = new FindCommand(predicate);
7274
expectedModelPerson.updateFilteredPersonList(predicate);
7375
assertCommandSuccess(command, modelPerson, expectedMessage, expectedModelPerson);
7476
assertEquals(Arrays.asList(CARL, ELLE, FIONA), modelPerson.getFilteredPersonList());
7577
}
7678

79+
@Test
80+
public void execute_multipleDescriptionKeywords_multiplePersonsFound() {
81+
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3);
82+
NameDescriptionContainsKeywordsPredicate predicate = preparePredicate("user business analyst");
83+
FindCommand command = new FindCommand(predicate);
84+
expectedModelPerson.updateFilteredPersonList(predicate);
85+
assertCommandSuccess(command, modelPerson, expectedMessage, expectedModelPerson);
86+
assertEquals(Arrays.asList(ALICE, CARL, GEORGE), modelPerson.getFilteredPersonList());
87+
}
88+
89+
@Test
90+
public void execute_multipleNameDescriptionKeywords_multiplePersonsFound() {
91+
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 4);
92+
NameDescriptionContainsKeywordsPredicate predicate = preparePredicate("Kurz user");
93+
FindCommand command = new FindCommand(predicate);
94+
expectedModelPerson.updateFilteredPersonList(predicate);
95+
assertCommandSuccess(command, modelPerson, expectedMessage, expectedModelPerson);
96+
// Fiona description contains "Kurz"
97+
assertEquals(Arrays.asList(ALICE, CARL, FIONA, GEORGE), modelPerson.getFilteredPersonList());
98+
}
99+
77100
/**
78101
* Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}.
79102
*/
80-
private NameContainsKeywordsPredicate preparePredicate(String userInput) {
81-
return new NameContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+")));
103+
private NameDescriptionContainsKeywordsPredicate preparePredicate(String userInput) {
104+
return new NameDescriptionContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+")));
82105
}
83106
}

0 commit comments

Comments
 (0)