Skip to content

Commit

Permalink
Fix finding profiles by address and condition
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-setyawan committed Mar 26, 2024
1 parent ab3acd2 commit 8b03593
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 11 deletions.
21 changes: 21 additions & 0 deletions src/main/java/seedu/address/commons/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ public static boolean containsWordIgnoreCase(String sentence, String word) {
.anyMatch(preppedWord::equalsIgnoreCase);
}

/**
* Returns true if the {@code sentence} contains the {@code word}.
* Ignores case, but a full word match is required.
* <br>examples:<pre>
* containsWordIgnoreCase("ABc def", "abc") == true
* containsWordIgnoreCase("ABc def", "DEF") == true
* containsWordIgnoreCase("ABc def", "AB") == false //not a full word match
* </pre>
* @param sentence cannot be null
* @param phrase cannot be null, cannot be empty, must be a single word
*/
public static boolean containsPhraseIgnoreCase(String sentence, String phrase) {
requireNonNull(sentence);
requireNonNull(phrase);

String preppedPhrase = phrase.trim();
checkArgument(!preppedPhrase.isEmpty(), "Word parameter cannot be empty");

return sentence.toLowerCase().contains(phrase.toLowerCase());
}

/**
* Returns a detailed message of the t, including the stack trace.
*/
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import static java.util.Objects.requireNonNull;

import java.util.function.Predicate;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.model.Model;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.Person;

/**
* Finds and lists all persons in address book whose name contains any of the argument keywords.
Expand All @@ -20,10 +22,10 @@ public class FindCommand extends Command {
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n"
+ "Example: " + COMMAND_WORD + " alice bob charlie";

private final NameContainsKeywordsPredicate predicate;
private final Predicate<Person> predicate;
//TODO: add nric contains keywords

public FindCommand(NameContainsKeywordsPredicate predicate) {
public FindCommand(Predicate<Person> predicate) {
this.predicate = predicate;
}

Expand Down
31 changes: 27 additions & 4 deletions src/main/java/seedu/address/logic/parser/FindCommandParser.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CONDITION;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;

import java.util.Arrays;
import java.util.List;

import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.AddressContainsKeywordsPredicate;
import seedu.address.model.person.ConditionContainsKeywordsPredicate;
import seedu.address.model.person.NameContainsKeywordsPredicate;

/**
Expand All @@ -25,9 +31,26 @@ public FindCommand parse(String args) throws ParseException {
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}

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

return new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList(nameKeywords)));
/*
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_CONDITION);
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_ADDRESS, PREFIX_CONDITION);
*/

if (trimmedArgs.startsWith(PREFIX_NAME.getPrefix())) {
String[] nameKeywords = trimmedArgs.substring(2).trim().split("\\s+");
List<String> list = Arrays.asList(nameKeywords);
return new FindCommand(new NameContainsKeywordsPredicate(list));
} else if (trimmedArgs.startsWith(PREFIX_ADDRESS.getPrefix())) {
String[] addressKeywords = trimmedArgs.substring(2).trim().split(",");
List<String> list = Arrays.asList(addressKeywords);
return new FindCommand(new AddressContainsKeywordsPredicate(list));
} else if (trimmedArgs.startsWith(PREFIX_CONDITION.getPrefix())) {
String[] conditionKeywords = trimmedArgs.substring(4).trim().split(",");
List<String> list = Arrays.asList(conditionKeywords);
return new FindCommand(new ConditionContainsKeywordsPredicate(list));
} else {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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 Address} matches any of the keywords given.
*/
public class AddressContainsKeywordsPredicate implements Predicate<Person> {
private final List<String> keywords;

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

@Override
public boolean test(Person person) {
return keywords.stream()
.anyMatch(keyword -> StringUtil.containsPhraseIgnoreCase(person.getAddress().toString(), keyword));
}

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

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

AddressContainsKeywordsPredicate otherAddressContainsKeywordsPredicate =
(AddressContainsKeywordsPredicate) other;
return keywords.equals(otherAddressContainsKeywordsPredicate.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,45 @@
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 Condition} matches any of the keywords given.
*/
public class ConditionContainsKeywordsPredicate implements Predicate<Person> {
private final List<String> keywords;

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

@Override
public boolean test(Person person) {
return keywords.stream().anyMatch(
keyword -> StringUtil.containsPhraseIgnoreCase(person.getCondition().toString(), keyword));
}

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

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

ConditionContainsKeywordsPredicate otherConditionContainsKeywordsPredicate =
(ConditionContainsKeywordsPredicate) other;
return keywords.equals(otherConditionContainsKeywordsPredicate.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 @@ -25,10 +25,10 @@ public void parse_validArgs_returnsFindCommand() {
// no leading and trailing whitespaces
FindCommand expectedFindCommand =
new FindCommand(new NameContainsKeywordsPredicate(Arrays.asList("Alice", "Bob")));
assertParseSuccess(parser, "Alice Bob", expectedFindCommand);
assertParseSuccess(parser, "n/Alice Bob", expectedFindCommand);

// multiple whitespaces between keywords
assertParseSuccess(parser, " \n Alice \n \t Bob \t", expectedFindCommand);
assertParseSuccess(parser, "n/ \n Alice \n \t Bob \t", expectedFindCommand);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public void test_nameDoesNotContainKeywords_returnsFalse() {
assertFalse(predicate.test(new PersonBuilder().withName("Alice Bob").build()));

// Keywords match phone, email and address, but does not match name
predicate = new NameContainsKeywordsPredicate(Arrays.asList("12345", "alice@email.com", "Main", "Street"));
assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("12345")
predicate = new NameContainsKeywordsPredicate(Arrays.asList("91234567", "alice@email.com", "Main", "Street"));
assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("91234567")
.withAddress("Main Street").build()));
}

Expand Down

0 comments on commit 8b03593

Please sign in to comment.