Chapter 7: Object-Oriented Design 115
Counting Transactions
File Account.java (see A Flexible Account Class exercise) contains a definition for a simple bank account class with
methods to withdraw, deposit, get the balance and account number, and return a String representation. Note that the
constructor for this class creates a random account number. Save this class to your directory and study it to see how it works.
Now modify it 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:
1. Add four private static variables to the Account class, one to keep track of each value above (number and total amount of
3. Modify the withdraw and deposit methods to update the appropriate static variables at each withdrawal and deposit
4. 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. Test your program.
Imagine that this loop contains the transactions for a single day. Embed it in a loop that allows the transactions to be
recorded and counted for many days. 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?
//************************************************************
import java.util.Scanner;
public class ProcessTransactions
{
public static void main(String[] args){
Account acct1, acct2; //two test accounts
String keepGoing = “y”; //more transactions?
acct1.printSummary();
System.out.println();
acct2.printSummary();