Skip to content

Commit

Permalink
Merge pull request #51 from KrashKart/branch-Restructure-Find
Browse files Browse the repository at this point in the history
Reformat find by name command
  • Loading branch information
yooplo authored Oct 9, 2024
2 parents 28594f9 + d5b0b4c commit c9a4c6c
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 13 deletions.
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.person.ContainsKeywordsPredicate;

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

public static final String COMMAND_WORD = "find";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names, contacts or emails "
+ "contain any of the specified keywords (case-insensitive) and displays them as a list with indices.\n"
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n"
+ "Example: " + COMMAND_WORD + " alice bob charlie";

private final ContainsKeywordsPredicate predicate;

public AbstractFindCommand(ContainsKeywordsPredicate predicate) {
this.predicate = predicate;
}

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(this.predicate);
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

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

AbstractFindCommand otherFindCommand = (AbstractFindCommand) other;
return this.predicate.equals(otherFindCommand.predicate);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("predicate", this.predicate)
.toString();
}
}
12 changes: 6 additions & 6 deletions src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

/**
* 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 static final String COMMAND_WORD = "find";

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"
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 indices.\n"
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n"
+ "Example: " + COMMAND_WORD + " alice bob charlie";

Expand All @@ -29,7 +29,7 @@ public FindCommand(NameContainsKeywordsPredicate predicate) {
@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(predicate);
model.updateFilteredPersonList(this.predicate);
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
}
Expand All @@ -46,13 +46,13 @@ public boolean equals(Object other) {
}

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

@Override
public String toString() {
return new ToStringBuilder(this)
.add("predicate", predicate)
.add("predicate", this.predicate)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package seedu.address.model.person;

import java.util.List;

import seedu.address.commons.util.StringUtil;
import seedu.address.commons.util.ToStringBuilder;

/**
* Tests that a {@code Person}'s {@code Name} matches any of the keywords given.
*/
public class ContactContainsKeywordsPredicate extends ContainsKeywordsPredicate {

public ContactContainsKeywordsPredicate(List<String> keywords) {
super(keywords);
}

@Override
public boolean test(Person person) {
return this.getKeywords().stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getPhone().value, keyword));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

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

ContactContainsKeywordsPredicate otherNameContainsKeywordsPredicate = (ContactContainsKeywordsPredicate) other;
return this.getKeywords().equals(otherNameContainsKeywordsPredicate.getKeywords());
}

@Override
public String toString() {
return new ToStringBuilder(this).add("keywords", this.getKeywords()).toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package seedu.address.model.person;

import java.util.List;
import java.util.function.Predicate;

import seedu.address.commons.util.ToStringBuilder;

/**
* Tests that a {@code Person}'s {@code Name} matches any of the keywords given.
*/
public abstract class ContainsKeywordsPredicate implements Predicate<Person> {
private final List<String> keywords;

public ContainsKeywordsPredicate(List<String> keywords) {
this.keywords = keywords;
}

protected List<String> getKeywords() {
return this.keywords;
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

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

ContainsKeywordsPredicate otherNameContainsKeywordsPredicate = (ContainsKeywordsPredicate) other;
return keywords.equals(otherNameContainsKeywordsPredicate.keywords);
}

@Override
public String toString() {
return new ToStringBuilder(this).add("keywords", keywords).toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package seedu.address.model.person;

import java.util.List;

import seedu.address.commons.util.StringUtil;
import seedu.address.commons.util.ToStringBuilder;

/**
* Tests that a {@code Person}'s {@code Name} matches any of the keywords given.
*/
public class EmailContainsKeywordsPredicate extends ContainsKeywordsPredicate {

public EmailContainsKeywordsPredicate(List<String> keywords) {
super(keywords);
}

@Override
public boolean test(Person person) {
return super.getKeywords().stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getEmail().value, keyword));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

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

EmailContainsKeywordsPredicate otherNameContainsKeywordsPredicate = (EmailContainsKeywordsPredicate) other;
return this.getKeywords().equals(otherNameContainsKeywordsPredicate.getKeywords());
}

@Override
public String toString() {
return new ToStringBuilder(this).add("keywords", this.getKeywords()).toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package seedu.address.model.person;

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 Person}'s {@code Name} matches any of the keywords given.
*/
public class NameContainsKeywordsPredicate implements Predicate<Person> {
private final List<String> keywords;
public class NameContainsKeywordsPredicate extends ContainsKeywordsPredicate {

public NameContainsKeywordsPredicate(List<String> keywords) {
this.keywords = keywords;
super(keywords);
}

@Override
public boolean test(Person person) {
return keywords.stream()
return this.getKeywords().stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(person.getName().fullName, keyword));
}

Expand All @@ -34,11 +32,11 @@ public boolean equals(Object other) {
}

NameContainsKeywordsPredicate otherNameContainsKeywordsPredicate = (NameContainsKeywordsPredicate) other;
return keywords.equals(otherNameContainsKeywordsPredicate.keywords);
return this.getKeywords().equals(otherNameContainsKeywordsPredicate.getKeywords());
}

@Override
public String toString() {
return new ToStringBuilder(this).add("keywords", keywords).toString();
return new ToStringBuilder(this).add("keywords", this.getKeywords()).toString();
}
}

0 comments on commit c9a4c6c

Please sign in to comment.