Lab 14
Readers and Writers for BankAccount Transactions
Introduction
For this lab you will implement some important utility
classes: "readers" that know how to read Transactions and
BankAccounts from files and a "writer" which knows how to write
BankAccounts to a file . This lab reinforces the idea of
substitution, but it also reinforces the idea of decentralizing
responsibilities. Instead of placing file reading and writing behavior in our
client classes, we will create objects that can read and write files
containing specific kinds of objects. This reduces the complexity of client code
while at the same time making the client code less dependent on the
representation of the objects that it uses.
When you are done, you will have a graphical program that mimics a simple ATM.
When the program starts a previously existing set of Transactions (a BankAccount)
will be loaded into the ATM from an input file. The ATM will allow
simple, whole dollar Deposits and Withdrawals to be made. When a provided
button on the ATM is pressed, the set of Transactions (the BankAccount),
including both the original Transactions and the new Transactions, will be written to
an output file.
Why this lab will frustrate you
This lab will drive some of you crazy for two reasons:
- I am going to ask you to use several classes you haven't read about and we
haven't talked about in class (Calendar and FileWriter at a minimum).
- The directions seem kind of cryptic at times. I tell you to build
some class that will likely make no sense to you on first pass because you
can't see the big picture yet.
While you may think I am being unreasonable, I am doing this for a reason.
- You are going to get asked to modify code you haven't seen before that
uses things you have never seen before. Your job is to check out the API
as necessary and figure it out.
- I could let you stumble around trying to find a working solution, but I
want you to design a GOOD solution. The directions have you write what
is, I think, a nicely written solution for this problem domain. When you
are done, go back and look at WHY this IS a good solution.
Getting Started
To begin with you will need to download the following starter code
This is based on a working solution to Lab 12
. Check out the following classes:
- Transaction
- Deposit
- Withdrawal
- ServiceCharge
- BankAccount
- BankAccountDemo
And then run BankAccountDemo to remind yourself how this works.
You don't need to know a lot about Calendar to understand how to use it in
this lab. Check out how I use it, and refer to this code as needed to
solve this lab.
Tasks
The code you have started with is a simple "hard coded" example. Now
your boss wants you to take this code and make it more like an actual banking
application. That is, we need long term storage of data (file I/O) and
something more like an ATM. You remember the "Accumulator" that we used
earlier in the semester and decide to modify that to look something like an ATM
(this part has done for you, mostly).
Complete this object-oriented program by doing the following:
- Implement a TransactionReader class. A TransactionReader
is an object that can read Transactions from a file.
The TransactionReader class should
provide:
- a constructor that takes the name of the source file as a parameter.
This file is opened for reading by the constructor and stored for use by...
- a read() method that returns a Transaction as its
answer. If the end of the file has been reached, the TransactionReader
should signal this by returning a null object.
Your class must be able to read the three Transaction types
provided by the starter code (deposit, withdrawal, and service charge) and contained in the file named
"transactions1.txt"
- Implement a BankAccountReader class. A BankAccountReader
uses a TransactionReader to read a stream of Transactions
into BankAccount. A BankAccountReader should provide at
least these services:
- BankAccountReader( ), which is provided a
BankAccount at construction
- read( String fileName ), in response to which the object reads
Transactions from the file named fileName into its
BankAccount. Notice that it won't actually do any reading itself.
Instead, it delegates the file reading to TransactionReader. BankAccountReader
simply assembles the Transactions returned by calls to TransactionReader into BankAccount
b.
When you think you have parts one and two working, compile and run
BankAccountPartTwoDemo.java and see if your output makes sense.
- Implement a BankAccountWriter class. A BankAccountWriter should provide at
least these services:
- BankAccountWriter( BankAccount b ), a constructor which takes
in a BankAccount b which will be used by the object.
- write( String fileName ), in response to which the
BankAccountWriter:
- gets a copy of all of the Transactions from the BankAccount b stored as
an instance variable
- for each
Transaction in this account builds a String description of the
Transaction similar to the Strings contained in transactions1.txt
Hint : to do this, you will need to figure out how to convert a Calendar into a printable,
comma-separated date. One way to do this is to consider some
Calendar d = <some mechanism for knowing the date of the transaction>;
String month = d.get(GregorianCalendar.MONTH);
...
- Writes this each String out to a file named fileName
in the format you used in steps 1 and 2. In other words, files written
by your BankAccountWriter should be able to be used in BankAccountPartTwoDemo by simply changing the hardcoded filename from transactions1.txt to whatever filename is used by the
write().
Hint : to do this, you will need to consider PrintWriter to open a file
to write and the use of println() to write to that file.
- When you are all done, you can test this by running Lab14 with the provided
file as input, performing a few transactions on the ATM and then pressing the
write button to save this to an output file. THEN, run the code again
using the output file from the first run as your input file in this run.
Is everything read in properly?
Deliverables
Zip all of these together into a single Lab14.zip file and submit:
- TransactionReader.java
- BankAccountReader.java
- BankAccountWriter.java