Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me with this please, I'm so confusing. This will due in the next 7 hours. Thank you! Writing static methods and variables.

Can someone help me with this please, I'm so confusing. This will due in the next 7 hours. Thank you!

Writing static methods and variables. Your task is to modify an existing class and tester. Follow the directions below.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Counting Transactions 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. (This is very much like the class that was extended in a lab exercise in Chapter 4.) 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: 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 Process Transactions.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? O // Account.java // A bank account class with methods to deposit, withdraw, // and check the balance. //**** public class Account { private double balance; private String name; private double acctNum; //Constructor -- initializes balance, owner, and account number public Account (double initBal, String owner, double 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. //--- public void deposit (double amount) { balance += amount; } // Returns balance. //-- public double getBalance() return balance; } //--- // Returns account number //-- public double getAcctNumber() { return acct Num; // Prints account number, name, and balance //---- public void printSummary() System.out.println("Account number: " + acctNum); System.out.println("Account owner: " + name); System.out.println("Account balance: " + balance); ) } import java.util.Scanner; public class ProcessTransactions { public static void main(String[] args) { Account accti, acct2; String keepGoing = "Y"; String acti double amount; double acct Number; //two test accounts //more transactions? sit or withdraw 1/how much to deposit or withdraw // which account to access Scanner scan = new Scanner (System.in); //Create two accounts acctl = new Account (1000, "Sue", 123); acct2 = new Account (1000, "Joe", 456); System.out.println("The following accounts are available: "); acctl.printSummary(); System.out.println(); acct2.printSummary(); while (keepGoing.equalsIgnoreCase ("y")) //get account number, what to do, and amount System.out.print(" Enter the number of the account you would like to access: acct Number = scan.next Double(); 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(); (amount > 0) if (acctNumber == accti.getAcctNumber()) if (action.equals IgnoreCase ("w")) accti.withdraw (amount); else if (action.equals IgnoreCase ("d")) acctl.deposit (amount); else System.out.println("Sorry, invalid action."); else if (acct Number == acct2.getAcctNumber()) if (action.equals IgnoreCase ("w")) acct2.withdraw (amount); else if (action.equals IgnoreCase ("d")) acct2.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)"); keepGoing = scan.next(); } //Print number of deposits //Print number of withdrawals //Print total amount of deposits //Print total amount of withdrawals

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

Online Systems For Physicians And Medical Professionals How To Use And Access Databases

Authors: Harley Bjelland

1st Edition

1878487442, 9781878487445

More Books

Students also viewed these Databases questions