forked from nus-cs2103-AY2425S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #47 from yhanyi/feature/find-by-name
Add Feature: Find By (Student) Name
- Loading branch information
Showing
6 changed files
with
75 additions
and
45 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
19 changes: 19 additions & 0 deletions
19
src/main/java/seedu/address/logic/commands/FindNameCommand.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,19 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import seedu.address.model.person.NameContainsKeywordsPredicate; | ||
|
||
/** | ||
* Finds and lists all persons in address book whose name contains any of the argument keywords. | ||
* Keyword matching is case insensitive. | ||
*/ | ||
public class FindNameCommand extends FindCommand { | ||
|
||
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: n/KEYWORD [n/MORE_KEYWORDS]...\n" | ||
+ "Example: " + COMMAND_WORD + " n/alice n/bob n/charlie"; | ||
|
||
public FindNameCommand(NameContainsKeywordsPredicate predicate) { | ||
super(predicate); | ||
} | ||
} |
34 changes: 24 additions & 10 deletions
34
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,33 +1,47 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.FindCommand; | ||
import seedu.address.logic.commands.FindNameCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.person.NameContainsKeywordsPredicate; | ||
|
||
/** | ||
* Parses input arguments and creates a new FindCommand object | ||
* Parses input arguments and creates a new FindNameCommand 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. | ||
* Parses the given {@code String} of arguments in the context of the FindNameCommand | ||
* and returns a FindNameCommand 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(); | ||
if (trimmedArgs.isEmpty()) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE)); | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME); | ||
|
||
if (!argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindNameCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_NAME)) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindNameCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
String[] nameKeywords = trimmedArgs.split("\\s+"); | ||
List<String> nameKeywords = argMultimap.getAllValues(PREFIX_NAME); | ||
|
||
return new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords))); | ||
return new FindNameCommand(new NameContainsKeywordsPredicate(nameKeywords)); | ||
} | ||
|
||
/** | ||
* Returns true if none of the prefixes contains empty {@code Optional} values in the given | ||
* {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
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