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

Add findprop feature #113

Merged
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Messages {

public static final String MESSAGE_PROPERTIES_MATCH_OVERVIEW = "%1$d customers matched with property ";
public static final String MESSAGE_CUSTOMERS_LISTED_OVERVIEW = "%1$d customers listed!";

public static final String MESSAGE_PROPERTIES_LISTED_OVERVIEW = "%1$d properties listed!";
public static final String MESSAGE_DUPLICATE_FIELDS =
"Multiple values specified for the following single-valued field(s): ";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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.property.PropNameContainsKeywordsPredicate;


/**
* Finds and lists all properties in address book whose name contains any of the argument keywords.
* Keyword matching is case insensitive.
*/
public class FindPropertyCommand extends Command {

public static final String COMMAND_WORD = "findprop";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all properties 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 + " Aquavista Luxeloft";

private final PropNameContainsKeywordsPredicate predicate;

public FindPropertyCommand(PropNameContainsKeywordsPredicate predicate) {
this.predicate = predicate;
}

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredPropertyList(predicate);
return new CommandResult(
String.format(Messages.MESSAGE_PROPERTIES_LISTED_OVERVIEW, model.getFilteredPropertyList().size()));
}

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

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

FindPropertyCommand otherFindCommand = (FindPropertyCommand) other;
return predicate.equals(otherFindCommand.predicate);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("predicate", predicate)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FilterCustomerCommand;
import seedu.address.logic.commands.FindCustomerCommand;
import seedu.address.logic.commands.FindPropertyCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCustomerCommand;
import seedu.address.logic.commands.ListPropertyCommand;
Expand Down Expand Up @@ -84,6 +85,9 @@
case FindCustomerCommand.COMMAND_WORD:
return new FindCustomerCommandParser().parse(arguments);

case FindPropertyCommand.COMMAND_WORD:
return new FindPropertyCommandParser().parse(arguments);

Check warning on line 89 in src/main/java/seedu/address/logic/parser/AddressBookParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/AddressBookParser.java#L89

Added line #L89 was not covered by tests

case FilterCustomerCommand.COMMAND_WORD:
return new FilterCustomerCommandParser().parse(arguments);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import java.util.Arrays;

import seedu.address.logic.commands.FindPropertyCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.property.PropNameContainsKeywordsPredicate;

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

Check warning on line 14 in src/main/java/seedu/address/logic/parser/FindPropertyCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/FindPropertyCommandParser.java#L14

Added line #L14 was not covered by tests

/**
* Parses the given {@code String} of arguments in the context of the FindPropertyCommand
* and returns a FindPropertyCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public FindPropertyCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim();

Check warning on line 22 in src/main/java/seedu/address/logic/parser/FindPropertyCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/FindPropertyCommandParser.java#L22

Added line #L22 was not covered by tests
if (trimmedArgs.isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindPropertyCommand.MESSAGE_USAGE));

Check warning on line 25 in src/main/java/seedu/address/logic/parser/FindPropertyCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/FindPropertyCommandParser.java#L24-L25

Added lines #L24 - L25 were not covered by tests
}

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

Check warning on line 28 in src/main/java/seedu/address/logic/parser/FindPropertyCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/FindPropertyCommandParser.java#L28

Added line #L28 was not covered by tests

return new FindPropertyCommand(new PropNameContainsKeywordsPredicate(Arrays.asList(nameKeywords)));

Check warning on line 30 in src/main/java/seedu/address/logic/parser/FindPropertyCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/FindPropertyCommandParser.java#L30

Added line #L30 was not covered by tests
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public PropNameContainsKeywordsPredicate(List<String> keywords) {
@Override
public boolean test(Property property) {
return keywords.stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(property.getName().fullName, keyword));
.anyMatch(keyword -> StringUtil.startsWithWordIgnoreCaseWithoutFullMatch(property.getName().fullName,
keyword));
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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_PROPERTIES_LISTED_OVERVIEW;
import static seedu.address.logic.commands.CommandPropertyTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalCustomers.getTypicalAddressBook;
import static seedu.address.testutil.TypicalProperties.AQUAVISTA;
import static seedu.address.testutil.TypicalProperties.HORIZONVIEW;
import static seedu.address.testutil.TypicalProperties.SKYVISTA;
import static seedu.address.testutil.TypicalProperties.getTypicalPropertyBook;

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.property.PropNameContainsKeywordsPredicate;


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

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

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

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

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

// different types -> returns false
assertFalse(findFirstCommand.equals(1));

// null -> returns false
assertFalse(findFirstCommand.equals(null));

// different customer -> returns false
assertFalse(findFirstCommand.equals(findSecondCommand));
}

@Test
public void execute_zeroKeywords_noPropertyFound() {
String expectedMessage = String.format(MESSAGE_PROPERTIES_LISTED_OVERVIEW, 0);
PropNameContainsKeywordsPredicate predicate = preparePredicate(" ");
FindPropertyCommand command = new FindPropertyCommand(predicate);
expectedModel.updateFilteredPropertyList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getFilteredPropertyList());
}

@Test
public void execute_multipleKeywords_multiplePropertiesFound() {
String expectedMessage = String.format(MESSAGE_PROPERTIES_LISTED_OVERVIEW, 3);
PropNameContainsKeywordsPredicate predicate = preparePredicate("Aquavi Skyvista Horizonview");
FindPropertyCommand command = new FindPropertyCommand(predicate);
expectedModel.updateFilteredPropertyList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(AQUAVISTA, SKYVISTA, HORIZONVIEW), model.getFilteredPropertyList());
}

@Test
public void toStringMethod() {
PropNameContainsKeywordsPredicate predicate = new PropNameContainsKeywordsPredicate(Arrays.asList("keyword"));
FindPropertyCommand findCommand = new FindPropertyCommand(predicate);
String expected = FindPropertyCommand.class.getCanonicalName() + "{predicate=" + predicate + "}";
assertEquals(expected, findCommand.toString());
}

/**
* Parses {@code userInput} into a {@code PropNameContainsKeywordsPredicate}.
*/
private PropNameContainsKeywordsPredicate preparePredicate(String userInput) {
return new PropNameContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ public static PropertyBook getTypicalPropertyBook() {
}

public static List<Property> getTypicalProperties() {
return new ArrayList<>(Arrays.asList(AQUAVIEW, SKYVISTA, HORIZONVIEW, LUXELOFT, RIVERIA, AZURE, TRANQUILIS));
return new ArrayList<>(Arrays.asList(AQUAVISTA, SKYVISTA, HORIZONVIEW, LUXELOFT, RIVERIA, AZURE, TRANQUILIS));
}
}
Loading