forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
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 nus-cs2103-AY2324S1#113 from nicolengk/Add-findpro…
…p-feature Add findprop feature
- Loading branch information
Showing
7 changed files
with
193 additions
and
4 deletions.
There are no files selected for viewing
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
59 changes: 59 additions & 0 deletions
59
src/main/java/seedu/address/logic/commands/FindPropertyCommand.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,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(); | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
src/main/java/seedu/address/logic/parser/FindPropertyCommandParser.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.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> { | ||
|
||
/** | ||
* 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(); | ||
if (trimmedArgs.isEmpty()) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindPropertyCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
String[] nameKeywords = trimmedArgs.split("\\s+"); | ||
|
||
return new FindPropertyCommand(new PropNameContainsKeywordsPredicate(Arrays.asList(nameKeywords))); | ||
} | ||
|
||
} |
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
93 changes: 93 additions & 0 deletions
93
src/test/java/seedu/address/logic/commands/FindPropertyCommandTest.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,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+"))); | ||
} | ||
} |
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