forked from AY2324S1-CS2103-T16-3/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8cc6fe
commit b8858b9
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
src/main/java/unicash/logic/parser/FilterCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
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 unicash.logic.commands.FilterCommand; | ||
import unicash.logic.parser.exceptions.ParseException; | ||
import unicash.model.category.Category; | ||
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; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Parses input arguments and creates a new FilterCommand object | ||
*/ | ||
public class FilterCommandParser implements Parser<FilterCommand> { | ||
|
||
private TransactionContainsAllKeywordsPredicate filterPredicate = | ||
new TransactionContainsAllKeywordsPredicate(); | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the FilterCommand | ||
* and returns a 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() || !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FilterCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_TYPE); | ||
|
||
|
||
/* Parses the names provided and adds them to the all keywords predicate */ | ||
if (argMultimap.getValue(PREFIX_NAME).isPresent()) { | ||
List<String> keywordsList = argMultimap.getAllValues(PREFIX_NAME); | ||
|
||
for (String keyword : keywordsList) { | ||
Name transactionName = ParserUtil.parseTransactionName(keyword); | ||
filterPredicate.addNameKeyword(transactionName.toString()); | ||
} | ||
|
||
} | ||
|
||
if (argMultimap.getValue(PREFIX_AMOUNT).isPresent()) { | ||
Amount transactionAmount = ParserUtil.parseAmount(argMultimap.getValue(PREFIX_AMOUNT).get()); | ||
filterPredicate.addAmountKeyword(Amount.amountToDecimalString(transactionAmount)); | ||
} | ||
|
||
if (argMultimap.getValue(PREFIX_CATEGORY).isPresent()) { | ||
Category transactionCategory = ParserUtil.parseCategory(argMultimap.getValue(PREFIX_CATEGORY).get()); | ||
filterPredicate.addCategoryKeyword(transactionCategory.toString()); | ||
} | ||
|
||
if (argMultimap.getValue(PREFIX_LOCATION).isPresent()) { | ||
Location transactionLocation = ParserUtil.parseLocation( | ||
argMultimap.getValue(PREFIX_LOCATION).get()); | ||
filterPredicate.addLocationKeyword(transactionLocation.toString()); | ||
} | ||
|
||
if (argMultimap.getValue(PREFIX_DATETIME).isPresent()) { | ||
DateTime transactionDateTime = ParserUtil.parseDateTime( | ||
argMultimap.getValue(PREFIX_DATETIME).get()); | ||
filterPredicate.addDateTimeKeyword(transactionDateTime.toString()); | ||
} | ||
|
||
if (argMultimap.getValue(PREFIX_TYPE).isPresent()) { | ||
Type transactionType = ParserUtil.parseType(argMultimap.getValue(PREFIX_TYPE).get()); | ||
filterPredicate.addTypeKeyword(transactionType.toString()); | ||
} | ||
|
||
return new FilterCommand(filterPredicate); | ||
} | ||
|
||
} | ||
|