Skip to content

Commit

Permalink
Merge pull request #69 from yooplo/branch-update-find-command-format
Browse files Browse the repository at this point in the history
Branch update find command format
  • Loading branch information
KrashKart authored Oct 15, 2024
2 parents 779c744 + 0094894 commit c600cef
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
public abstract class AbstractFindCommand extends Command {

public static final String COMMAND_WORD = "find";
public static final String NAME_COMMAND_WORD = "/n";
public static final String EMAIL_COMMAND_WORD = "/e";
public static final String CONTACT_COMMAND_WORD = "/c";
public static final String NAME_COMMAND_WORD = " n/";
public static final String EMAIL_COMMAND_WORD = " e/";
public static final String CONTACT_COMMAND_WORD = " c/";

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:\n"
+ COMMAND_WORD + NAME_COMMAND_WORD + " alice bob charlie\n"
+ COMMAND_WORD + EMAIL_COMMAND_WORD + " [email protected]\n"
+ COMMAND_WORD + CONTACT_COMMAND_WORD + " 12345678\n";
+ COMMAND_WORD + NAME_COMMAND_WORD + "alice bob charlie\n"
+ COMMAND_WORD + EMAIL_COMMAND_WORD + "[email protected]\n"
+ COMMAND_WORD + CONTACT_COMMAND_WORD + "12345678\n";

private final ContainsKeywordsPredicate predicate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* 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 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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
public class FindByEmailCommand extends AbstractFindCommand {

public static final String COMMAND_WORD = "find /e";
public static final String COMMAND_WORD = "find e/";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose email "
+ "is the same as the specified keywords (case-insensitive) and displays them as a list with indices.\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Keyword matching is case-insensitive.
*/
public class FindByNameCommand extends AbstractFindCommand {
public static final String COMMAND_WORD = "find /n";
public static final String COMMAND_WORD = "find n/";

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 indices.\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class FindCommandParser implements Parser<AbstractFindCommand> {

public static final Pattern KEYWORD_EXTRACTOR =
Pattern.compile("^(?<tag>/[cen])\\s*(?<arguments>[\\S\\s]+)$");
Pattern.compile("^(?<tag>[cen]/)\\s*(?<arguments>[\\S\\s]+)$");

/**
* Parses the given {@code String} of arguments in the context of the FindCommand
Expand All @@ -45,13 +45,13 @@ public AbstractFindCommand parse(String args) throws ParseException {

// return appropriate FindCommand class depending on tag
switch (tag) {
case "/n":
case "n/":
return new FindByNameCommand(
new NameContainsKeywordsPredicate(Arrays.asList(searchTermArray)));
case "/c":
case "c/":
return new FindByContactCommand(
new ContactContainsKeywordsPredicate(Arrays.asList(searchTermArray)));
case "/e":
case "e/":
return new FindByEmailCommand(
new EmailContainsKeywordsPredicate(Arrays.asList(searchTermArray)));
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.List;

import seedu.address.commons.util.StringUtil;
import seedu.address.commons.util.ToStringBuilder;

/**
Expand All @@ -17,7 +16,7 @@ public ContactContainsKeywordsPredicate(List<String> keywords) {
@Override
public boolean test(Person person) {
return this.getKeywords().stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getPhone().value, keyword));
.anyMatch(keyword -> person.getPhone().value.toLowerCase().contains(keyword.toLowerCase()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.List;

import seedu.address.commons.util.StringUtil;
import seedu.address.commons.util.ToStringBuilder;

/**
Expand All @@ -17,7 +16,7 @@ public EmailContainsKeywordsPredicate(List<String> keywords) {
@Override
public boolean test(Person person) {
return super.getKeywords().stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getEmail().value, keyword));
.anyMatch(keyword -> person.getEmail().value.toLowerCase().contains(keyword.toLowerCase()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.List;

import seedu.address.commons.util.StringUtil;
import seedu.address.commons.util.ToStringBuilder;

/**
Expand All @@ -17,7 +16,7 @@ public NameContainsKeywordsPredicate(List<String> keywords) {
@Override
public boolean test(Person person) {
return this.getKeywords().stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getName().fullName, keyword));
.anyMatch(keyword -> person.getName().fullName.toLowerCase().contains(keyword.toLowerCase()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
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.ELLE;
import static seedu.address.testutil.TypicalPersons.FIONA;
import static seedu.address.testutil.TypicalPersons.GEORGE;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import java.util.Arrays;
Expand Down Expand Up @@ -62,6 +65,16 @@ public void execute_zeroKeywords_noPersonFound() {
assertEquals(Collections.emptyList(), model.getFilteredPersonList());
}

@Test
public void execute_resultsWithPartialMatch_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3);
ContactContainsKeywordsPredicate predicate = preparePredicate("82");
FindByContactCommand command = new FindByContactCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(ELLE, FIONA, GEORGE), model.getFilteredPersonList());
}

// Commented out because expectedModel does not contain contact numbers
// @Test
// public void execute_multipleKeywords_multiplePersonsFound() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static seedu.address.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalPersons.DANIEL;
import static seedu.address.testutil.TypicalPersons.ELLE;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import java.util.Arrays;
Expand Down Expand Up @@ -58,6 +60,16 @@ public void execute_zeroKeywords_noPersonFound() {
assertEquals(Collections.emptyList(), model.getFilteredPersonList());
}

@Test
public void execute_resultsWithPartialMatch_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 2);
EmailContainsKeywordsPredicate predicate = preparePredicate("ne");
FindByEmailCommand command = new FindByEmailCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(DANIEL, ELLE), model.getFilteredPersonList());
}

@Test
public void toStringMethod() {
EmailContainsKeywordsPredicate predicate = new EmailContainsKeywordsPredicate(Arrays.asList("keyword"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
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.ALICE;
import static seedu.address.testutil.TypicalPersons.BENSON;
import static seedu.address.testutil.TypicalPersons.CARL;
import static seedu.address.testutil.TypicalPersons.DANIEL;
import static seedu.address.testutil.TypicalPersons.ELLE;
import static seedu.address.testutil.TypicalPersons.FIONA;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;
Expand All @@ -24,8 +27,8 @@
* Contains integration tests (interaction with the Model) for {@code FindByNameCommand}.
*/
public class FindByNameCommandTest {
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private final Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private final Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void equals() {
Expand Down Expand Up @@ -74,6 +77,16 @@ public void execute_multipleKeywords_multiplePersonsFound() {
assertEquals(Arrays.asList(CARL, ELLE, FIONA), model.getFilteredPersonList());
}

@Test
public void execute_resultsWithPartialMatch_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 4);
NameContainsKeywordsPredicate predicate = preparePredicate("n");
FindByNameCommand command = new FindByNameCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(ALICE, BENSON, DANIEL, FIONA), model.getFilteredPersonList());
}

@Test
public void toStringMethod() {
NameContainsKeywordsPredicate predicate = new NameContainsKeywordsPredicate(Arrays.asList("keyword"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public void parse_validArgs_returnsFindByNameCommand() {
new FindByNameCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob")));

// no leading and trailing whitespaces
assertParseSuccess(parser, "/n Alice Bob", expectedFindCommand);
assertParseSuccess(parser, "n/ Alice Bob", expectedFindCommand);

// multiple whitespaces between keywords
assertParseSuccess(parser, "/n \n Alice \n \t Bob \t", expectedFindCommand);
assertParseSuccess(parser, "n/ \n Alice \n \t Bob \t", expectedFindCommand);
}

@Test
Expand All @@ -45,10 +45,10 @@ public void parse_validArgs_returnsFindByContactCommand() {
Arrays.asList("91234567", "995")));

// no leading and trailing whitespaces
assertParseSuccess(parser, "/c 91234567 995", expectedFindCommand);
assertParseSuccess(parser, "c/ 91234567 995", expectedFindCommand);

// multiple whitespaces between keywords
assertParseSuccess(parser, "/c \n 91234567 \n \t 995 \t", expectedFindCommand);
assertParseSuccess(parser, "c/ \n 91234567 \n \t 995 \t", expectedFindCommand);
}

@Test
Expand All @@ -58,10 +58,10 @@ public void parse_validArgs_returnsFindByEmailCommand() {
Arrays.asList("[email protected]", "[email protected]")));

// no leading and trailing whitespaces
assertParseSuccess(parser, "/e [email protected] [email protected]", expectedFindCommand);
assertParseSuccess(parser, "e/ [email protected] [email protected]", expectedFindCommand);

// multiple whitespaces between keywords
assertParseSuccess(parser, "/e \n [email protected] \n \t [email protected] \t", expectedFindCommand);
assertParseSuccess(parser, "e/ \n [email protected] \n \t [email protected] \t", expectedFindCommand);
}

}

0 comments on commit c600cef

Please sign in to comment.