From 896ed00732e7d2b5a47f0a4b979f5091d2e384ad Mon Sep 17 00:00:00 2001 From: Rubesh S Date: Sat, 28 Oct 2023 18:15:56 +0800 Subject: [PATCH] Add rudimentary name and amount functionality to FilterCommand --- .../unicash/logic/commands/FilterCommand.java | 86 ++++++++++++ .../unicash/logic/commands/FindCommand.java | 6 +- .../logic/parser/FilterCommandParser.java | 67 +++++++++ .../logic/parser/FindCommandParser.java | 4 +- .../unicash/logic/parser/UniCashParser.java | 4 + ...ansactionContainsAllKeywordsPredicate.java | 129 ++++++++++++++++++ ...nsactionContainsAnyKeywordsPredicate.java} | 10 +- ...nsactionNameContainsKeywordsPredicate.java | 1 + .../logic/commands/CommandTestUtil.java | 4 +- .../logic/commands/FindCommandTest.java | 22 +-- .../logic/parser/FindCommandParserTest.java | 4 +- .../logic/parser/UniCashParserTest.java | 4 +- .../java/unicash/model/ModelManagerTest.java | 4 +- ...tionContainsAnyKeywordsPredicateTest.java} | 34 ++--- 14 files changed, 333 insertions(+), 46 deletions(-) create mode 100644 src/main/java/unicash/logic/commands/FilterCommand.java create mode 100644 src/main/java/unicash/logic/parser/FilterCommandParser.java create mode 100644 src/main/java/unicash/model/transaction/predicates/TransactionContainsAllKeywordsPredicate.java rename src/main/java/unicash/model/transaction/predicates/{TransactionContainsKeywordsPredicate.java => TransactionContainsAnyKeywordsPredicate.java} (84%) rename src/test/java/unicash/model/transaction/predicates/{TransactionContainsKeywordsPredicateTest.java => TransactionContainsAnyKeywordsPredicateTest.java} (62%) diff --git a/src/main/java/unicash/logic/commands/FilterCommand.java b/src/main/java/unicash/logic/commands/FilterCommand.java new file mode 100644 index 00000000000..7837be779a3 --- /dev/null +++ b/src/main/java/unicash/logic/commands/FilterCommand.java @@ -0,0 +1,86 @@ +package unicash.logic.commands; + +import static java.util.Objects.requireNonNull; +import static unicash.logic.parser.CliSyntax.PREFIX_AMOUNT; +import static unicash.logic.parser.CliSyntax.PREFIX_CATEGORY; +import static unicash.logic.parser.CliSyntax.PREFIX_DATETIME; +import static unicash.logic.parser.CliSyntax.PREFIX_LOCATION; +import static unicash.logic.parser.CliSyntax.PREFIX_NAME; +import static unicash.logic.parser.CliSyntax.PREFIX_TYPE; + +import unicash.commons.util.ToStringBuilder; +import unicash.logic.UniCashMessages; +import unicash.logic.commands.exceptions.CommandException; +import unicash.model.Model; +import unicash.model.transaction.predicates.TransactionContainsAllKeywordsPredicate; + +/** + * Filters the displayed Transactions according to certain parameters + */ +public class FilterCommand extends Command { + public static final String COMMAND_WORD = "filter"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Filters the transactions in UniCa$h " + + "according to the specified properties" + + "All properties must match in order for the transaction to be displayed." + + "Any combination of properties may be provided but at least one property must be provided." + + "\n\n" + + "[" + PREFIX_NAME + "NAME] " + + "[" + PREFIX_TYPE + "TYPE] " + + "[" + PREFIX_AMOUNT + "AMOUNT] " + + "[" + PREFIX_DATETIME + "DATETIME] " + + "[" + PREFIX_LOCATION + "LOCATION]" + + "[" + PREFIX_CATEGORY + "CATEGORY]..." + + "\n\n" + + "Example: " + COMMAND_WORD + " " + + PREFIX_NAME + "Buying groceries " + + PREFIX_TYPE + "expense " + + PREFIX_AMOUNT + "300 " + + PREFIX_DATETIME + "18-08-2001 19:30 " + + PREFIX_LOCATION + "ntuc" + + PREFIX_CATEGORY + "household"; + + public static final String MESSAGE_SUCCESS = "Filtered %1$s Transactions"; + + private final TransactionContainsAllKeywordsPredicate predicate; + + /** + * Creates an FilterCommand to filter the transactions list + */ + public FilterCommand(TransactionContainsAllKeywordsPredicate predicate) { + requireNonNull(predicate); + this.predicate = predicate; + } + + @Override + public CommandResult execute(Model model) throws CommandException { + requireNonNull(model); + model.updateFilteredTransactionList(predicate); + int listSize = model.getFilteredTransactionList().size(); + return new CommandResult(String.format(UniCashMessages.MESSAGE_TRANSACTIONS_LISTED_OVERVIEW, + listSize)); + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof FilterCommand)) { + return false; + } + + FilterCommand otherCommand = (FilterCommand) other; + return predicate.equals(otherCommand.predicate); + + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .add("predicate", predicate) + .toString(); + } +} diff --git a/src/main/java/unicash/logic/commands/FindCommand.java b/src/main/java/unicash/logic/commands/FindCommand.java index 0ce01b7eb33..9783a942d9b 100644 --- a/src/main/java/unicash/logic/commands/FindCommand.java +++ b/src/main/java/unicash/logic/commands/FindCommand.java @@ -5,7 +5,7 @@ import unicash.commons.util.ToStringBuilder; import unicash.logic.UniCashMessages; import unicash.model.Model; -import unicash.model.transaction.predicates.TransactionContainsKeywordsPredicate; +import unicash.model.transaction.predicates.TransactionContainsAnyKeywordsPredicate; /** * Finds and lists all transactions in UniCa$h whose name contains any of the argument keywords. @@ -22,9 +22,9 @@ public class FindCommand extends Command { + "\n" + "Example: " + COMMAND_WORD + " chicken rice"; - private final TransactionContainsKeywordsPredicate predicate; + private final TransactionContainsAnyKeywordsPredicate predicate; - public FindCommand(TransactionContainsKeywordsPredicate predicate) { + public FindCommand(TransactionContainsAnyKeywordsPredicate predicate) { this.predicate = predicate; } diff --git a/src/main/java/unicash/logic/parser/FilterCommandParser.java b/src/main/java/unicash/logic/parser/FilterCommandParser.java new file mode 100644 index 00000000000..094eb6feafc --- /dev/null +++ b/src/main/java/unicash/logic/parser/FilterCommandParser.java @@ -0,0 +1,67 @@ +package unicash.logic.parser; + +import static java.util.Objects.requireNonNull; +import static unicash.logic.UniCashMessages.MESSAGE_INVALID_COMMAND_FORMAT; +import static unicash.logic.parser.CliSyntax.PREFIX_AMOUNT; +import static unicash.logic.parser.CliSyntax.PREFIX_CATEGORY; +import static unicash.logic.parser.CliSyntax.PREFIX_DATETIME; +import static unicash.logic.parser.CliSyntax.PREFIX_LOCATION; +import static unicash.logic.parser.CliSyntax.PREFIX_NAME; +import static unicash.logic.parser.CliSyntax.PREFIX_TYPE; + +import java.util.List; + +import unicash.logic.commands.FilterCommand; +import unicash.logic.parser.exceptions.ParseException; +import unicash.model.commons.Amount; +import unicash.model.transaction.Name; +import unicash.model.transaction.predicates.TransactionContainsAllKeywordsPredicate; + +/** + * Parses input arguments and creates a new FilterCommand object + */ +public class FilterCommandParser implements Parser { + + public static final String EMPTY_STRING = ""; + public static final List EMPTY_STRING_LIST = List.of(EMPTY_STRING); + + private static TransactionContainsAllKeywordsPredicate filterPredicate = + new TransactionContainsAllKeywordsPredicate(); + + /** + * Parses the given {@code String} of arguments in the context of the FilterCommand + * and returns an FilterCommand object for execution. + * + * @throws ParseException if the user input does not conform the expected format + */ + public FilterCommand parse(String args) throws ParseException { + requireNonNull(args); + ArgumentMultimap argMultimap = + ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_TYPE, PREFIX_AMOUNT, PREFIX_DATETIME, + PREFIX_CATEGORY, PREFIX_LOCATION); + + String trimmedArgs = args.trim(); + if (trimmedArgs.isEmpty()) { + throw new ParseException( + String.format(MESSAGE_INVALID_COMMAND_FORMAT, FilterCommand.MESSAGE_USAGE)); + } + + argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_TYPE, PREFIX_AMOUNT, PREFIX_DATETIME, + PREFIX_LOCATION); + + + if (argMultimap.getValue(PREFIX_NAME).isPresent()) { + Name transactionName = ParserUtil.parseTransactionName(argMultimap.getValue(PREFIX_NAME).get()); + filterPredicate.setName(transactionName.toString()); + + } + + if (argMultimap.getValue(PREFIX_AMOUNT).isPresent()) { + Amount transactionAmount = ParserUtil.parseAmount(argMultimap.getValue(PREFIX_AMOUNT).get()); + filterPredicate.setAmount(Amount.amountToDecimalString(transactionAmount)); + } + + return new FilterCommand(filterPredicate); + } + +} diff --git a/src/main/java/unicash/logic/parser/FindCommandParser.java b/src/main/java/unicash/logic/parser/FindCommandParser.java index 29feff5355e..dece320ca6d 100644 --- a/src/main/java/unicash/logic/parser/FindCommandParser.java +++ b/src/main/java/unicash/logic/parser/FindCommandParser.java @@ -6,7 +6,7 @@ import unicash.logic.commands.FindCommand; import unicash.logic.parser.exceptions.ParseException; -import unicash.model.transaction.predicates.TransactionContainsKeywordsPredicate; +import unicash.model.transaction.predicates.TransactionContainsAnyKeywordsPredicate; /** * Parses input arguments and creates a new FindCommand object @@ -27,7 +27,7 @@ public FindCommand parse(String args) throws ParseException { String[] nameKeywords = trimmedArgs.split("\\s+"); - return new FindCommand(new TransactionContainsKeywordsPredicate( + return new FindCommand(new TransactionContainsAnyKeywordsPredicate( Arrays.asList(nameKeywords))); } diff --git a/src/main/java/unicash/logic/parser/UniCashParser.java b/src/main/java/unicash/logic/parser/UniCashParser.java index 0ed3980edd7..51e9ac34d44 100644 --- a/src/main/java/unicash/logic/parser/UniCashParser.java +++ b/src/main/java/unicash/logic/parser/UniCashParser.java @@ -14,6 +14,7 @@ import unicash.logic.commands.DeleteTransactionCommand; import unicash.logic.commands.EditTransactionCommand; import unicash.logic.commands.ExitCommand; +import unicash.logic.commands.FilterCommand; import unicash.logic.commands.FindCommand; import unicash.logic.commands.GetCommand; import unicash.logic.commands.GetTotalExpenditureCommand; @@ -93,6 +94,9 @@ public Command parseCommand(String userInput) throws ParseException { case SummaryCommand.COMMAND_WORD: return new SummaryCommand(); + case FilterCommand.COMMAND_WORD: + return new FilterCommandParser().parse(arguments); + default: logger.finer("This user input caused a ParseException: " + userInput); throw new ParseException(MESSAGE_UNKNOWN_COMMAND); diff --git a/src/main/java/unicash/model/transaction/predicates/TransactionContainsAllKeywordsPredicate.java b/src/main/java/unicash/model/transaction/predicates/TransactionContainsAllKeywordsPredicate.java new file mode 100644 index 00000000000..81e974c4dc0 --- /dev/null +++ b/src/main/java/unicash/model/transaction/predicates/TransactionContainsAllKeywordsPredicate.java @@ -0,0 +1,129 @@ +package unicash.model.transaction.predicates; + +import java.util.List; +import java.util.function.Predicate; + +import unicash.commons.util.ToStringBuilder; +import unicash.model.transaction.Transaction; + + + + +/** + * Tests that any of a {@code Transactions}'s properties matches all the keywords given. + * Matching in this context either means a partial match of the keyword with the query, or + * a full match of the keyword, repeated for each keyword present in the list of keywords. + * + *

The matching depends on the property of the Transaction being matched, and the + * most appropriate matching is specified within the associated property predicate. This class + * simulates a composed predicate that represents a short-circuiting logical AND of all property + * predicates. + */ +public class TransactionContainsAllKeywordsPredicate + implements Predicate { + public static final String EMPTY_STRING = ""; + public static final List EMPTY_STRING_LIST = List.of(EMPTY_STRING); + + private TransactionAmountContainsValuePredicate amountPredicate; + private boolean amountPredicateExists; + + private TransactionCategoryContainsKeywordsPredicate categoryPredicate; + private TransactionDateTimeContainsValuePredicate dateTimePredicate; + private TransactionLocationContainsKeywordsPredicate locationPredicate; + private TransactionNameContainsKeywordsPredicate namePredicate; + private boolean namePredicateExists; + + private TransactionTypeContainsValuePredicate typePredicate; + private Predicate composedTransactionPredicate; + + + /** + * Creates a composed predicate object. + */ + public TransactionContainsAllKeywordsPredicate() { + amountPredicate = new TransactionAmountContainsValuePredicate(EMPTY_STRING_LIST); + amountPredicateExists = false; + + categoryPredicate = new TransactionCategoryContainsKeywordsPredicate(EMPTY_STRING_LIST); + dateTimePredicate = new TransactionDateTimeContainsValuePredicate(EMPTY_STRING_LIST); + locationPredicate = new TransactionLocationContainsKeywordsPredicate(EMPTY_STRING_LIST); + + namePredicate = new TransactionNameContainsKeywordsPredicate(EMPTY_STRING_LIST); + namePredicateExists = false; + + typePredicate = new TransactionTypeContainsValuePredicate(EMPTY_STRING_LIST); + composedTransactionPredicate = amountPredicate + .and(categoryPredicate) + .and(dateTimePredicate) + .and(locationPredicate) + .and(namePredicate) + .and(typePredicate); + } + + @Override + public boolean test(Transaction transaction) { + Predicate composedPredicate = composeAllPredicates(); + return composedPredicate.test(transaction); + + } + + /** + * Composes all predicates. + * + * @return a composed predicate + */ + public Predicate composeAllPredicates() { + Predicate composedPredicate = unused -> true; + + if (amountPredicateExists) { + composedPredicate = composedPredicate.and(amountPredicate); + } + + if (namePredicateExists) { + composedPredicate = composedPredicate.and(namePredicate); + } + + return composedPredicate; + } + + public void setAmount(String amount) { + amountPredicate = new TransactionAmountContainsValuePredicate(List.of(amount)); + amountPredicateExists = true; + } + + public void setName(String name) { + namePredicate = new TransactionNameContainsKeywordsPredicate(List.of(name)); + namePredicateExists = true; + } + + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof TransactionContainsAllKeywordsPredicate)) { + return false; + } + + TransactionContainsAllKeywordsPredicate otherContainsKeywordsPredicate = + (TransactionContainsAllKeywordsPredicate) other; + + return amountPredicate.equals(otherContainsKeywordsPredicate.amountPredicate) + && namePredicate.equals(otherContainsKeywordsPredicate.namePredicate) + && composedTransactionPredicate.equals(otherContainsKeywordsPredicate + .composedTransactionPredicate); + + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .add("composedTransactionPredicate", composedTransactionPredicate) + .add("amountPredicate", amountPredicate) + .add("namePredicate", namePredicate) + .toString(); + } +} diff --git a/src/main/java/unicash/model/transaction/predicates/TransactionContainsKeywordsPredicate.java b/src/main/java/unicash/model/transaction/predicates/TransactionContainsAnyKeywordsPredicate.java similarity index 84% rename from src/main/java/unicash/model/transaction/predicates/TransactionContainsKeywordsPredicate.java rename to src/main/java/unicash/model/transaction/predicates/TransactionContainsAnyKeywordsPredicate.java index e86a1a2565d..fc5590897f2 100644 --- a/src/main/java/unicash/model/transaction/predicates/TransactionContainsKeywordsPredicate.java +++ b/src/main/java/unicash/model/transaction/predicates/TransactionContainsAnyKeywordsPredicate.java @@ -16,11 +16,11 @@ * simulates a composed predicate that represents a short-circuiting logical OR of all property * predicates. */ -public class TransactionContainsKeywordsPredicate +public class TransactionContainsAnyKeywordsPredicate implements Predicate { private final List keywords; - public TransactionContainsKeywordsPredicate(List keywords) { + public TransactionContainsAnyKeywordsPredicate(List keywords) { this.keywords = keywords; } @@ -46,12 +46,12 @@ public boolean equals(Object other) { } // instanceof handles nulls - if (!(other instanceof TransactionContainsKeywordsPredicate)) { + if (!(other instanceof TransactionContainsAnyKeywordsPredicate)) { return false; } - TransactionContainsKeywordsPredicate otherNameContainsKeywordsPredicate = - (TransactionContainsKeywordsPredicate) other; + TransactionContainsAnyKeywordsPredicate otherNameContainsKeywordsPredicate = + (TransactionContainsAnyKeywordsPredicate) other; return keywords.equals(otherNameContainsKeywordsPredicate.keywords); } diff --git a/src/main/java/unicash/model/transaction/predicates/TransactionNameContainsKeywordsPredicate.java b/src/main/java/unicash/model/transaction/predicates/TransactionNameContainsKeywordsPredicate.java index d92f895ea4b..f8b4a84cce2 100644 --- a/src/main/java/unicash/model/transaction/predicates/TransactionNameContainsKeywordsPredicate.java +++ b/src/main/java/unicash/model/transaction/predicates/TransactionNameContainsKeywordsPredicate.java @@ -12,6 +12,7 @@ */ public class TransactionNameContainsKeywordsPredicate implements Predicate { + private final List keywords; public TransactionNameContainsKeywordsPredicate(List keywords) { diff --git a/src/test/java/unicash/logic/commands/CommandTestUtil.java b/src/test/java/unicash/logic/commands/CommandTestUtil.java index 7b7a06915fb..a0824855382 100644 --- a/src/test/java/unicash/logic/commands/CommandTestUtil.java +++ b/src/test/java/unicash/logic/commands/CommandTestUtil.java @@ -19,7 +19,7 @@ import unicash.model.Model; import unicash.model.UniCash; import unicash.model.transaction.Transaction; -import unicash.model.transaction.predicates.TransactionContainsKeywordsPredicate; +import unicash.model.transaction.predicates.TransactionContainsAnyKeywordsPredicate; import unicash.testutil.EditTransactionDescriptorBuilder; /** @@ -147,7 +147,7 @@ public static void showTransactionAtIndex(Model model, Index targetIndex) { Transaction transaction = model.getFilteredTransactionList().get(targetIndex.getZeroBased()); final String[] splitName = transaction.getName().fullName.split("\\s+"); model.updateFilteredTransactionList( - new TransactionContainsKeywordsPredicate(Collections.singletonList(splitName[0]))); + new TransactionContainsAnyKeywordsPredicate(Collections.singletonList(splitName[0]))); assertEquals(1, model.getFilteredTransactionList().size()); } diff --git a/src/test/java/unicash/logic/commands/FindCommandTest.java b/src/test/java/unicash/logic/commands/FindCommandTest.java index 08d8b49cdbd..b99797d7553 100644 --- a/src/test/java/unicash/logic/commands/FindCommandTest.java +++ b/src/test/java/unicash/logic/commands/FindCommandTest.java @@ -21,7 +21,7 @@ import unicash.model.ModelManager; import unicash.model.UniCash; import unicash.model.UserPrefs; -import unicash.model.transaction.predicates.TransactionContainsKeywordsPredicate; +import unicash.model.transaction.predicates.TransactionContainsAnyKeywordsPredicate; /** * Contains integration tests (interaction with the Model) for {@code FindCommand}. @@ -33,10 +33,10 @@ public class FindCommandTest { @Test public void equals() { - TransactionContainsKeywordsPredicate firstPredicate = - new TransactionContainsKeywordsPredicate(Collections.singletonList("first")); - TransactionContainsKeywordsPredicate secondPredicate = - new TransactionContainsKeywordsPredicate(Collections.singletonList("second")); + TransactionContainsAnyKeywordsPredicate firstPredicate = + new TransactionContainsAnyKeywordsPredicate(Collections.singletonList("first")); + TransactionContainsAnyKeywordsPredicate secondPredicate = + new TransactionContainsAnyKeywordsPredicate(Collections.singletonList("second")); FindCommand findFirstCommand = new FindCommand(firstPredicate); FindCommand findSecondCommand = new FindCommand(secondPredicate); @@ -63,7 +63,7 @@ public void equals() { @Test public void execute_zeroKeywords_noTransactionsFound() { String expectedMessage = String.format(MESSAGE_TRANSACTIONS_LISTED_OVERVIEW, 0); - TransactionContainsKeywordsPredicate predicate = preparePredicate(" "); + TransactionContainsAnyKeywordsPredicate predicate = preparePredicate(" "); FindCommand command = new FindCommand(predicate); expectedModel.updateFilteredTransactionList(predicate); assertCommandSuccess(command, model, expectedMessage, expectedModel); @@ -73,7 +73,7 @@ public void execute_zeroKeywords_noTransactionsFound() { @Test public void execute_oneKeyword_multipleTransactionsFound() { String expectedMessage = String.format(MESSAGE_TRANSACTIONS_LISTED_OVERVIEW, 3); - TransactionContainsKeywordsPredicate predicate = preparePredicate("work"); + TransactionContainsAnyKeywordsPredicate predicate = preparePredicate("work"); FindCommand command = new FindCommand(predicate); Model expectedModel = new ModelManager(getTypicalUniCash(), new UserPrefs()); @@ -89,7 +89,7 @@ public void execute_oneKeyword_multipleTransactionsFound() { @Test public void execute_predicateNull_assertionFailure() { String expectedMessage = String.format(MESSAGE_TRANSACTIONS_LISTED_OVERVIEW, 0); - TransactionContainsKeywordsPredicate predicate = preparePredicate(" "); + TransactionContainsAnyKeywordsPredicate predicate = preparePredicate(" "); FindCommand command = new FindCommand(null); expectedModel.updateFilteredTransactionList(predicate); @@ -103,7 +103,7 @@ public void execute_predicateNotNull_assertion() { @Test public void toStringMethod() { - TransactionContainsKeywordsPredicate predicate = new TransactionContainsKeywordsPredicate( + TransactionContainsAnyKeywordsPredicate predicate = new TransactionContainsAnyKeywordsPredicate( List.of("keyword")); FindCommand findCommand = new FindCommand(predicate); String expected = FindCommand.class.getCanonicalName() + "{predicate=" + predicate + "}"; @@ -113,7 +113,7 @@ public void toStringMethod() { /** * Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}. */ - private TransactionContainsKeywordsPredicate preparePredicate(String userInput) { - return new TransactionContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+"))); + private TransactionContainsAnyKeywordsPredicate preparePredicate(String userInput) { + return new TransactionContainsAnyKeywordsPredicate(Arrays.asList(userInput.split("\\s+"))); } } diff --git a/src/test/java/unicash/logic/parser/FindCommandParserTest.java b/src/test/java/unicash/logic/parser/FindCommandParserTest.java index e52df3581a6..39b24104746 100644 --- a/src/test/java/unicash/logic/parser/FindCommandParserTest.java +++ b/src/test/java/unicash/logic/parser/FindCommandParserTest.java @@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test; import unicash.logic.commands.FindCommand; -import unicash.model.transaction.predicates.TransactionContainsKeywordsPredicate; +import unicash.model.transaction.predicates.TransactionContainsAnyKeywordsPredicate; /** * A class to test the FindCommandParser. @@ -30,7 +30,7 @@ public void parse_emptyArg_throwsParseException() { public void parse_validArgs_returnsFindCommand() { // no leading and trailing whitespaces FindCommand expectedFindCommand = - new FindCommand(new TransactionContainsKeywordsPredicate(Arrays.asList("Shopping", "Work"))); + new FindCommand(new TransactionContainsAnyKeywordsPredicate(Arrays.asList("Shopping", "Work"))); CommandParserTestUtil.assertParseSuccess(parser, "Shopping Work", expectedFindCommand); // multiple whitespaces between keywords diff --git a/src/test/java/unicash/logic/parser/UniCashParserTest.java b/src/test/java/unicash/logic/parser/UniCashParserTest.java index a67cd0a4a44..ce098b19389 100644 --- a/src/test/java/unicash/logic/parser/UniCashParserTest.java +++ b/src/test/java/unicash/logic/parser/UniCashParserTest.java @@ -26,7 +26,7 @@ import unicash.logic.commands.SummaryCommand; import unicash.logic.parser.exceptions.ParseException; import unicash.model.transaction.Transaction; -import unicash.model.transaction.predicates.TransactionContainsKeywordsPredicate; +import unicash.model.transaction.predicates.TransactionContainsAnyKeywordsPredicate; import unicash.testutil.EditTransactionDescriptorBuilder; import unicash.testutil.TransactionBuilder; import unicash.testutil.TransactionUtil; @@ -46,7 +46,7 @@ public void parseCommand_find() throws Exception { List keywords = Arrays.asList("foo", "bar", "baz"); FindCommand command = (FindCommand) parser.parseCommand( FindCommand.COMMAND_WORD + " " + keywords.stream().collect(Collectors.joining(" "))); - assertEquals(new FindCommand(new TransactionContainsKeywordsPredicate(keywords)), command); + assertEquals(new FindCommand(new TransactionContainsAnyKeywordsPredicate(keywords)), command); } @Test diff --git a/src/test/java/unicash/model/ModelManagerTest.java b/src/test/java/unicash/model/ModelManagerTest.java index 84c68a98de7..e80eb47325d 100644 --- a/src/test/java/unicash/model/ModelManagerTest.java +++ b/src/test/java/unicash/model/ModelManagerTest.java @@ -20,7 +20,7 @@ import unicash.commons.core.GuiSettings; import unicash.model.transaction.exceptions.TransactionNotFoundException; -import unicash.model.transaction.predicates.TransactionContainsKeywordsPredicate; +import unicash.model.transaction.predicates.TransactionContainsAnyKeywordsPredicate; import unicash.testutil.UniCashBuilder; public class ModelManagerTest { @@ -157,7 +157,7 @@ public void equals() { // different filteredList -> returns false String[] keywords = new String[] {"internship"}; modelManager.updateFilteredTransactionList( - new TransactionContainsKeywordsPredicate(Arrays.asList(keywords)) + new TransactionContainsAnyKeywordsPredicate(Arrays.asList(keywords)) ); assertFalse(modelManager.equals(new ModelManager(uniCash, userPrefs))); diff --git a/src/test/java/unicash/model/transaction/predicates/TransactionContainsKeywordsPredicateTest.java b/src/test/java/unicash/model/transaction/predicates/TransactionContainsAnyKeywordsPredicateTest.java similarity index 62% rename from src/test/java/unicash/model/transaction/predicates/TransactionContainsKeywordsPredicateTest.java rename to src/test/java/unicash/model/transaction/predicates/TransactionContainsAnyKeywordsPredicateTest.java index ec9cfe88516..781fd359237 100644 --- a/src/test/java/unicash/model/transaction/predicates/TransactionContainsKeywordsPredicateTest.java +++ b/src/test/java/unicash/model/transaction/predicates/TransactionContainsAnyKeywordsPredicateTest.java @@ -13,24 +13,24 @@ import unicash.testutil.TransactionBuilder; -public class TransactionContainsKeywordsPredicateTest { +public class TransactionContainsAnyKeywordsPredicateTest { @Test public void equals() { List firstPredicateKeywordList = Collections.singletonList("first"); List secondPredicateKeywordList = Arrays.asList("first", "second"); - TransactionContainsKeywordsPredicate firstPredicate = - new TransactionContainsKeywordsPredicate(firstPredicateKeywordList); - TransactionContainsKeywordsPredicate secondPredicate = - new TransactionContainsKeywordsPredicate(secondPredicateKeywordList); + TransactionContainsAnyKeywordsPredicate firstPredicate = + new TransactionContainsAnyKeywordsPredicate(firstPredicateKeywordList); + TransactionContainsAnyKeywordsPredicate secondPredicate = + new TransactionContainsAnyKeywordsPredicate(secondPredicateKeywordList); // same object -> returns true assertEquals(firstPredicate, firstPredicate); // same values -> returns true - TransactionContainsKeywordsPredicate firstPredicateCopy = - new TransactionContainsKeywordsPredicate(firstPredicateKeywordList); + TransactionContainsAnyKeywordsPredicate firstPredicateCopy = + new TransactionContainsAnyKeywordsPredicate(firstPredicateKeywordList); assertEquals(firstPredicate, firstPredicateCopy); // different types -> returns false @@ -48,38 +48,38 @@ public void equals() { @Test public void test_transactionContainsKeywords_returnsTrue() { // One keyword - TransactionContainsKeywordsPredicate predicate = - new TransactionContainsKeywordsPredicate(Collections.singletonList("Food")); + TransactionContainsAnyKeywordsPredicate predicate = + new TransactionContainsAnyKeywordsPredicate(Collections.singletonList("Food")); assertTrue(predicate.test(new TransactionBuilder().withName("Food at mcdonalds").build())); // Multiple keywords - predicate = new TransactionContainsKeywordsPredicate(Arrays.asList("Food", "mcdonalds")); + predicate = new TransactionContainsAnyKeywordsPredicate(Arrays.asList("Food", "mcdonalds")); assertTrue(predicate.test(new TransactionBuilder().withName("Food mcdonalds").build())); // Only one matching keyword - predicate = new TransactionContainsKeywordsPredicate(Arrays.asList("Chicken", "Rice")); + predicate = new TransactionContainsAnyKeywordsPredicate(Arrays.asList("Chicken", "Rice")); assertTrue(predicate.test(new TransactionBuilder().withName("Chicken Rice").build())); // Mixed-case keywords - predicate = new TransactionContainsKeywordsPredicate(Arrays.asList("fOod", "McDonalds")); + predicate = new TransactionContainsAnyKeywordsPredicate(Arrays.asList("fOod", "McDonalds")); assertTrue(predicate.test(new TransactionBuilder().withName("Food mcdonalds").build())); } @Test public void test_transactionDoesNotContainKeywords_returnsFalse() { // Zero keywords - TransactionContainsKeywordsPredicate predicate = - new TransactionContainsKeywordsPredicate(Collections.singletonList("chicken")); + TransactionContainsAnyKeywordsPredicate predicate = + new TransactionContainsAnyKeywordsPredicate(Collections.singletonList("chicken")); assertFalse(predicate.test(new TransactionBuilder().withName("test").build())); } @Test public void toStringMethod() { List keywords = List.of("keyword1", "keyword2"); - TransactionContainsKeywordsPredicate predicate = - new TransactionContainsKeywordsPredicate(keywords); + TransactionContainsAnyKeywordsPredicate predicate = + new TransactionContainsAnyKeywordsPredicate(keywords); - String expected = TransactionContainsKeywordsPredicate + String expected = TransactionContainsAnyKeywordsPredicate .class.getCanonicalName() + "{keywords=" + keywords + "}"; assertEquals(expected, predicate.toString()); }