Skip to content

Commit

Permalink
Merge pull request #109 from teoks0199/branch-find-item
Browse files Browse the repository at this point in the history
Branch find item
  • Loading branch information
Ruizhi2001 authored Oct 26, 2023
2 parents b138bfe + d31f64a commit 01b5031
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public boolean equals(Object other) {
return false;
}

FindItemCommand otherFindCommand = (FindItemCommand) other;
return predicate.equals(otherFindCommand.predicate);
FindItemCommand otherFindItemCommand = (FindItemCommand) other;
return predicate.equals(otherFindItemCommand.predicate);
}

@Override
Expand Down
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class FindStallCommand extends Command {
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all stalls 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";
+ "Example: " + COMMAND_WORD + " japanese western noodle";

private final NameContainsKeywordsPredicate predicate;

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/seedu/address/logic/parser/AddressBookParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindItemCommand;
import seedu.address.logic.commands.FindLocationCommand;
import seedu.address.logic.commands.FindStallCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
Expand Down Expand Up @@ -95,12 +96,15 @@ public Command parseCommand(String userInput) throws ParseException {
case ClearCommand.COMMAND_WORD:
return new ClearCommand();

case FindStallCommand.COMMAND_WORD:
return new FindStallCommandParser().parse(arguments);

case FindItemCommand.COMMAND_WORD:
return new FindItemCommandParser().parse(arguments);

case FindLocationCommand.COMMAND_WORD:
return new FindLocationCommandParser().parse(arguments);

case FindStallCommand.COMMAND_WORD:
return new FindStallCommandParser().parse(arguments);

case ListCommand.COMMAND_WORD:
return new ListCommand();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@


/**
* Parses input arguments and creates a new FindStallCommand object
* Parses input arguments and creates a new FindItemCommand object
*/
public class FindItemCommandParser implements Parser<FindItemCommand> {

/**
* Parses the given {@code String} of arguments in the context of the FindStallCommand
* and returns a FindStallCommand object for execution.
* Parses the given {@code String} of arguments in the context of the FindItemCommand
* and returns a FindItemCommand object for execution.
*
* @throws ParseException if the user input does not conform the expected format
*/
Expand All @@ -29,9 +29,9 @@ public FindItemCommand parse(String args) throws ParseException {
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindItemCommand.MESSAGE_USAGE));
}

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

return new FindItemCommand(new MenuContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
return new FindItemCommand(new MenuContainsKeywordsPredicate(Arrays.asList(itemKeywords)));
}

}
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)));
}

}
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public boolean equals(Object other) {
return false;
}

MenuContainsKeywordsPredicate otherNameContainsKeywordsPredicate = (MenuContainsKeywordsPredicate) other;
return keywords.equals(otherNameContainsKeywordsPredicate.keywords);
MenuContainsKeywordsPredicate otherMenuContainsKeywordsPredicate = (MenuContainsKeywordsPredicate) other;
return keywords.equals(otherMenuContainsKeywordsPredicate.keywords);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.ExitCommand;
//import seedu.address.logic.commands.FindItemCommand;
//import seedu.address.logic.commands.FindLocationCommand;
import seedu.address.logic.commands.FindStallCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
Expand Down

0 comments on commit 01b5031

Please sign in to comment.