forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #109 from teoks0199/branch-find-item
Branch find item
- Loading branch information
Showing
9 changed files
with
156 additions
and
13 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
58 changes: 58 additions & 0 deletions
58
src/main/java/seedu/address/logic/commands/FindLocationCommand.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,58 @@ | ||
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.stall.LocationContainsKeywordsPredicate; | ||
|
||
/** | ||
* Finds and lists all stalls in address book whose location contains any of the argument keywords. | ||
* Keyword matching is case insensitive. | ||
*/ | ||
public class FindLocationCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "find-location"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all stalls whose location 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 + " deck techno terrace"; | ||
|
||
private final LocationContainsKeywordsPredicate predicate; | ||
|
||
public FindLocationCommand(LocationContainsKeywordsPredicate predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) { | ||
requireNonNull(model); | ||
model.updateFilteredStallList(predicate); | ||
return new CommandResult( | ||
String.format(Messages.MESSAGE_STALLS_LISTED_OVERVIEW, model.getFilteredStallList().size())); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof FindLocationCommand)) { | ||
return false; | ||
} | ||
|
||
FindLocationCommand otherFindLocationCommand = (FindLocationCommand) other; | ||
return predicate.equals(otherFindLocationCommand.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
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
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
35 changes: 35 additions & 0 deletions
35
src/main/java/seedu/address/logic/parser/FindLocationCommandParser.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,35 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import java.util.Arrays; | ||
|
||
import seedu.address.logic.commands.FindLocationCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.stall.LocationContainsKeywordsPredicate; | ||
|
||
|
||
/** | ||
* Parses input arguments and creates a new FindLocationCommand object | ||
*/ | ||
public class FindLocationCommandParser implements Parser<FindLocationCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the FindLocationCommand | ||
* and returns a FindLocationCommand object for execution. | ||
* | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public FindLocationCommand parse(String args) throws ParseException { | ||
String trimmedArgs = args.trim(); | ||
if (trimmedArgs.isEmpty()) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindLocationCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
String[] locationKeywords = trimmedArgs.split("\\s+"); | ||
|
||
return new FindLocationCommand(new LocationContainsKeywordsPredicate(Arrays.asList(locationKeywords))); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/seedu/address/model/stall/LocationContainsKeywordsPredicate.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,45 @@ | ||
package seedu.address.model.stall; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.StringUtil; | ||
import seedu.address.commons.util.ToStringBuilder; | ||
|
||
/** | ||
* Tests that a {@code Stall}'s {@code Name} matches any of the keywords given. | ||
*/ | ||
public class LocationContainsKeywordsPredicate implements Predicate<Stall> { | ||
private final List<String> keywords; | ||
|
||
public LocationContainsKeywordsPredicate(List<String> keywords) { | ||
this.keywords = keywords; | ||
} | ||
|
||
@Override | ||
public boolean test(Stall stall) { | ||
return keywords.stream() | ||
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(stall.getLocation().locationName, keyword)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof LocationContainsKeywordsPredicate)) { | ||
return false; | ||
} | ||
|
||
LocationContainsKeywordsPredicate otherLocationContainsKeywordsPredicate = | ||
(LocationContainsKeywordsPredicate) other; | ||
return keywords.equals(otherLocationContainsKeywordsPredicate.keywords); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("keywords", keywords).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
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