Skip to content

Commit

Permalink
Allow search using telegram handles
Browse files Browse the repository at this point in the history
  • Loading branch information
yyihaoc committed Oct 23, 2024
1 parent 9332429 commit 111dbaa
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
17 changes: 12 additions & 5 deletions src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import seedu.address.model.Model;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.RoleContainsKeywordsPredicate;
import seedu.address.model.person.TelegramContainsKeywordsPredicate;

/**
* Finds and lists all persons in address book whose name contains any of the argument keywords.
Expand All @@ -19,27 +20,31 @@ public class FindCommand extends Command {

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"
+ "Parameters: [n/nameKeyword] [r/roleKeyword] ...\n"
+ "Example: " + COMMAND_WORD + " n/alice tan n/bob r/member n/charlie";
+ "Parameters: [n/nameKeyword] [r/roleKeyword] [t/telegramKeyword] ...\n"
+ "Example: " + COMMAND_WORD + " n/alice tan n/bob r/member n/charlie t/ccharliee";

private final NameContainsKeywordsPredicate namePredicate;

private final RoleContainsKeywordsPredicate rolePredicate;

private final TelegramContainsKeywordsPredicate telegramPredicate;

/**
* @param namePredicate determines whether a person has a name that satisfy a condition
* @param rolePredicate determines whether a person has a role that satisfy a condition
*/
public FindCommand(NameContainsKeywordsPredicate namePredicate, RoleContainsKeywordsPredicate rolePredicate) {
public FindCommand(NameContainsKeywordsPredicate namePredicate, RoleContainsKeywordsPredicate rolePredicate,
TelegramContainsKeywordsPredicate telegramPredicate) {
this.namePredicate = namePredicate;
this.rolePredicate = rolePredicate;
this.telegramPredicate = telegramPredicate;
}

@Override
public CommandResult execute(Model model) {
requireNonNull(model);

model.updateFilteredPersonList(rolePredicate.or(namePredicate));
model.updateFilteredPersonList(rolePredicate.or(namePredicate).or(telegramPredicate));

return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
Expand All @@ -58,14 +63,16 @@ public boolean equals(Object other) {

FindCommand otherFindCommand = (FindCommand) other;
return namePredicate.equals(otherFindCommand.namePredicate)
&& rolePredicate.equals(otherFindCommand.rolePredicate);
&& rolePredicate.equals(otherFindCommand.rolePredicate)
&& telegramPredicate.equals(otherFindCommand.telegramPredicate);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("namePredicate", namePredicate)
.add("rolePredicate", rolePredicate)
.add("telegramPredicate", telegramPredicate)
.toString();
}
}
14 changes: 10 additions & 4 deletions src/main/java/seedu/address/logic/parser/FindCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ROLE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TELEGRAM;

import java.util.List;
import java.util.stream.Stream;
Expand All @@ -11,6 +12,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.RoleContainsKeywordsPredicate;
import seedu.address.model.person.TelegramContainsKeywordsPredicate;

/**
* Parses input arguments and creates a new FindCommand object
Expand All @@ -23,28 +25,32 @@ public class FindCommandParser implements Parser<FindCommand> {
* @throws ParseException if the user input does not conform the expected format
*/
public FindCommand parse(String args) throws ParseException {
ArgumentMultimap argumentMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_ROLE);
ArgumentMultimap argumentMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_ROLE, PREFIX_TELEGRAM);

if (!areSomePrefixesPresent(argumentMultimap, PREFIX_NAME, PREFIX_ROLE)
if (!areSomePrefixesPresent(argumentMultimap, PREFIX_NAME, PREFIX_ROLE, PREFIX_TELEGRAM)
|| !argumentMultimap.getPreamble().isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}
List<String> nameKeywords = argumentMultimap.getAllValues(PREFIX_NAME);
List<String> roleKeywords = argumentMultimap.getAllValues(PREFIX_ROLE);
List<String> telegramKeywords = argumentMultimap.getAllValues(PREFIX_TELEGRAM);

boolean hasEmptyInput = nameKeywords.stream().anyMatch(str -> str.trim().isEmpty())
|| roleKeywords.stream().anyMatch(str -> str.trim().isEmpty());
|| roleKeywords.stream().anyMatch(str -> str.trim().isEmpty())
|| telegramKeywords.stream().anyMatch(str -> str.trim().isEmpty());

if (hasEmptyInput) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindCommand.MESSAGE_USAGE));
}

nameKeywords = nameKeywords.stream().map(String::trim).toList();
roleKeywords = roleKeywords.stream().map(String::trim).toList();
telegramKeywords = telegramKeywords.stream().map(String::trim).toList();

return new FindCommand(new NameContainsKeywordsPredicate(nameKeywords),
new RoleContainsKeywordsPredicate(roleKeywords));
new RoleContainsKeywordsPredicate(roleKeywords),
new TelegramContainsKeywordsPredicate(telegramKeywords));
}

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

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

@Override
public boolean test(Person person) {
return keywords.stream()
.anyMatch(keyword -> person.getTelegram().equals(new Telegram(keyword)));
}

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

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

TelegramContainsKeywordsPredicate otherTelegramContainsKeywordsPredicate = (TelegramContainsKeywordsPredicate) other;
return keywords.equals(otherTelegramContainsKeywordsPredicate.keywords);
}

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

0 comments on commit 111dbaa

Please sign in to comment.