Skip to content

Commit

Permalink
Merge branch 'master' into branch-fixEditCompanyNameBug
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/test/java/seedu/address/model/person/CompanyNameTest.java
  • Loading branch information
Lalelulilulela committed Apr 2, 2024
2 parents f133aa1 + 121a195 commit a2e7c86
Show file tree
Hide file tree
Showing 12 changed files with 426 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
public class FilterSalaryCommand extends FilterCommand {
public static final String COMMAND_WORD = FilterCommand.COMMAND_WORD + " " + PREFIX_SALARY;
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all companies whose "
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Filters all companies whose "
+ "salary range contain any of "
+ "the specified salary and displays them as a list with index numbers.\n"
+ "Parameters: SALARY_RANGE [MORE_SALARY_RANGE]...\n"
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/seedu/address/logic/commands/FilterTagCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

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


/**
* Filters and lists all persons in address book whose tags contains any of the argument keywords.
*/
public class FilterTagCommand extends FilterCommand {

public static final String COMMAND_WORD = FilterCommand.COMMAND_WORD + " " + PREFIX_TAG;

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Filters all contacts whose tags contain any of "
+ "the specified tags and displays them as a list with index numbers.\n"
+ "Parameters: TAG [MORE_TAGS]...\n"
+ "Example: " + COMMAND_WORD + " owes money";

private final TagContainsKeywordsPredicate predicate;

public FilterTagCommand(TagContainsKeywordsPredicate predicate) {
this.predicate = predicate;
}

@Override

public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(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 FilterTagCommand)) {
return false;
}

FilterTagCommand otherFindCommand = (FilterTagCommand) other;
return predicate.equals(otherFindCommand.predicate);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("tag", predicate)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public FilterCommand parse(String args) throws ParseException {
if (argMultimap.getValue(PREFIX_SALARY).isPresent()) {
return new FilterSalaryCommandParser().parse(argMultimap.getValue(PREFIX_SALARY).get());
} else if (argMultimap.getValue(PREFIX_TAG).isPresent()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FilterCommand.MESSAGE_USAGE));
// to be implemented
return new FilterTagCommandParser().parse(argMultimap.getValue(PREFIX_TAG).get());
} else {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, FilterCommand.MESSAGE_USAGE));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import java.util.ArrayList;
import java.util.List;

import seedu.address.logic.commands.FilterTagCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.TagContainsKeywordsPredicate;
import seedu.address.model.tag.Tag;


/**
* Parses input arguments and creates a new FindCommand object
*/
public class FilterTagCommandParser implements Parser<FilterTagCommand> {

/**
* Parses the given {@code String} of arguments in the context of the FilterTagCommand
* and returns a FilterTagCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public FilterTagCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim();
if (trimmedArgs.isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FilterTagCommand.MESSAGE_USAGE));
}
String[] tagKeywords = trimmedArgs.split("\\s+");
return new FilterTagCommand(new TagContainsKeywordsPredicate(createTags(tagKeywords)));
}

/**
* Parses {@code tagKeywords} into a {@code List<Tag>}.
*/
public static List<Tag> createTags(String... tagKeywords) throws ParseException {
List<Tag> tags = new ArrayList<>();
for (String keyword : tagKeywords) {
tags.add(ParserUtil.parseTag(keyword));
}
return tags;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package seedu.address.model.person;

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

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.tag.Tag;

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

public TagContainsKeywordsPredicate(List<Tag> tags) {
this.tags = tags;
}

@Override
public boolean test(Person person) {
Set<Tag> personTags = person.getTags();
return tags.stream()
.anyMatch(personTags::contains);
}

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

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

TagContainsKeywordsPredicate otherTagContainsKeywordsPredicate = (TagContainsKeywordsPredicate) other;
return tags.equals(otherTagContainsKeywordsPredicate.tags);
}

@Override
public String toString() {
return new ToStringBuilder(this).add("tags", tags).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void execute_oneSalary_multiplePersonsFound() {
@Test
public void execute_multipleSalaries_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 2);
SalaryContainsKeywordsPredicate predicate = preparePredicate(new SalaryRange("100"),
SalaryContainsKeywordsPredicate predicate = preparePredicate(new SalaryRange("1000"),
new SalaryRange(">=10000"));
FilterSalaryCommand command = new FilterSalaryCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package seedu.address.logic.commands;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.parser.FilterTagCommandParser.createTags;
import static seedu.address.testutil.TypicalPersons.ALICE;
import static seedu.address.testutil.TypicalPersons.BENSON;
import static seedu.address.testutil.TypicalPersons.DANIEL;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import java.util.Arrays;
import java.util.Collections;

import org.junit.jupiter.api.Test;

import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.TagContainsKeywordsPredicate;


/**
* Contains integration tests (interaction with the Model) for {@code FilterTagCommand}.
*/
public class FilterTagCommandTest {
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void equals() throws ParseException {
TagContainsKeywordsPredicate firstPredicate =
new TagContainsKeywordsPredicate(createTags("first"));
TagContainsKeywordsPredicate secondPredicate =
new TagContainsKeywordsPredicate(createTags("second"));

FilterTagCommand findTagFirstCommand = new FilterTagCommand(firstPredicate);
FilterTagCommand findTagSecondCommand = new FilterTagCommand(secondPredicate);

// same object -> returns true
assertTrue(findTagFirstCommand.equals(findTagFirstCommand));

// same values -> returns true
FilterTagCommand findTagFirstCommandCopy = new FilterTagCommand(firstPredicate);
assertTrue(findTagFirstCommand.equals(findTagFirstCommandCopy));

// different types -> returns false
assertFalse(findTagFirstCommand.equals(1));

// null -> returns false
assertFalse(findTagFirstCommand.equals(null));

// different person -> returns false
assertFalse(findTagFirstCommand.equals(findTagSecondCommand));
}

@Test
public void execute_zeroKeywords_noPersonFound() throws ParseException {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0);
TagContainsKeywordsPredicate predicate = preparePredicate(" ");
FilterTagCommand command = new FilterTagCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getFilteredPersonList());
}

@Test
public void execute_multipleKeywords_multiplePersonsFound() throws ParseException {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3);
TagContainsKeywordsPredicate predicate = preparePredicate("friends friend");
FilterTagCommand command = new FilterTagCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Arrays.asList(ALICE, BENSON, DANIEL), model.getFilteredPersonList());
}

