Skip to content

Commit

Permalink
Add remaining filters for FilterCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
sp4ce-cowboy committed Oct 28, 2023
1 parent 4b4ea49 commit a7a05fe
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
40 changes: 32 additions & 8 deletions src/main/java/unicash/logic/parser/FilterCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@

import unicash.logic.commands.FilterCommand;
import unicash.logic.parser.exceptions.ParseException;
import unicash.model.category.Category;
import unicash.model.category.UniqueCategoryList;
import unicash.model.commons.Amount;
import unicash.model.transaction.DateTime;
import unicash.model.transaction.Location;
import unicash.model.transaction.Name;
import unicash.model.transaction.Type;
import unicash.model.transaction.predicates.TransactionContainsAllKeywordsPredicate;

/**
* Parses input arguments and creates a new FilterCommand object
*/
public class FilterCommandParser implements Parser<FilterCommand> {

public static final String EMPTY_STRING = "";
public static final List<String> EMPTY_STRING_LIST = List.of(EMPTY_STRING);

private static TransactionContainsAllKeywordsPredicate filterPredicate =
private TransactionContainsAllKeywordsPredicate filterPredicate =
new TransactionContainsAllKeywordsPredicate();

/**
Expand All @@ -46,19 +48,41 @@ public FilterCommand parse(String args) throws ParseException {
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FilterCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_TYPE, PREFIX_AMOUNT, PREFIX_DATETIME,
PREFIX_LOCATION);
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_CATEGORY, 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());
filterPredicate.setNameKeyword(transactionName.toString());

}

if (argMultimap.getValue(PREFIX_AMOUNT).isPresent()) {
Amount transactionAmount = ParserUtil.parseAmount(argMultimap.getValue(PREFIX_AMOUNT).get());
filterPredicate.setAmount(Amount.amountToDecimalString(transactionAmount));
filterPredicate.setAmountKeyword(Amount.amountToDecimalString(transactionAmount));
}

if (argMultimap.getValue(PREFIX_CATEGORY).isPresent()) {
Category transactionCategory = ParserUtil.parseCategory(argMultimap.getValue(PREFIX_CATEGORY).get());
filterPredicate.setCategoryKeyword(transactionCategory.toString());
}

if (argMultimap.getValue(PREFIX_LOCATION).isPresent()) {
Location transactionLocation = ParserUtil.parseLocation(
argMultimap.getValue(PREFIX_LOCATION).get());
filterPredicate.setLocationKeyword(transactionLocation.toString());
}

if (argMultimap.getValue(PREFIX_DATETIME).isPresent()) {
DateTime transactionDateTime = ParserUtil.parseDateTime(
argMultimap.getValue(PREFIX_DATETIME).get());
filterPredicate.setDateTimeKeyword(transactionDateTime.toString());
}

if (argMultimap.getValue(PREFIX_TYPE).isPresent()) {
Type transactionType = ParserUtil.parseType(argMultimap.getValue(PREFIX_TYPE).get());
filterPredicate.setTypeKeyword(transactionType.toString());
}

return new FilterCommand(filterPredicate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,56 @@ public Predicate<Transaction> composeAllActivePredicates() {
return composedPredicate;
}

public void setAmount(String amount) {
public void setAmountKeyword(String amount) {
BooleanPredicatePair predicatePair = new BooleanPredicatePair(true,
new TransactionAmountContainsValuePredicate(
Collections.singletonList(amount)));

predicatePairMap.put(TransactionProperty.AMOUNT, predicatePair);
}

public void setName(String name) {
public void setNameKeyword(String name) {
BooleanPredicatePair predicatePair = new BooleanPredicatePair(true,
new TransactionNameContainsKeywordsPredicate(
Collections.singletonList(name)));

predicatePairMap.put(TransactionProperty.NAME, predicatePair);
}

public void setLocationKeyword(String location) {
BooleanPredicatePair predicatePair = new BooleanPredicatePair(true,
new TransactionLocationContainsKeywordsPredicate(
Collections.singletonList(location)));

predicatePairMap.put(TransactionProperty.LOCATION, predicatePair);
}

public void setDateTimeKeyword(String dateTime) {
BooleanPredicatePair predicatePair = new BooleanPredicatePair(true,
new TransactionDateTimeContainsValuePredicate(
Collections.singletonList(dateTime)));

predicatePairMap.put(TransactionProperty.NAME, predicatePair);
}

public void setTypeKeyword(String type) {
BooleanPredicatePair predicatePair = new BooleanPredicatePair(true,
new TransactionTypeContainsValuePredicate(
Collections.singletonList(type)));

predicatePairMap.put(TransactionProperty.TYPE, predicatePair);
}

public void setCategoryKeyword(String category) {
BooleanPredicatePair predicatePair = new BooleanPredicatePair(true,
new TransactionTypeContainsValuePredicate(
Collections.singletonList(category)));

predicatePairMap.put(TransactionProperty.CATEGORY, predicatePair);
}




@Override
public boolean equals(Object other) {
Expand Down

0 comments on commit a7a05fe

Please sign in to comment.