Skip to content

Commit

Permalink
Merge pull request #69 from notnotmax/v1.3-unabstract-find-command
Browse files Browse the repository at this point in the history
Revert abstraction of FindCommand
  • Loading branch information
notnotmax authored Oct 14, 2024
2 parents 9044e40 + 2820b59 commit 284473c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 78 deletions.
22 changes: 10 additions & 12 deletions src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,27 @@

import static java.util.Objects.requireNonNull;

import java.util.function.Predicate;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.model.Model;
import seedu.address.model.student.Student;
import seedu.address.model.student.NameContainsKeywordsPredicate;

/**
* Finds and lists all persons in address book whose attribute satisfies the given predicate.
* Finds and lists all persons in address book whose name contains any of the argument keywords.
* Keyword matching is case-insensitive.
*/
public abstract class FindCommand extends Command {
public class FindCommand extends Command {

public static final String COMMAND_WORD = "find";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons with the specified attribute "
+ "containing any of the specified keywords (case-insensitive)"
+ "and displays them as a list with index numbers.\n"
+ "Parameters: PREFIX/KEYWORD [PREFIX/MORE_KEYWORDS]...\n"
+ "Example: " + COMMAND_WORD + " n/alice n/bob";
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: KEYWORD [MORE_KEYWORDS]...\n"
+ "Example: " + COMMAND_WORD + " alice bob charlie";

protected final Predicate<Student> predicate;
private final NameContainsKeywordsPredicate predicate;

public FindCommand(Predicate<Student> predicate) {
public FindCommand(NameContainsKeywordsPredicate predicate) {
this.predicate = predicate;
}

Expand Down
19 changes: 0 additions & 19 deletions src/main/java/seedu/address/logic/commands/FindNameCommand.java

This file was deleted.

35 changes: 10 additions & 25 deletions src/main/java/seedu/address/logic/parser/FindCommandParser.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,32 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;

import java.util.List;
import java.util.stream.Stream;
import java.util.Arrays;

import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.FindNameCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.student.NameContainsKeywordsPredicate;

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

/**
* Parses the given {@code String} of arguments in the context of the FindNameCommand
* and returns a FindNameCommand object for execution.
* 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 {
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME);

if (!argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindNameCommand.MESSAGE_USAGE));
}

if (!arePrefixesPresent(argMultimap, PREFIX_NAME)) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindNameCommand.MESSAGE_USAGE));
String trimmedArgs = args.trim();
if (trimmedArgs.isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}

List<String> nameKeywords = argMultimap.getAllValues(PREFIX_NAME);
String[] nameKeywords = trimmedArgs.split("\\s+");

return new FindNameCommand(new NameContainsKeywordsPredicate(nameKeywords));
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
return new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import seedu.address.model.student.NameContainsKeywordsPredicate;

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

Expand All @@ -34,14 +34,14 @@ public void equals() {
NameContainsKeywordsPredicate secondPredicate =
new NameContainsKeywordsPredicate(Collections.singletonList("second"));

FindNameCommand findFirstCommand = new FindNameCommand(firstPredicate);
FindNameCommand findSecondCommand = new FindNameCommand(secondPredicate);
FindCommand findFirstCommand = new FindCommand(firstPredicate);
FindCommand findSecondCommand = new FindCommand(secondPredicate);

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

// same values -> returns true
FindNameCommand findFirstCommandCopy = new FindNameCommand(firstPredicate);
FindCommand findFirstCommandCopy = new FindCommand(firstPredicate);
assertTrue(findFirstCommand.equals(findFirstCommandCopy));

// different types -> returns false
Expand All @@ -58,7 +58,7 @@ public void equals() {
public void execute_zeroKeywords_noPersonFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0);
NameContainsKeywordsPredicate predicate = preparePredicate(" ");
FindNameCommand command = new FindNameCommand(predicate);
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getFilteredPersonList());
Expand All @@ -68,12 +68,20 @@ public void execute_zeroKeywords_noPersonFound() {
public void execute_multipleKeywords_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3);
NameContainsKeywordsPredicate predicate = preparePredicate("Kurz Elle Kunz");
FindNameCommand command = new FindNameCommand(predicate);
FindCommand command = new FindCommand(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"));
FindCommand findCommand = new FindCommand(predicate);
String expected = FindCommand.class.getCanonicalName() + "{predicate=" + predicate + "}";
assertEquals(expected, findCommand.toString());
}

/**
* Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.FindNameCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.parser.exceptions.ParseException;
Expand Down Expand Up @@ -79,10 +78,9 @@ public void parseCommand_exit() throws Exception {
@Test
public void parseCommand_find() throws Exception {
List<String> keywords = Arrays.asList("foo", "bar", "baz");
FindNameCommand command = (FindNameCommand) parser.parseCommand(
FindCommand.COMMAND_WORD + " "
+ keywords.stream().map(k -> "n/" + k).collect(Collectors.joining(" ")));
assertEquals(new FindNameCommand(new NameContainsKeywordsPredicate(keywords)), command);
FindCommand command = (FindCommand) parser.parseCommand(
FindCommand.COMMAND_WORD + " " + keywords.stream().collect(Collectors.joining(" ")));
assertEquals(new FindCommand(new NameContainsKeywordsPredicate(keywords)), command);
}

@Test
Expand All @@ -100,7 +98,7 @@ public void parseCommand_list() throws Exception {
@Test
public void parseCommand_unrecognisedInput_throwsParseException() {
assertThrows(ParseException.class, String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE), ()
-> parser.parseCommand(""));
-> parser.parseCommand(""));
}

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

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.FindNameCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.model.student.NameContainsKeywordsPredicate;

public class FindCommandParserTest {
Expand All @@ -17,19 +17,17 @@ public class FindCommandParserTest {

@Test
public void parse_emptyArg_throwsParseException() {
assertParseFailure(parser, " ",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindNameCommand.MESSAGE_USAGE));
assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}

@Test
public void parse_validArgs_returnsFindNameCommand() {
public void parse_validArgs_returnsFindCommand() {
// no leading and trailing whitespaces
FindNameCommand expectedFindCommand =
new FindNameCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob")));
assertParseSuccess(parser, " n/Alice n/Bob", expectedFindCommand);
FindCommand expectedFindCommand =
new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob")));
assertParseSuccess(parser, "Alice Bob", expectedFindCommand);

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

}

0 comments on commit 284473c

Please sign in to comment.