Question

In: Computer Science

File Account.java (see 4.1. A Flexible Account Class exercise) contains a definition for a simple bank...



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 +

"\nAccount Number: " + acctNum +

"\nBalance: " + 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("\nEnter initial balance");

balance = scan.nextDouble();

acct = new Account(balance,name);

System.out.println("Account for " + name + ":");

System.out.println(acct);

System.out.println("\nEnter account number");

acctNum = scan.nextLong();

acct = new Account(balance,name,acctNum);

System.out.println("Account for " + name + ":");

System.out.println(acct);
System.out.print("\nDepositing 100 into account, balance is now ");

acct.deposit(100);

System.out.println(acct.getBalance());

System.out.print("\nWithdrawing $25, balance is now ");

acct.withdraw(25);

System.out.println(acct.getBalance());

System.out.print("\nWithdrawing $25 with $2 fee, balance is now ");

acct.withdraw(25,2);

System.out.println(acct.getBalance());
System.out.println("\nBye!");

}

}

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:\n");

acct1.printSummary();
System.out.println();acct2.printSummary();
while (keepGoing.equalsIgnoreCase("y"))

    {

//get account number, what to do, and amount

System.out.print("\nEnter 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("\nMore 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";

    }

    }

}

Solutions

Expert 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 +"\nAccount Number: " + acctNum +"\nBalance: " + balance);

   }

   //-------------------------------------------------// Returns a string containing the name, account number, and balance.//-------------------------------------------------

   public String toString()

   {

       return "Name:" + name +

               "\nAccount Number: " + acctNum +

               "\nBalance: " + 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:\n");

           acct1.printSummary();

           System.out.println();acct2.printSummary();

           while (keepGoing.equalsIgnoreCase("y"))

           {

               //get account number, what to do, and amount

               System.out.print("\nEnter 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("\nMore 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";

       }

   }

}


Related Solutions

Write a Java program for a simple bank account. You shall define a Customer class: A...
Write a Java program for a simple bank account. You shall define a Customer class: A customer has a first name, last name, and social security number. The social security number is a String variable and must comply with this format: xxx-xx-xxxx where 'x' is a digit between 0-9. If a customer is supplied with an invalid SSN, a message must be printed stating SSN of the customer is invalid; however, the account still is created. You shall define a...
Below is a definition of the class of a simple link list. class Chain; class ChainNode...
Below is a definition of the class of a simple link list. class Chain; class ChainNode { friend class Chain; private: int data; ChainNode *link ; }; class Chain{ public: ... private: ChainNode *first; // The first node points. } Write a member function that inserts a node with an x value just in front of a node with a val value by taking two parameters, x and val. If no node has a val value, insert the node with...
A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
1.   Design a class, Account, that models the basic workings of a bank account. The class...
1.   Design a class, Account, that models the basic workings of a bank account. The class should perform the following tasks: a.   Save the account balance. b.   Save the number of transactions performed on the account. c.   Allow deposits to be made to the account. d.   Allow withdrawals to be taken from the account. e.   Calculate interest for the period. f.   Report the current account balance at any time. g.   Report the current number of transactions at any time The...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data field named id for the account. ■ A private float data field named balance for the account. ■ A private float data field named annualInterestRate that stores the current interest rate. ■ A constructor that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0). ■ The accessor and mutator methods for id, balance, and...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator functions for id, balance, and annualInterestRate....
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ polygon.h file- #ifndef POLY_RVC_H #define POLY_RVC_H #include <iostream> using namespace std; class Polygon { public:    Polygon();    Polygon(int n, double l);    //accessors - all accessors should be declared "const"    // usually, accessors are also inline functions    int getSides() const { return sides; }    double getLength()...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ Write a polygon.h file with given instructions for the polygon.cpp file #include <iostream> #include <math.h> using namespace std; #ifndef M_PI # define M_PI 3.14159265358979323846 #endif int main() {    float areaP, length, sides;    cout << "\n\n Polygon area program.\n";    cout << "---------------------------------\n";    cout << " Enter the...
Using the class Date that you defined in Exercise O3, write the definition of the class...
Using the class Date that you defined in Exercise O3, write the definition of the class named Employee with the following private instance variables: first name               -           class string last name                -           class string ID number             -           integer (the default ID number is 999999) birth day                -           class Date date hired               -           class Date base pay                 -           double precision (the default base pay is $0.00) The default constructor initializes the first name to “john”, the last name to “Doe”, and...
Object Design Example: Bank Account Object Implement the bank account class that stores account number and...
Object Design Example: Bank Account Object Implement the bank account class that stores account number and the balance. It has methods to deposit and withdraw money. In addition, the default constructor sets account number to 0000 and balance 0. Other constructor should take the account number and the initial balance as parameters. Finally, the account class should have a static data member that stores how many accounts are created for a given application. a) Draw the UML diagram b) Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT