Skip to content

Commit

Permalink
Merge pull request #98 from ryantzr1/FindFunctionality
Browse files Browse the repository at this point in the history
Add Find Functionality
  • Loading branch information
ryantzr1 authored Oct 27, 2023
2 parents be0587f + a42fed2 commit 61742cc
Show file tree
Hide file tree
Showing 25 changed files with 634 additions and 46 deletions.
2 changes: 2 additions & 0 deletions src/main/java/connectify/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class Messages {
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_DUPLICATE_FIELDS =
"Multiple values specified for the following single-valued field(s): ";
public static final String MESSAGE_COMPANIES_LISTED_OVERVIEW = "%1$d companies listed!";
public static final String MESSAGE_PEOPLE_AND_COMPANIES_LISTED_OVERVIEW = "%1$d people and companies listed!";

/**
* Returns an error message indicating the duplicate prefixes.
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/connectify/logic/commands/FindAllCommand.java
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 src/main/java/connectify/logic/commands/FindCompaniesCommand.java
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

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

public static final String COMMAND_WORD = "find";
public static final String COMMAND_WORD = "findPerson";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names contain any of "
+ "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n"
Expand All @@ -22,7 +22,7 @@ public class FindCommand extends Command {

private final NameContainsKeywordsPredicate predicate;

public FindCommand(NameContainsKeywordsPredicate predicate) {
public FindPeopleCommand(NameContainsKeywordsPredicate predicate) {
this.predicate = predicate;
}

Expand All @@ -41,12 +41,12 @@ public boolean equals(Object other) {
}

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

FindCommand otherFindCommand = (FindCommand) other;
return predicate.equals(otherFindCommand.predicate);
FindPeopleCommand otherFindAllCommand = (FindPeopleCommand) other;
return predicate.equals(otherFindAllCommand.predicate);
}

@Override
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/connectify/logic/parser/ConnectifyParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import connectify.logic.commands.DeletePersonCommand;
import connectify.logic.commands.EditPersonCommand;
import connectify.logic.commands.ExitCommand;
import connectify.logic.commands.FindCommand;
import connectify.logic.commands.FindAllCommand;
import connectify.logic.commands.FindCompaniesCommand;
import connectify.logic.commands.FindPeopleCommand;
import connectify.logic.commands.HelpCommand;
import connectify.logic.commands.ListAllCommand;
import connectify.logic.commands.ListCompaniesCommand;
Expand Down Expand Up @@ -75,8 +77,14 @@ public Command parseCommand(String userInput) throws ParseException {
case ClearCommand.COMMAND_WORD:
return new ClearCommand();

case FindCommand.COMMAND_WORD:
return new FindCommandParser().parse(arguments);
case FindAllCommand.COMMAND_WORD:
return new FindAllCommandParser().parse(arguments);

case FindPeopleCommand.COMMAND_WORD:
return new FindPeopleCommandParser().parse(arguments);

case FindCompaniesCommand.COMMAND_WORD:
return new FindCompaniesCommandParser().parse(arguments);

case ListCompaniesCommand.COMMAND_WORD:
return new ListCompaniesCommand();
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/connectify/logic/parser/FindAllCommandParser.java
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)));
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@

import java.util.Arrays;

import connectify.logic.commands.FindCommand;
import connectify.logic.commands.FindPeopleCommand;
import connectify.logic.parser.exceptions.ParseException;
import connectify.model.person.NameContainsKeywordsPredicate;

/**
* Parses input arguments and creates a new FindCommand object
*/
public class FindCommandParser implements Parser<FindCommand> {
public class FindPeopleCommandParser implements Parser<FindPeopleCommand> {

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

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

return new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
return new FindPeopleCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
}

}
7 changes: 6 additions & 1 deletion src/main/java/connectify/model/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@
* Represents an Entity in Connectify. An entity is either a Company or a Person.
*/
public abstract class Entity {

/**
* Gets the name of the entity.
*
* @return the name of the entity.
*/
public abstract Name getName();
}
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();
}
}
8 changes: 8 additions & 0 deletions src/main/java/connectify/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ public interface Model {
/** Sets the current entity to be displayed to be all the entities*/
void updateToAllEntities();

/**
* Updates the filter of the filtered company and people list to filter by the given {@code predicate}.
* @throws NullPointerException if {@code predicate} is null.
*/
void updateFilteredEntityList(Predicate<Entity> predicate);


/** Get the current entity type */
String getCurrEntity();

Expand All @@ -145,4 +152,5 @@ public interface Model {

/** Check if there is no entities */
Boolean isEmpty();

}
Loading

0 comments on commit 61742cc

Please sign in to comment.