@Test
public void toStringMethod() throws ParseException {
TagContainsKeywordsPredicate predicate = new TagContainsKeywordsPredicate(createTags("friends"));
FilterTagCommand filterTagCommand = new FilterTagCommand(predicate);
String expected = FilterTagCommand.class.getCanonicalName() + "{tag=" + predicate + "}";
assertEquals(expected, filterTagCommand.toString());
}

/**
* Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}.
*/
private TagContainsKeywordsPredicate preparePredicate(String userInput) throws ParseException {
return new TagContainsKeywordsPredicate(createTags(userInput.split("\\s+")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.Messages.MESSAGE_UNKNOWN_COMMAND;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SALARY;
import static seedu.address.logic.parser.FilterTagCommandParser.createTags;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;

Expand All @@ -22,6 +23,7 @@
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FilterCommand;
import seedu.address.logic.commands.FilterSalaryCommand;
import seedu.address.logic.commands.FilterTagCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
Expand All @@ -31,6 +33,7 @@
import seedu.address.model.person.Person;
import seedu.address.model.person.SalaryContainsKeywordsPredicate;
import seedu.address.model.person.SalaryRange;
import seedu.address.model.person.TagContainsKeywordsPredicate;
import seedu.address.testutil.EditPersonDescriptorBuilder;
import seedu.address.testutil.PersonBuilder;
import seedu.address.testutil.PersonUtil;
Expand Down Expand Up @@ -82,13 +85,6 @@ public void parseCommand_find() throws Exception {
assertEquals(new FindCommand(new NameOrCompanyNameContainsKeywordsPredicate(keywords)), command);
}








@Test
public void parseCommand_filter_salary() throws Exception {
List<String> keywords = Arrays.asList("2000", "9000-10000", "4000");
Expand All @@ -98,6 +94,15 @@ public void parseCommand_filter_salary() throws Exception {
assertEquals(new FilterSalaryCommand(new SalaryContainsKeywordsPredicate(Arrays.asList(new SalaryRange("2000"),
new SalaryRange("9000-10000"), new SalaryRange("4000")))), command);
}
@Test
public void parseCommand_filter_tag() throws Exception {
List<String> keywords = Arrays.asList("foo", "bar", "baz");
FilterTagCommand command = (FilterTagCommand) parser.parseCommand(
FilterTagCommand.COMMAND_WORD + " " + keywords.stream().collect(Collectors.joining(" ")));
assertEquals(new FilterTagCommand(new TagContainsKeywordsPredicate(
createTags("foo", "bar", "baz"))), command);
}

@Test
public void parseCommand_help() throws Exception {
assertTrue(parser.parseCommand(HelpCommand.COMMAND_WORD) instanceof HelpCommand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

import seedu.address.logic.commands.FilterCommand;
import seedu.address.logic.commands.FilterSalaryCommand;
import seedu.address.logic.commands.FilterTagCommand;
import seedu.address.model.person.SalaryContainsKeywordsPredicate;
import seedu.address.model.person.SalaryRange;
import seedu.address.model.person.TagContainsKeywordsPredicate;
import seedu.address.model.tag.Tag;

public class FilterCommandParserTest {
private FilterCommandParser parser = new FilterCommandParser();
Expand All @@ -43,4 +46,12 @@ public void parse_validArgs_returnsFilterSalaryCommand() {
assertParseSuccess(parser, " " + PREFIX_SALARY + "2000 5000-7000", expected);
}

@Test
public void parse_validArgs_returnsFilterTagCommand() {
FilterCommand expected =
new FilterTagCommand(new TagContainsKeywordsPredicate(Arrays.asList(new Tag("friends"),
new Tag("owesMoney"))));
assertParseSuccess(parser, " " + PREFIX_TAG + "friends owesMoney", expected);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;
import static seedu.address.logic.parser.FilterTagCommandParser.createTags;

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.FilterTagCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.TagContainsKeywordsPredicate;


public class FilterTagCommandParserTest {

private FilterTagCommandParser parser = new FilterTagCommandParser();

@Test
public void parse_emptyArg_throwsParseException() {
assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT,
FilterTagCommand.MESSAGE_USAGE));
}

@Test
public void parse_validArgs_returnsFilterTagCommand() throws ParseException {
// no leading and trailing whitespaces
FilterTagCommand expectedFilterTagCommand =
new FilterTagCommand(new TagContainsKeywordsPredicate(createTags("Alice", "Bob")));
assertParseSuccess(parser, "Alice Bob", expectedFilterTagCommand);

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

0 comments on commit a2e7c86

Please sign in to comment.