In this assignment you will practice
- Using Optional
- Using Predicate
- Using Lists
- Using Lambdas, Anonymous classes, and Inner classes
- Problem Description
In this homework, you will create many useful classes for our end goal of making an ATM database!
- Transaction
- Keeps track of withdrawals/deposits from/into an account.
- Account
- Keeps track of a list of Transactions for a customer.
You will be provided a TransactionType enum.
Solution Description
Classes:
- Transaction
-
- fields:
- TransactionType type;
- double amount;
- Optional<String> comment; (See link above for Optional documentation)
- methods:
- Getter methods for all fields.
- boolean hasComment() should return true if comment is not empty, false otherwise.
- constructors:
- one that takes in type and amount in that order and assigns them. Sets the comment to Optional.empty().
- one that takes in type and amount in that order and assigns them. Also take in a third parameter that is a String that represents that value of the comment. Properly initialize the comment field with this String.
- fields:
- Account
-
- fields:
- List<Transaction> pastTransactions
- methods:
- Transaction getTransaction(int n): returns the nth transaction from pastTransactions. n will always be valid.
- List<Transaction> findTransactionsByPredicate(Predicate<Transaction> predicate): Returns a list of Transactions from pastTransactions filtered by the predicate. If the predicate returns true when a Transaction is passed in, keep it. Otherwise filter it out of the returned list. Must not modify pastTransactions field. (See link above for documentation on Predicate)
- List<Transaction> getTransactionsByAmount(double amount): returns a list of Transactions with an amount field that equals the parameter amount. Must not modify pastTransactions field. Must call findTransactionsByPredicate with an instance of an inner class.
- List<Transaction> getWithdrawals(): returns a list of Transactions that are withdrawals. Must not modify pastTransactions field. Must call findTransactionsByPredicate with an instance created with an anonymous inner class.
- List<Transaction> getDeposits(): returns a list of Transactions that are deposits. Must not modify pastTransactions field. Must call findTransactionsByPredicate with an instance created with a lambda expression.
- List<Transaction> getTransactionsWithCommentLongerThan(int length): returns a list of Transactions with comments that are longer than length. Must not modify pastTransactions field. Must call findTransactionsByPredicate but you can use whatever technique to create the Predicate that you like.
- List<Transaction> getTransactionsWithComment(): returns a list of Transactions with a comment that is not empty. Must not modify pastTransactions field. Must call findTransactionsByPredicate with a lambda expression or method reference. Method references aren’t taught but they are very useful so feel free to look up how to use them!
- Getter for pastTransactions.
- constructors
- Make a constructor that takes in a List of Transactions assign that list to pastTransactions.
- fields:
If there are any classes that you are unfamiliar with (e.g. Predicate or Optional), look them up in Javadocs. Make the visibility of all the methods and fields as you see best fits good programming style.
Allowed imports
If there is something else you’d like to use, ask on Piazza.
- Optional
- Predicate
- List
- Any subclass of List
Running and Testing
You should create your own testing class to verify that your code works (Don’t submit it though).
Make sure that all your code can be compiled and ran from the command line.
Reviews
There are no reviews yet.