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.
Resolved merge conflict. Add FindClientTypeCommand and the relevant classes
- Loading branch information
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/main/java/seedu/address/logic/commands/FindClientTypeCommand.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,49 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.ClientTypeContainsKeywordsPredicate; | ||
|
||
/** | ||
* Finds and lists all persons in address book whose client_type contains any of the argument keywords. | ||
* Keyword matching is case insensitive. | ||
*/ | ||
public class FindClientTypeCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "fc"; | ||
|
||
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: CLIENT_TYPE [MORE_CLIENT_TYPES]...\n" | ||
+ "Example: " + COMMAND_WORD + "Investment Plan 1"; | ||
|
||
private final ClientTypeContainsKeywordsPredicate predicate; | ||
|
||
public FindClientTypeCommand(ClientTypeContainsKeywordsPredicate predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
|
||
@Override | ||
public CommandResult execute(Model model) { | ||
requireNonNull(model); | ||
model.updateFilteredPersonList(predicate); | ||
return new CommandResult( | ||
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size())); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("predicate", predicate) | ||
.toString(); | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
src/main/java/seedu/address/logic/parser/FindClientTypeCommandParser.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,33 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import java.util.Arrays; | ||
|
||
import seedu.address.logic.commands.FindClientTypeCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.person.ClientTypeContainsKeywordsPredicate; | ||
|
||
/** | ||
* Parses input arguments and creates a new FindCommand object | ||
*/ | ||
public class FindClientTypeCommandParser implements Parser<FindClientTypeCommand> { | ||
|
||
/** | ||
* 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 FindClientTypeCommand parse(String args) throws ParseException { | ||
String trimmedArgs = args.trim(); | ||
if (trimmedArgs.isEmpty()) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindClientTypeCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
String[] nameKeywords = trimmedArgs.split("\\s+"); | ||
|
||
return new FindClientTypeCommand(new ClientTypeContainsKeywordsPredicate(Arrays.asList(nameKeywords))); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/seedu/address/model/person/ClientTypeContainsKeywordsPredicate.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,49 @@ | ||
package seedu.address.model.person; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.model.clienttype.ClientType; | ||
|
||
/** | ||
* Tests that a {@code Person}'s {@code Name} matches any of the keywords given. | ||
*/ | ||
public class ClientTypeContainsKeywordsPredicate implements Predicate<Person> { | ||
private final List<String> keywords; | ||
|
||
public ClientTypeContainsKeywordsPredicate(List<String> keywords) { | ||
this.keywords = keywords; | ||
} | ||
|
||
@Override | ||
public boolean test(Person person) { | ||
Set<ClientType> personClientTypes = person.getClientTypes(); | ||
return keywords.stream() | ||
.anyMatch(keyword -> personClientTypes.stream().anyMatch(clientType -> | ||
clientType.clientTypeName.toLowerCase().startsWith(keyword.toLowerCase())) | ||
); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof ClientTypeContainsKeywordsPredicate)) { | ||
return false; | ||
} | ||
|
||
ClientTypeContainsKeywordsPredicate otherNameContainsKeywordsPredicate = | ||
(ClientTypeContainsKeywordsPredicate) other; | ||
return keywords.equals(otherNameContainsKeywordsPredicate.keywords); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("keywords", keywords).toString(); | ||
} | ||
} |