forked from nus-cs2103-AY2223S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nus-cs2103-AY2223S2#70 from hongshenggg/master
Implement find command to find by prefix and keyword pairs
- Loading branch information
Showing
23 changed files
with
322 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,38 +2,35 @@ | |
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.NameContainsKeywordsPredicate; | ||
import seedu.address.model.person.PhoneContainsKeywordsPredicate; | ||
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 details contains any of the | ||
* argument keywords based on the prefixes in the user input. | ||
* 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 " | ||
+ "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"; | ||
|
||
public static final String MESSAGE_USAGE_2 = COMMAND_WORD + ": Finds all persons whose phone numbers contain any " | ||
+ "of the specified keywords / phone number substring and displays them as a list with index numbers.\n" | ||
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n" | ||
+ "Example: " + COMMAND_WORD + " 99999999"; | ||
|
||
private NameContainsKeywordsPredicate predicate; | ||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Finds all persons whose details contain any of " | ||
+ "the specified keywords (case-insensitive) based on " | ||
+ "the prefix provided and displays them as a list with index numbers.\n" | ||
+ "Each prefix must be followed by one and only one keyword.\n" | ||
+ "Please use the \"help\" command for more information on " | ||
+ "the usage of this command.\n" | ||
+ "Parameters: [PREFIX]/KEYWORD [MORE [PREFIX]/KEYWORD]...\n" | ||
+ "Example: " + COMMAND_WORD + " n/alice s/y4 p/91234567" | ||
+ " e/[email protected] a/blk 123 t/cs2103"; | ||
|
||
private PhoneContainsKeywordsPredicate phonePredicate; | ||
|
||
public FindCommand(PhoneContainsKeywordsPredicate phonePredicate) { | ||
this.phonePredicate = phonePredicate; | ||
} | ||
private Predicate<Person> predicate; | ||
|
||
public FindCommand(NameContainsKeywordsPredicate predicate) { | ||
public FindCommand(Predicate<Person> predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 69 additions & 25 deletions
94
src/main/java/seedu/address/logic/parser/FindCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,98 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_STATUS; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import seedu.address.logic.commands.FindCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.person.NameContainsKeywordsPredicate; | ||
import seedu.address.model.person.PhoneContainsKeywordsPredicate; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.person.predicates.AddressContainsKeywordPredicate; | ||
import seedu.address.model.person.predicates.EmailContainsKeywordPredicate; | ||
import seedu.address.model.person.predicates.NameContainsKeywordPredicate; | ||
import seedu.address.model.person.predicates.PhoneContainsKeywordsPredicate; | ||
import seedu.address.model.person.predicates.StatusContainsKeywordsPredicate; | ||
import seedu.address.model.person.predicates.TagContainsKeywordsPredicate; | ||
|
||
/** | ||
* Parses input arguments and creates a new FindCommand object | ||
*/ | ||
public class FindCommandParser implements Parser<FindCommand> { | ||
|
||
private Prefix[] possiblePrefixes = { | ||
PREFIX_NAME, | ||
PREFIX_STATUS, | ||
PREFIX_PHONE, | ||
PREFIX_EMAIL, | ||
PREFIX_ADDRESS, | ||
PREFIX_TAG | ||
}; | ||
|
||
/** | ||
* 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 | ||
*/ | ||
public FindCommand parse(String args) throws ParseException { | ||
String trimmedArgs = args.trim(); | ||
Boolean isNumber = onlyDigits(trimmedArgs); | ||
if (trimmedArgs.isEmpty()) { | ||
if (isNumber) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE)); | ||
} else { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE)); | ||
} | ||
} | ||
|
||
if (isNumber) { | ||
return new FindCommand(new PhoneContainsKeywordsPredicate(trimmedArgs)); | ||
} else { | ||
String[] nameKeywords = trimmedArgs.split("\\s+"); | ||
if (args.isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE)); | ||
} | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, possiblePrefixes); | ||
|
||
return new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords))); | ||
Predicate<Person> predicate = null; | ||
for (Prefix p : possiblePrefixes) { | ||
List<String> prefixArguments = argMultimap.getAllValues(p); | ||
if (prefixArguments.isEmpty()) { | ||
continue; | ||
} | ||
for (String arg : prefixArguments) { | ||
if (!isValidArgument(arg)) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE)); | ||
} | ||
Predicate<Person> currentPredicate = null; | ||
if (p == PREFIX_NAME) { | ||
currentPredicate = new NameContainsKeywordPredicate(arg); | ||
} else if (p == PREFIX_STATUS) { | ||
currentPredicate = new StatusContainsKeywordsPredicate(arg); | ||
} else if (p == PREFIX_PHONE) { | ||
currentPredicate = new PhoneContainsKeywordsPredicate(arg); | ||
} else if (p == PREFIX_EMAIL) { | ||
currentPredicate = new EmailContainsKeywordPredicate(arg); | ||
} else if (p == PREFIX_ADDRESS) { | ||
currentPredicate = new AddressContainsKeywordPredicate(arg); | ||
} else if (p == PREFIX_TAG) { | ||
currentPredicate = new TagContainsKeywordsPredicate(arg); | ||
} | ||
assert currentPredicate != null; | ||
if (predicate == null) { | ||
predicate = currentPredicate; | ||
} else { | ||
predicate = predicate.and(currentPredicate); | ||
} | ||
} | ||
} | ||
if (predicate == null) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
return new FindCommand(predicate); | ||
} | ||
|
||
private boolean onlyDigits(String str) { | ||
for (int i = 0; i < str.length(); i++) { | ||
if (!Character.isDigit(str.charAt(i))) { | ||
return false; | ||
} | ||
private static boolean isValidArgument(String argument) { | ||
String preppedWord = argument.trim().toLowerCase(); | ||
if (preppedWord.isEmpty() || preppedWord.split("\\s+").length != 1) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
} |
31 changes: 0 additions & 31 deletions
31
src/main/java/seedu/address/model/person/NameContainsKeywordsPredicate.java
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
src/main/java/seedu/address/model/person/predicates/AddressContainsKeywordPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package seedu.address.model.person.predicates; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.StringUtil; | ||
import seedu.address.model.person.Person; | ||
|
||
/** | ||
* Tests that a {@code Person}'s {@code Address} matches any of the keywords given. | ||
*/ | ||
public class AddressContainsKeywordPredicate implements Predicate<Person> { | ||
private final String keyword; | ||
|
||
public AddressContainsKeywordPredicate(String keyword) { | ||
this.keyword = keyword; | ||
} | ||
|
||
@Override | ||
public boolean test(Person person) { | ||
return StringUtil.containsWordIgnoreCase(person.getAddress().toString(), keyword); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof AddressContainsKeywordPredicate // instanceof handles nulls | ||
&& keyword.equals(((AddressContainsKeywordPredicate) other).keyword)); // state check | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/seedu/address/model/person/predicates/EmailContainsKeywordPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package seedu.address.model.person.predicates; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.StringUtil; | ||
import seedu.address.model.person.Person; | ||
|
||
/** | ||
* Tests that a {@code Person}'s {@code Email} matches any of the keywords given. | ||
*/ | ||
public class EmailContainsKeywordPredicate implements Predicate<Person> { | ||
private final String keyword; | ||
|
||
public EmailContainsKeywordPredicate(String keyword) { | ||
this.keyword = keyword; | ||
} | ||
|
||
@Override | ||
public boolean test(Person person) { | ||
return StringUtil.containsWordIgnoreCase(person.getEmail().toString(), keyword); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof EmailContainsKeywordPredicate // instanceof handles nulls | ||
&& keyword.equals(((EmailContainsKeywordPredicate) other).keyword)); // state check | ||
} | ||
|
||
} |
Oops, something went wrong.