Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 d0 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 deposits, number and total of withdrawals). Note that since these variables are static, all of the Account objects share them. This is in contrast to the instance variables that hold the balance, name, and account number; each Account has its own copy of these. Recall that numeric static and instance variables are initialized to 0 by default.

2. Add public methods to return the values of each of the variables you just added, e.g., public static int getNumDeposits().

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?

//************************************************************ // ProcessTransactions.java // // A class to process deposits and withdrawals for two bank // accounts for a single day. //************************************************************ import java.util.Scanner; public class ProcessTransactions { public static void main(String[] args){ Account acct1, acct2; //two test accounts String keepGoing = "y"; //more transactions? String action; //deposit or withdraw double amount; //how much to deposit or withdraw long acctNumber; //which account to access Scanner scan = new Scanner(System.in); //Create two accounts acct1 = new Account(1000, "Sue", 123); acct2 = new Account(1000, "Joe", 456); System.out.println( "The following accounts are available: " ); acct1.printSummary(); System.out.println(); acct2.printSummary();

while (keepGoing.equals("y") || keepGoing.equals("y")) { //get account number, what to do, and amount System.out.print(" Enter the number of the account you would like to access: "); acctNumber = scan.nextLong(); System.out.print("Would you like to make a deposit (D) or withdrawal (W) ? "); action = scan.next(); System.out.print("Enter the amount: "); amount = scan.nextDouble(); if (amount > 0) if (acctNumber == acct1.getAcctNumber()) if (action.equals("w") || action.equals("W")) acct1.withdraw(amount); else if (action.equals("d") || action.equals("D")) acct1.deposit(amount); else System.out.println("Sorry, invalid action."); else if (acctNumber == acct2.getAcctNumber()) if (action.equals("w") || action.equals("W")) acct1.withdraw(amount); else if (action.equals("d") || action.equals("D")) acct1.deposit(amount); else System.out.println( "Sorry, invalid action. "); else System.out.println( "Sorry, invalid account number. "); else System.out.println("Sorry, amount must be > 0 . "); System.out.print(" More transactions? (y/n)"); keepGoing = scan.next(); } //Print number of deposits //Print number of withdrawals //Print total amount of deposits //Print total amount of withdrawals } }

//************************************************************ // Account.java // // A bank account class with methods to deposit to, withdraw from, // change the name on, and get a String representation // of the account. //************************************************************ public class Account { private double balance; private String name; private long acctNum; //------------------------------------------------- //Constructor -- initializes balance, owner, and account number //------------------------------------------------- public Account(double initBal, String owner, long number) { balance = initBal; name = owner; acctNum = number; } //------------------------------------------------- // Checks to see if balance is sufficient for withdrawal. // If so, decrements balance by amount; if not, prints message. //------------------------------------------------- public void withdraw(double amount) { if (balance >= amount) balance -= amount; else System.out.println("Insufficient funds"); } //------------------------------------------------- // Adds deposit amount to balance. //-------------------------------------------------

110 Chapter 7: Object-Oriented Design public void deposit(double amount) { balance += amount; } //------------------------------------------------- // Returns balance. //------------------------------------------------- public double getBalance() { return balance; } //------------------------------------------------- // Returns a string containing the name, account number, and balance. //------------------------------------------------- public String toString() { return "Name:" + name + " Account Number: " + acctNum + " Balance: " + balance; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions