File Accountjava see 41 A Flexible Account Class exercise co
File Account.java (see 4.1. A Flexible Account Class exercise) contains a definition for a simple bank account class withmethods to withdraw, deposit, get the balance and account number, and return a String representation. Note that theconstructor 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 amountdeposited and withdrawn. Write code to do this as follows:
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;
elseSystem.out.println(\"Insufficient funds\");
}
//-------------------------------------------------// Adds deposit amount to balance.//-------------------------------------------------
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;
}
}
//************************************************************// TestAccount.java//// A simple driver to test the overloaded methods of// the Account class.//************************************************************
import java.util.Scanner;
public class TestAccount
{
public static void main(String[] args)
{
String name;
double balance;
long acctNum;
Account acct;
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter account holder\'s first name\");
name = scan.next();
acct = new Account(name);
System.out.println(\"Account for \" + name + \":\");
System.out.println(acct);
System.out.println(\"\ Enter initial balance\");
balance = scan.nextDouble();
acct = new Account(balance,name);
System.out.println(\"Account for \" + name + \":\");
System.out.println(acct);
System.out.println(\"\ Enter account number\");
acctNum = scan.nextLong();
acct = new Account(balance,name,acctNum);
System.out.println(\"Account for \" + name + \":\");
System.out.println(acct);
System.out.print(\"\ Depositing 100 into account, balance is now \");
acct.deposit(100);
System.out.println(acct.getBalance());
System.out.print(\"\ Withdrawing $25, balance is now \");
acct.withdraw(25);
System.out.println(acct.getBalance());
System.out.print(\"\ Withdrawing $25 with $2 fee, balance is now \");
acct.withdraw(25,2);
System.out.println(acct.getBalance());
System.out.println(\"\ Bye!\");
}
}
1. Add four private static variables to the Account class, one to keep track of each value above (number and total amount ofdeposits, number and total of withdrawals). Note that since these variables are static, all of the Account objects sharethem. This is in contrast to the instance variables that hold the balance, name, and account number; each Account has itsown 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 deposit4. The Provided 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. This loop contains the transactionsfor a single day. Embedded loop allows the transactions to be recorded and counted for many days. At the beginning ofeach day print the summary for each account, then have the user enter the transactions for the day. When all of thetransactions have been entered, print the total numbers and amounts (as above), then reset these values to 0 and repeatfor the next day. Run this test. No need to change this class.
//*******************************************************// ProcessTransactions.java//// A class to process deposits and withdrawals for two bank// accounts for multiple days.//*******************************************************
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 anotherDay = \"y\"; //go for another day?
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);
while (anotherDay.equalsIgnoreCase(\"y\"))
{
System.out.println(\"The following accounts are available:\ \");
acct1.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: \");
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.equalsIgnoreCase(\"w\"))
acct1.withdraw(amount);
else if (action.equalsIgnoreCase(\"d\")) acct1.deposit(amount);else System.out.println(\"Sorry, invalid action.\");
else if (acctNumber == acct2.getAcctNumber())
if (action.equalsIgnoreCase(\"w\"))
acct2.withdraw(amount);
else if (action.equalsIgnoreCase(\"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/n)\");
keepGoing = scan.next();
}
//Print number of deposits
System.out.println(\"Total number of deposits: \" +
Account.getNumDeposits());
//Print number of withdrawals
System.out.println(\"Total number of withdrawals: \" +
Account.getNumWithdrawals());
//Print total amount of deposits
System.out.println(\"Total amount of deposits: \" +
Account.getAmtDeposits());
//Print total amount of withdrawalsSystem.out.println(\"Total amount of withdrawals: \" +
Account.getAmtWithdrawals());
System.out.println(\"Would you like to enter transactions for another
day? (y/n)\");
anotherDay = scan.next();
acct1.resetCounters();acct2.resetCounters();
keepGoing = \"y\";
}
}
}
Solution
Hi Friend, You have not mentioned about methos \'resetCounters\', so i have implemented all required functionality except \'resetCounters\' methos.
please let me know in case of any issue.
######## Account.java #########
public class Account
{
private double balance;
private String name;
private long acctNum;
private static int numDeposits = 0;
private static int numWithdrawals = 0;
private static int amtDeposits = 0;
private static int amtWithdrawals = 0;
//-------------------------------------------------//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;
numWithdrawals++;
}
else
System.out.println(\"Insufficient funds\");
}
//-------------------------------------------------// Adds deposit amount to balance.//-------------------------------------------------
public void deposit(double amount){
balance += amount;
numDeposits++;
}
//-------------------------------------------------// Returns balance.//-------------------------------------------------
public double getBalance()
{
return balance;
}
public double getAmtDeposits(){
return balance;
}
public long getAcctNumber(){
return acctNum;
}
public static int getNumDeposits(){
return numDeposits;
}
public static int getNumWithdrawals(){
return numWithdrawals;
}
public static double getTotalAmtDeposits(){
return amtDeposits;
}
public static double getTotalAmtWithdrawals(){
return amtWithdrawals;
}
public void printSummary(){
System.out.println(\"Name:\" + name +\"\ Account Number: \" + acctNum +\"\ Balance: \" + balance);
}
//-------------------------------------------------// Returns a string containing the name, account number, and balance.//-------------------------------------------------
public String toString()
{
return \"Name:\" + name +
\"\ Account Number: \" + acctNum +
\"\ Balance: \" + balance;
}
}
######## Processing Account ###########
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 anotherDay = \"y\"; //go for another day?
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);
while (anotherDay.equalsIgnoreCase(\"y\"))
{
System.out.println(\"The following accounts are available:\ \");
acct1.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: \");
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.equalsIgnoreCase(\"w\"))
acct1.withdraw(amount);
else if (action.equalsIgnoreCase(\"d\")) acct1.deposit(amount);else System.out.println(\"Sorry, invalid action.\");
else if (acctNumber == acct2.getAcctNumber())
if (action.equalsIgnoreCase(\"w\"))
acct2.withdraw(amount);
else if (action.equalsIgnoreCase(\"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/n)\");
keepGoing = scan.next();
}
//Print number of deposits
System.out.println(\"Total number of deposits: \" +
Account.getNumDeposits());
//Print number of withdrawals
System.out.println(\"Total number of withdrawals: \" +
Account.getNumWithdrawals());
//Print total amount of deposits
System.out.println(\"Total amount of deposits: \" +
Account.getTotalAmtDeposits());
//Print total amount of withdrawals
System.out.println(\"Total amount of withdrawals: \" +
Account.getTotalAmtWithdrawals());
System.out.println(\"Would you like to enter transactions for another day? (y/n)\");
anotherDay = scan.next();
//acct1.resetCounters();
//acct2.resetCounters();
keepGoing = \"y\";
}
}
}








