//Copyright (c) 2002 Art Gittleman //This example is provided WITHOUT ANY WARRANTY //either expressed or implied. using System; namespace AcctAndTest { /* Modifies the BankAccount class to include a class variable * to store the total number of successful Deposit, Withdraw, * and GetBalance operations by any Acct object. */ /*!!!!!!!!!! *Modify class Acct as required by Exercise 5.13. */ public class Acct { private double balance; // instance variable private static int transactions = 0; // class variable private static int acctNum = 0; //Add a class variable to count created bank accounts public Acct() { balance = 0; acctNum++; //Increment account number when this constructor is called } public Acct(double initialAmount) { balance = initialAmount; acctNum++; //Increment account number when this constructor is called } public static int GetAcctNum() { return acctNum; } // Increments transactions public void Deposit(double amount) { // instance method balance += amount; transactions++; } /* Only increments transactins if the * withdrawal is successful. */ public double Withdraw(double amount) { if (balance >= amount) { balance -= amount; transactions++; return balance; } else return -1.0; } public double GetBalance() { transactions++; return balance; } public static int GetTransactionCount() { // class method return transactions; } } /* Creates some Acct objects and illustrates the use of * class variables and methods.. */ public class TestAcct { public static void Main () { Acct myAcct = new Acct(25.00); myAcct.Deposit(700.00); if(myAcct.Withdraw(300.00) < 0) Console.WriteLine("Insufficient funds"); if(myAcct.Withdraw(450.00) < 0) Console.WriteLine("Insufficient funds"); Console.WriteLine ("My balance after completing transactions is {0:C}", myAcct.GetBalance()); Console.WriteLine("The number of transactions is {0}", Acct.GetTransactionCount()); Acct yourAcct = new Acct(); yourAcct.Deposit(1234.56); Console.WriteLine ("Your balance after completing transactions is {0:C}", yourAcct.GetBalance()); Console.WriteLine("The number of transactions is {0}", Acct.GetTransactionCount()); /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! * * Add your code here to test modified class Acct * */ Console.WriteLine("\nThe number of created bank accounts is: {0}", Acct.GetAcctNum()); } } }