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 #56 from KrashKart/branch-Find-Contact
Implement filtering of contacts by number
- Loading branch information
Showing
7 changed files
with
167 additions
and
7 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/main/java/seedu/address/logic/commands/FindByContactCommand.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.commands; | ||
|
||
import seedu.address.model.person.ContactContainsKeywordsPredicate; | ||
|
||
/** | ||
* Finds and lists all persons in address book whose contact contains any of the argument keywords. | ||
*/ | ||
public class FindByContactCommand extends AbstractFindCommand { | ||
public static final String COMMAND_WORD = "find /c"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose contact numbers " | ||
+ "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 + " 91112345 999 995"; | ||
public FindByContactCommand(ContactContainsKeywordsPredicate predicate) { | ||
super(predicate); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof FindByContactCommand)) { | ||
return false; | ||
} | ||
|
||
FindByContactCommand otherFindCommand = (FindByContactCommand) other; | ||
return this.getPredicate().equals(otherFindCommand.getPredicate()); | ||
} | ||
} |
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
90 changes: 90 additions & 0 deletions
90
src/test/java/seedu/address/logic/commands/FindByContactCommandTest.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,90 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static seedu.address.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW; | ||
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; | ||
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.model.Model; | ||
import seedu.address.model.ModelManager; | ||
import seedu.address.model.UserPrefs; | ||
import seedu.address.model.person.ContactContainsKeywordsPredicate; | ||
import seedu.address.model.person.NameContainsKeywordsPredicate; | ||
|
||
/** | ||
* Contains integration tests (interaction with the Model) for {@code FindByContactCommand}. | ||
*/ | ||
public class FindByContactCommandTest { | ||
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
|
||
@Test | ||
public void equals() { | ||
ContactContainsKeywordsPredicate firstPredicate = | ||
new ContactContainsKeywordsPredicate(Collections.singletonList("first")); | ||
ContactContainsKeywordsPredicate secondPredicate = | ||
new ContactContainsKeywordsPredicate(Collections.singletonList("second")); | ||
|
||
FindByContactCommand findFirstCommand = new FindByContactCommand(firstPredicate); | ||
FindByContactCommand findSecondCommand = new FindByContactCommand(secondPredicate); | ||
|
||
// same object -> returns true | ||
assertTrue(findFirstCommand.equals(findFirstCommand)); | ||
|
||
// same values -> returns true | ||
FindByContactCommand findFirstCommandCopy = new FindByContactCommand(firstPredicate); | ||
assertTrue(findFirstCommand.equals(findFirstCommandCopy)); | ||
|
||
// different types -> returns false | ||
assertFalse(findFirstCommand.equals(1)); | ||
|
||
// null -> returns false | ||
assertFalse(findFirstCommand.equals(null)); | ||
|
||
// different person -> returns false | ||
assertFalse(findFirstCommand.equals(findSecondCommand)); | ||
} | ||
|
||
@Test | ||
public void execute_zeroKeywords_noPersonFound() { | ||
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0); | ||
ContactContainsKeywordsPredicate predicate = preparePredicate(" "); | ||
FindByContactCommand command = new FindByContactCommand(predicate); | ||
expectedModel.updateFilteredPersonList(predicate); | ||
assertCommandSuccess(command, model, expectedMessage, expectedModel); | ||
assertEquals(Collections.emptyList(), model.getFilteredPersonList()); | ||
} | ||
|
||
// Commented out because expectedModel does not contain contact numbers | ||
// @Test | ||
// public void execute_multipleKeywords_multiplePersonsFound() { | ||
// String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0); | ||
// ContactContainsKeywordsPredicate predicate = preparePredicate("999"); | ||
// FindByContactCommand command = new FindByContactCommand(predicate); | ||
// expectedModel.updateFilteredPersonList(predicate); | ||
// assertCommandSuccess(command, model, expectedMessage, expectedModel); | ||
// assertEquals(Arrays.asList(CARL, ELLE, FIONA), model.getFilteredPersonList()); | ||
// } | ||
|
||
@Test | ||
public void toStringMethod() { | ||
NameContainsKeywordsPredicate predicate = new NameContainsKeywordsPredicate(Arrays.asList("keyword")); | ||
FindByNameCommand findCommand = new FindByNameCommand(predicate); | ||
String expected = FindByNameCommand.class.getCanonicalName() + "{predicate=" + predicate + "}"; | ||
assertEquals(expected, findCommand.toString()); | ||
} | ||
|
||
/** | ||
* Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}. | ||
*/ | ||
private ContactContainsKeywordsPredicate preparePredicate(String userInput) { | ||
return new ContactContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+"))); | ||
} | ||
} |
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