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

Add find tag feature to FindCommand #59

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 8 additions & 7 deletions src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@

import static java.util.Objects.requireNonNull;

import java.util.function.Predicate;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.model.Model;
import seedu.address.model.person.NameContainsKeywordsPredicate;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

side note I think predicates can have their own directory for organisation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noted, will do in next iteration

import seedu.address.model.person.Person;

/**
* Finds and lists all persons in address book whose name contains any of the argument keywords.
* Finds and lists all persons in address book whose name or tags contain any of the argument keywords.
* Keyword matching is case insensitive.
*/
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 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";
+ "Example: " + COMMAND_WORD + " alice bob charlie (for names) OR find t/diabetic t/G6PD-Def";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there might be a better way to display this usage message but small issue


private final NameContainsKeywordsPredicate predicate;
private final Predicate<Person> predicate;

public FindCommand(NameContainsKeywordsPredicate predicate) {
public FindCommand(Predicate<Person> predicate) {
this.predicate = predicate;
}

Expand All @@ -40,7 +42,6 @@ public boolean equals(Object other) {
return true;
}

// instanceof handles nulls
if (!(other instanceof FindCommand)) {
return false;
}
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/seedu/address/logic/parser/FindCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,45 @@
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

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

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

/**
* Parses input arguments and creates a new FindCommand object
* Parses input arguments and creates a new FindCommand object.
*/
public class FindCommandParser implements Parser<FindCommand> {

/**
* Parses the given {@code String} of arguments in the context of the FindCommand
* and returns a FindCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*
* @throws ParseException if the user input does not conform to the expected format.
*/
public FindCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim();

// If the user provides a tag search prefix (e.g., "t/diabetic"), search by tags
if (trimmedArgs.startsWith("t/")) {
String tagKeywords = trimmedArgs.replace("t/", "").trim();

if (tagKeywords.isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}
List<String> tagKeywordList = Arrays.asList(tagKeywords.split("\\s+"));
return new FindCommand(new TagContainsKeywordsPredicate(tagKeywordList));
}

// Otherwise, search by name
if (trimmedArgs.isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}

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

return new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
List<String> nameKeywords = Arrays.asList(trimmedArgs.split("\\s+"));
return new FindCommand(new NameContainsKeywordsPredicate(nameKeywords));
}

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

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

/**
* Tests that a {@code Person}'s {@code Tag} matches any of the keywords given.
*/
public class TagContainsKeywordsPredicate implements Predicate<Person> {
private final List<String> keywords;

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

@Override
public boolean test(Person person) {
return keywords.stream()
.anyMatch(keyword -> person.getTags().stream()
.anyMatch(tag -> tag.tagName.equalsIgnoreCase(keyword)));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof TagContainsKeywordsPredicate // instanceof handles nulls
&& keywords.equals(((TagContainsKeywordsPredicate) other).keywords)); // state check
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
"nric": "S1234567E",
"address" : "michegan ave",
"remark" : "",
"tags" : [ ]
"tags" : [ "Diabetic", "G6PD" ]
}, {
"name" : "Fiona Kunz",
"phone" : "9482427",
"email" : "[email protected]",
"nric": "S1234567F",
"address" : "little tokyo",
"remark" : "",
"tags" : [ ]
"tags" : [ "Diabetic", "G6PD" ]
}, {
"name" : "George Best",
"phone" : "9482442",
Expand Down
67 changes: 47 additions & 20 deletions src/test/java/seedu/address/logic/commands/FindCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.TagContainsKeywordsPredicate;

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

@Test
public void equals() {
NameContainsKeywordsPredicate firstPredicate =
NameContainsKeywordsPredicate firstNamePredicate =
new NameContainsKeywordsPredicate(Collections.singletonList("first"));
NameContainsKeywordsPredicate secondPredicate =
NameContainsKeywordsPredicate secondNamePredicate =
new NameContainsKeywordsPredicate(Collections.singletonList("second"));
TagContainsKeywordsPredicate firstTagPredicate =
new TagContainsKeywordsPredicate(Collections.singletonList("diabetic"));
TagContainsKeywordsPredicate secondTagPredicate =
new TagContainsKeywordsPredicate(Collections.singletonList("allergy"));

FindCommand findFirstCommand = new FindCommand(firstPredicate);
FindCommand findSecondCommand = new FindCommand(secondPredicate);
FindCommand findFirstNameCommand = new FindCommand(firstNamePredicate);
FindCommand findSecondNameCommand = new FindCommand(secondNamePredicate);
FindCommand findFirstTagCommand = new FindCommand(firstTagPredicate);
FindCommand findSecondTagCommand = new FindCommand(secondTagPredicate);

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

// same values -> returns true
FindCommand findFirstCommandCopy = new FindCommand(firstPredicate);
assertTrue(findFirstCommand.equals(findFirstCommandCopy));
FindCommand findFirstNameCommandCopy = new FindCommand(firstNamePredicate);
assertTrue(findFirstNameCommand.equals(findFirstNameCommandCopy));

// different types -> returns false
assertFalse(findFirstCommand.equals(1));
assertFalse(findFirstNameCommand.equals(1));

// null -> returns false
assertFalse(findFirstCommand.equals(null));
assertFalse(findFirstNameCommand.equals(null));

// different person -> returns false
assertFalse(findFirstCommand.equals(findSecondCommand));
assertFalse(findFirstNameCommand.equals(findSecondNameCommand));

// different predicate type -> returns false
assertFalse(findFirstNameCommand.equals(findFirstTagCommand));
}

@Test
public void execute_zeroKeywords_noPersonFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0);
NameContainsKeywordsPredicate predicate = preparePredicate(" ");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
NameContainsKeywordsPredicate namePredicate = prepareNamePredicate(" ");
FindCommand command = new FindCommand(namePredicate);
expectedModel.updateFilteredPersonList(namePredicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getFilteredPersonList());
}

