Counting Transactions, in Perl
(Practicing Object Oriented Programming)
This assignment is a modification of one I give to my CS1 students. I want you to solve the assignment, but use Perl to do the programming, instead of the Java code I have provided. In other words, reimplement Account.java as Account.pm and ProcessTransactions.java as ProcessTransaction.pl. Use the techniques outlined in my lecture notes.
File Account.java contains a
definition for a simple bank account class with methods to
withdraw, deposit, get the balance and account number, and
print a summary. Save it to your directory and study it to see how it works.
Suppose the bank wanted to keep track of the total number of
deposits and withdrawals (separately) for each day, and
the total amount deposited and withdrawn. Write code to do this
as follows:
- Add a toString method to the class so that Account objects can be easily printed.
(Your Perl function should return a string that can be printed via a print statement.)
- Add four class variables to the Account class, one to keep track
of each value above (number and total amount of deposits, number and total
of withdrawals). I.e., these variables are shared by all Account
objects.
As we discussed in class, you should use the closure technique to implement class variables that provide for encapsulation.
- Create accessor methods for each of the instance variables. (Don't use the fancy AUTOLOAD technique we studied. Implement them by hand.)
- Modify the withdraw and deposit methods to update the appropriate
class variables at each withdrawal and deposit.
- File ProcessTransactions.java contains
a program that creates and initializes two Account objects and enters a loop
that allows the user to enter transactions for either account until asking
to quit. Modify this program as follows:
- After the loop, print the total number of deposits and withdrawals and
the total amount of each. You will need to use the Account methods that
you wrote above, in step 2.
- Create a new subroutine in ProcessTransactions, handleOneDay, that takes two arguments,
the accounts. The body of this method should be all the code in the current
main method between the comments //Finish handling the account
transaction and //Handle the account transactions. Cut and
paste this code into the new subroutine. You may also need to move some of the variable declarations in main to be in handleOneDay. Now, replace the code that was in main with a single call to handleOneDay. Do that, then run your program
to make sure everything still works as it did before you began this surgery.
- Now, change main so that the user can enter data for many days,
instead of just one. Embed the call to handleOneDay in a loop that
allows the transactions to be recorded and counted for many days. The
user should be prompted, "do you want to enter transactions for another
day?" at the bottom of this loop. At the beginning of each day print
the summary for each account, then have the user enter the transactions
for the day. When all of the transactions have been entered, print the
total numbers and amounts (as above), then reset these values to 0 and
repeat for the next day. Note that you will need to add methods to reset
the variables holding the numbers and amounts of withdrawals and deposits
to the Account class. Think: should these be static or instance methods?