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 #51 from KrashKart/branch-Restructure-Find
Reformat find by name command
- Loading branch information
Showing
6 changed files
with
194 additions
and
13 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/main/java/seedu/address/logic/commands/AbstractFindCommand.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,58 @@ | ||
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.ContainsKeywordsPredicate; | ||
|
||
/** | ||
* Finds and lists all persons in address book whose name contains any of the argument keywords. | ||
* Keyword matching is case-insensitive. | ||
*/ | ||
public abstract class AbstractFindCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "find"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names, contacts or emails " | ||
+ "contain any of the specified keywords (case-insensitive) and displays them as a list with indices.\n" | ||
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n" | ||
+ "Example: " + COMMAND_WORD + " alice bob charlie"; | ||
|
||
private final ContainsKeywordsPredicate predicate; | ||
|
||
public AbstractFindCommand(ContainsKeywordsPredicate predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) { | ||
requireNonNull(model); | ||
model.updateFilteredPersonList(this.predicate); | ||
return new CommandResult( | ||
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size())); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof AbstractFindCommand)) { | ||
return false; | ||
} | ||
|
||
AbstractFindCommand otherFindCommand = (AbstractFindCommand) other; | ||
return this.predicate.equals(otherFindCommand.predicate); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("predicate", this.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
42 changes: 42 additions & 0 deletions
42
src/main/java/seedu/address/model/person/ContactContainsKeywordsPredicate.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,42 @@ | ||
package seedu.address.model.person; | ||
|
||
import java.util.List; | ||
|
||
import seedu.address.commons.util.StringUtil; | ||
import seedu.address.commons.util.ToStringBuilder; | ||
|
||
/** | ||
* Tests that a {@code Person}'s {@code Name} matches any of the keywords given. | ||
*/ | ||
public class ContactContainsKeywordsPredicate extends ContainsKeywordsPredicate { | ||
|
||
public ContactContainsKeywordsPredicate(List<String> keywords) { | ||
super(keywords); | ||
} | ||
|
||
@Override | ||
public boolean test(Person person) { | ||
return this.getKeywords().stream() | ||
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getPhone().value, keyword)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof ContactContainsKeywordsPredicate)) { | ||
return false; | ||
} | ||
|
||
ContactContainsKeywordsPredicate otherNameContainsKeywordsPredicate = (ContactContainsKeywordsPredicate) other; | ||
return this.getKeywords().equals(otherNameContainsKeywordsPredicate.getKeywords()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("keywords", this.getKeywords()).toString(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/seedu/address/model/person/ContainsKeywordsPredicate.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,41 @@ | ||
package seedu.address.model.person; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
|
||
/** | ||
* Tests that a {@code Person}'s {@code Name} matches any of the keywords given. | ||
*/ | ||
public abstract class ContainsKeywordsPredicate implements Predicate<Person> { | ||
private final List<String> keywords; | ||
|
||
public ContainsKeywordsPredicate(List<String> keywords) { | ||
this.keywords = keywords; | ||
} | ||
|
||
protected List<String> getKeywords() { | ||
return this.keywords; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof ContainsKeywordsPredicate)) { | ||
return false; | ||
} | ||
|
||
ContainsKeywordsPredicate otherNameContainsKeywordsPredicate = (ContainsKeywordsPredicate) other; | ||
return keywords.equals(otherNameContainsKeywordsPredicate.keywords); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("keywords", keywords).toString(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/seedu/address/model/person/EmailContainsKeywordsPredicate.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,42 @@ | ||
package seedu.address.model.person; | ||
|
||
import java.util.List; | ||
|
||
import seedu.address.commons.util.StringUtil; | ||
import seedu.address.commons.util.ToStringBuilder; | ||
|
||
/** | ||
* Tests that a {@code Person}'s {@code Name} matches any of the keywords given. | ||
*/ | ||
public class EmailContainsKeywordsPredicate extends ContainsKeywordsPredicate { | ||
|
||
public EmailContainsKeywordsPredicate(List<String> keywords) { | ||
super(keywords); | ||
} | ||
|
||
@Override | ||
public boolean test(Person person) { | ||
return super.getKeywords().stream() | ||
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getEmail().value, keyword)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof EmailContainsKeywordsPredicate)) { | ||
return false; | ||
} | ||
|
||
EmailContainsKeywordsPredicate otherNameContainsKeywordsPredicate = (EmailContainsKeywordsPredicate) other; | ||
return this.getKeywords().equals(otherNameContainsKeywordsPredicate.getKeywords()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("keywords", this.getKeywords()).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