@Test
public void execute_multipleKeywords_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3);
NameContainsKeywordsPredicate predicate = preparePredicate("Kurz Elle Kunz");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
NameContainsKeywordsPredicate namePredicate = prepareNamePredicate("Kurz Elle Kunz");
FindCommand command = new FindCommand(namePredicate);
expectedModel.updateFilteredPersonList(namePredicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(CARL, ELLE, FIONA), model.getFilteredPersonList());
}

@Test
public void execute_tagSearch_personsWithTagsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 2);
TagContainsKeywordsPredicate tagPredicate = prepareTagPredicate("Diabetic G6PD");
FindCommand command = new FindCommand(tagPredicate);
expectedModel.updateFilteredPersonList(tagPredicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(ELLE, FIONA), model.getFilteredPersonList());
}

@Test
public void toStringMethod() {
NameContainsKeywordsPredicate predicate = new NameContainsKeywordsPredicate(Arrays.asList("keyword"));
FindCommand findCommand = new FindCommand(predicate);
String expected = FindCommand.class.getCanonicalName() + "{predicate=" + predicate + "}";
NameContainsKeywordsPredicate namePredicate = new NameContainsKeywordsPredicate(Arrays.asList("keyword"));
FindCommand findCommand = new FindCommand(namePredicate);
String expected = FindCommand.class.getCanonicalName() + "{predicate=" + namePredicate + "}";
assertEquals(expected, findCommand.toString());
}

/**
* Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}.
*/
private NameContainsKeywordsPredicate preparePredicate(String userInput) {
private NameContainsKeywordsPredicate prepareNamePredicate(String userInput) {
return new NameContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+")));
}

/**
* Parses {@code userInput} into a {@code TagContainsKeywordsPredicate}.
*/
private TagContainsKeywordsPredicate prepareTagPredicate(String userInput) {
return new TagContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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

public class FindCommandParserTest {

Expand All @@ -21,7 +22,7 @@ public void parse_emptyArg_throwsParseException() {
}

@Test
public void parse_validArgs_returnsFindCommand() {
public void parse_validNameArgs_returnsFindCommand() {
// no leading and trailing whitespaces
FindCommand expectedFindCommand =
new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob")));
Expand All @@ -31,4 +32,20 @@ public void parse_validArgs_returnsFindCommand() {
assertParseSuccess(parser, " \n Alice \n \t Bob \t", expectedFindCommand);
}

@Test
public void parse_validTagArgs_returnsFindCommand() {
// Test valid tag-based search
FindCommand expectedFindCommand =
new FindCommand(new TagContainsKeywordsPredicate(Arrays.asList("Diabetic", "G6PD")));
assertParseSuccess(parser, "t/Diabetic t/G6PD", expectedFindCommand);

// multiple whitespaces between tag keywords
assertParseSuccess(parser, " \n t/Diabetic \n \t t/G6PD \t", expectedFindCommand);
}

@Test
public void parse_invalidArgs_throwsParseException() {
// Test invalid argument (e.g., empty string)
assertParseFailure(parser, "t/", String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}
}
2 changes: 2 additions & 0 deletions src/test/java/seedu/address/testutil/TypicalPersons.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class TypicalPersons {
.withNric("S1234567E")
.withEmail("[email protected]")
.withAddress("michegan ave")
.withTags("Diabetic", "G6PD")
.withAppointment("10-11-2024 05:31")
.build();
public static final Person FIONA = new PersonBuilder()
Expand All @@ -76,6 +77,7 @@ public class TypicalPersons {
.withNric("S1234567F")
.withEmail("[email protected]")
.withAddress("little tokyo")
.withTags("Diabetic", "G6PD")
.withAppointment("08-11-2024 13:22")
.build();
public static final Person GEORGE = new PersonBuilder()
Expand Down