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 #98 from ryantzr1/FindFunctionality
Add Find Functionality
- Loading branch information
Showing
25 changed files
with
634 additions
and
46 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
64 changes: 64 additions & 0 deletions
64
src/main/java/connectify/logic/commands/FindAllCommand.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,64 @@ | ||
package connectify.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import connectify.commons.util.ToStringBuilder; | ||
import connectify.logic.Messages; | ||
import connectify.model.EntityNameContainsKeywordsPredicate; | ||
import connectify.model.Model; | ||
|
||
/** | ||
* Finds and lists all persons and companies in the address book whose name contains any of the argument keywords. | ||
* Keyword matching is case-insensitive. | ||
*/ | ||
public class FindAllCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "find"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Finds all persons and companies 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 google"; | ||
|
||
private final EntityNameContainsKeywordsPredicate predicate; | ||
|
||
public FindAllCommand(EntityNameContainsKeywordsPredicate predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) { | ||
|
||
requireNonNull(model); | ||
model.updateFilteredEntityList(predicate); | ||
return new CommandResult( | ||
String.format(Messages.MESSAGE_PEOPLE_AND_COMPANIES_LISTED_OVERVIEW, model.getNumberOfEntities())); | ||
|
||
|
||
} | ||
|
||
|
||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
if (!(other instanceof FindAllCommand)) { | ||
return false; | ||
} | ||
|
||
FindAllCommand otherFindAllCommand = (FindAllCommand) other; | ||
|
||
return predicate.equals(otherFindAllCommand.predicate); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("predicate", predicate) | ||
.toString(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/connectify/logic/commands/FindCompaniesCommand.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 connectify.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import connectify.commons.util.ToStringBuilder; | ||
import connectify.logic.Messages; | ||
import connectify.model.Model; | ||
import connectify.model.company.CompanyNameContainsKeywordsPredicate; | ||
|
||
/** | ||
* Finds and lists all companies in address book whose name contains any of the argument keywords. | ||
* Keyword matching is case-insensitive. | ||
*/ | ||
public class FindCompaniesCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "findCompany"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all companies 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 + " apple google microsoft"; | ||
|
||
private final CompanyNameContainsKeywordsPredicate predicate; | ||
|
||
public FindCompaniesCommand(CompanyNameContainsKeywordsPredicate predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) { | ||
requireNonNull(model); | ||
model.updateFilteredCompanyList(predicate); | ||
return new CommandResult( | ||
String.format(Messages.MESSAGE_COMPANIES_LISTED_OVERVIEW, model.getFilteredCompanyList().size())); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof FindCompaniesCommand)) { | ||
return false; | ||
} | ||
|
||
FindCompaniesCommand otherFindCompaniesCommand = (FindCompaniesCommand) other; | ||
return predicate.equals(otherFindCompaniesCommand.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
42 changes: 42 additions & 0 deletions
42
src/main/java/connectify/logic/parser/FindAllCommandParser.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,42 @@ | ||
package connectify.logic.parser; | ||
|
||
import static connectify.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import java.util.Arrays; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import connectify.logic.commands.FindAllCommand; | ||
import connectify.logic.parser.exceptions.ParseException; | ||
import connectify.model.EntityNameContainsKeywordsPredicate; | ||
|
||
/** | ||
* Parses input arguments and creates a new FindCommand object | ||
*/ | ||
public class FindAllCommandParser implements Parser<FindAllCommand> { | ||
|
||
private static final Logger logger = Logger.getLogger(FindCompaniesCommandParser.class.getName()); | ||
|
||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the FindAllCommand | ||
* and returns a FindAllCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public FindAllCommand parse(String args) throws ParseException { | ||
String trimmedArgs = args.trim(); | ||
if (trimmedArgs.isEmpty()) { | ||
logger.log(Level.WARNING, "Invalid arguments provided for FindCompaniesCommand: Empty string."); | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindAllCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
String[] nameKeywords = trimmedArgs.split("\\s+"); | ||
|
||
logger.log(Level.INFO, | ||
"Successfully parsed FindCompaniesCommand with keywords: " + Arrays.toString(nameKeywords)); | ||
|
||
return new FindAllCommand(new EntityNameContainsKeywordsPredicate(Arrays.asList(nameKeywords))); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/connectify/logic/parser/FindCompaniesCommandParser.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 connectify.logic.parser; | ||
|
||
import static connectify.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import java.util.Arrays; | ||
|
||
import connectify.logic.commands.FindCompaniesCommand; | ||
import connectify.logic.parser.exceptions.ParseException; | ||
import connectify.model.company.CompanyNameContainsKeywordsPredicate; | ||
|
||
/** | ||
* Parses input arguments and creates a new FindCompaniesCommand object | ||
*/ | ||
public class FindCompaniesCommandParser implements Parser<FindCompaniesCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the FindCompaniesCommand | ||
* and returns a FindCompaniesCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public FindCompaniesCommand parse(String args) throws ParseException { | ||
String trimmedArgs = args.trim(); | ||
if (trimmedArgs.isEmpty()) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCompaniesCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
String[] nameKeywords = trimmedArgs.split("\\s+"); | ||
|
||
return new FindCompaniesCommand(new CompanyNameContainsKeywordsPredicate(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
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
47 changes: 47 additions & 0 deletions
47
src/main/java/connectify/model/EntityNameContainsKeywordsPredicate.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,47 @@ | ||
package connectify.model; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import connectify.commons.util.StringUtil; | ||
import connectify.commons.util.ToStringBuilder; | ||
|
||
/** | ||
* Tests that an entity's {@code Name} matches any of the keywords given. | ||
* This entity can be a {@code Person} or a {@code Company}. | ||
*/ | ||
public class EntityNameContainsKeywordsPredicate implements Predicate<Entity> { | ||
private final List<String> keywords; | ||
|
||
public EntityNameContainsKeywordsPredicate(List<String> keywords) { | ||
this.keywords = keywords; | ||
} | ||
|
||
@Override | ||
public boolean test(Entity entity) { | ||
String name = entity.getName().fullName; | ||
return keywords.stream() | ||
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(name, keyword)); | ||
} | ||
|
||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof EntityNameContainsKeywordsPredicate)) { | ||
return false; | ||
} | ||
|
||
EntityNameContainsKeywordsPredicate otherPredicate = (EntityNameContainsKeywordsPredicate) other; | ||
return keywords.equals(otherPredicate.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
Oops, something went wrong.