Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-implement find by name command #54

Merged
merged 5 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public AbstractFindCommand(ContainsKeywordsPredicate predicate) {
this.predicate = predicate;
}

protected ContainsKeywordsPredicate getPredicate() {
return this.predicate;
}

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
Expand All @@ -34,21 +38,6 @@ public CommandResult execute(Model model) {
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)
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/seedu/address/logic/commands/FindByNameCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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 FindByNameCommand extends AbstractFindCommand {
public FindByNameCommand(NameContainsKeywordsPredicate predicate) {
super(predicate);
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof FindByNameCommand)) {
return false;
}

FindByNameCommand otherFindCommand = (FindByNameCommand) other;
return this.getPredicate().equals(otherFindCommand.getPredicate());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import java.util.regex.Pattern;

import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.AbstractFindCommand;
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.parser.exceptions.ParseException;
Expand Down Expand Up @@ -65,7 +65,7 @@ public Command parseCommand(String userInput) throws ParseException {
case ClearCommand.COMMAND_WORD:
return new ClearCommand();

case FindCommand.COMMAND_WORD:
case AbstractFindCommand.COMMAND_WORD:
return new FindCommandParser().parse(arguments);

case ListCommand.COMMAND_WORD:
Expand Down
35 changes: 27 additions & 8 deletions src/main/java/seedu/address/logic/parser/FindCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,50 @@
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import seedu.address.logic.commands.AbstractFindCommand;
import seedu.address.logic.commands.FindByNameCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.NameContainsKeywordsPredicate;

/**
* Parses input arguments and creates a new FindCommand object
*/
public class FindCommandParser implements Parser<FindCommand> {
public class FindCommandParser implements Parser<AbstractFindCommand> {

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

/**
* 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 FindCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim();
if (trimmedArgs.isEmpty()) {
public AbstractFindCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim(); // trim space
Matcher m = KEYWORD_EXTRACTOR.matcher(trimmedArgs); // find tag and search words

// will throw exception if no args/command format not correct
if (trimmedArgs.isEmpty() || !m.matches()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}

String[] nameKeywords = trimmedArgs.split("\\s+");

return new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
// extract tag and search argument
String tag = m.group("tag");
String searchTerms = m.group("arguments");
String[] searchTermArray = searchTerms.split("\\s+");

// return approppriate FindCommand class depending on tag
switch (tag) {
case "/n":
return new FindByNameCommand(
new NameContainsKeywordsPredicate(Arrays.asList(searchTermArray)));
default:
return null; // temporary value, this should not occur due to regex

Check warning on line 49 in src/main/java/seedu/address/logic/parser/FindCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/FindCommandParser.java#L49

Added line #L49 was not covered by tests
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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.CARL;
import static seedu.address.testutil.TypicalPersons.ELLE;
import static seedu.address.testutil.TypicalPersons.FIONA;
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.NameContainsKeywordsPredicate;

/**
* Contains integration tests (interaction with the Model) for {@code FindCommand}.
*/
public class FindByNameCommandTest {
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void equals() {
NameContainsKeywordsPredicate firstPredicate =
new NameContainsKeywordsPredicate(Collections.singletonList("first"));
NameContainsKeywordsPredicate secondPredicate =
new NameContainsKeywordsPredicate(Collections.singletonList("second"));

FindByNameCommand findFirstCommand = new FindByNameCommand(firstPredicate);
FindByNameCommand findSecondCommand = new FindByNameCommand(secondPredicate);

// same object -> returns true
assertTrue(findFirstCommand.equals(findFirstCommand));

// same values -> returns true
FindByNameCommand findFirstCommandCopy = new FindByNameCommand(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);
NameContainsKeywordsPredicate predicate = preparePredicate(" ");
FindByNameCommand command = new FindByNameCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getFilteredPersonList());
}

@Test
public void execute_multipleKeywords_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3);
NameContainsKeywordsPredicate predicate = preparePredicate("Kurz Elle Kunz");
FindByNameCommand command = new FindByNameCommand(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 NameContainsKeywordsPredicate preparePredicate(String userInput) {
return new NameContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.FindByNameCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.parser.exceptions.ParseException;
Expand Down Expand Up @@ -68,12 +68,21 @@ public void parseCommand_exit() throws Exception {
assertTrue(parser.parseCommand(ExitCommand.COMMAND_WORD + " 3") instanceof ExitCommand);
}

// @Test
// public void parseCommand_find() throws Exception {
// List<String> keywords = Arrays.asList("foo", "bar", "baz");
// FindCommand command = (FindCommand) parser.parseCommand(
// FindCommand.COMMAND_WORD + " " + keywords.stream().collect(Collectors.joining(" ")));
// assertEquals(new FindCommand(new NameContainsKeywordsPredicate(keywords)), command);
// }

@Test
public void parseCommand_find() throws Exception {
public void parseCommand_findByName() throws Exception {
List<String> keywords = Arrays.asList("foo", "bar", "baz");
FindCommand command = (FindCommand) parser.parseCommand(
FindCommand.COMMAND_WORD + " " + keywords.stream().collect(Collectors.joining(" ")));
assertEquals(new FindCommand(new NameContainsKeywordsPredicate(keywords)), command);
FindByNameCommand command = (FindByNameCommand) parser.parseCommand(
FindByNameCommand.COMMAND_WORD + " /n "
+ keywords.stream().collect(Collectors.joining(" ")));
assertEquals(new FindByNameCommand(new NameContainsKeywordsPredicate(keywords)), command);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.FindByNameCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.model.person.NameContainsKeywordsPredicate;

Expand All @@ -21,14 +22,15 @@ public void parse_emptyArg_throwsParseException() {
}

@Test
public void parse_validArgs_returnsFindCommand() {
public void parse_validArgs_returnsFindByNameCommand() {
FindByNameCommand expectedFindCommand =
new FindByNameCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob")));

// no leading and trailing whitespaces
FindCommand expectedFindCommand =
new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob")));
assertParseSuccess(parser, "Alice Bob", expectedFindCommand);
assertParseSuccess(parser, "/n Alice Bob", expectedFindCommand);

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

}