//Copyright (c) 2002, Art Gittleman //This example is provided WITHOUT ANY WARRANTY //either expressed or implied. using System; namespace BankAccountAndTest { /* Declares a BankAccount class with an account balance * attribute, two constructors,and GetBalance, Deposit, and * Withdraw operations. */ public class BankAccount { private double balance; // Creates a Bank Account with a balance of 0 public BankAccount() { balance = 0; } // Creates a Bank Account with a balance of initialAmount public BankAccount(double initialAmount) { balance = initialAmount; } // Increases balance by amount public void Deposit(double amount) { balance += amount; } // Reduces balance by amount, if possible public double Withdraw(double amount) { if (balance >= amount) { balance -= amount; return balance; } else return -1.0; } /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! * * Add your overloading method Withdraw here * */ public double Withdraw() { if (balance >= 40.0) { balance -= 40.0; return balance; } else return -1.0; } // Returns the balance public double GetBalance() { return balance; } } /* Creates and tests some BankAccount objects. */ public class TestBankAccount { public static void Main () { BankAccount myAccount = new BankAccount(25.00); Console.WriteLine ("My balance = {0:C}", myAccount.GetBalance()); myAccount.Deposit(700.00); Console.WriteLine("My balance = {0:C}", myAccount.GetBalance()); if(myAccount.Withdraw(300.00) < 0) Console.WriteLine("Insufficient funds"); Console.WriteLine ("My balance = {0:C}", myAccount.GetBalance()); if(myAccount.Withdraw(450.00) < 0) Console.WriteLine("Insufficient funds"); Console.WriteLine("My balance = {0:C}", myAccount.GetBalance()); BankAccount yourAccount = new BankAccount(); yourAccount.Deposit(1234.56); Console.WriteLine("Your balance = {0:C}", yourAccount.GetBalance()); /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! * * Add your code here to test the overloading method Withdraw in class BankAccount * */ Console.WriteLine("\nTesting overloaded Withdraw method..."); if( yourAccount.Withdraw() >=0 ) Console.WriteLine("Your balance = {0:C}", yourAccount.GetBalance()); else Console.WriteLine("Insufficient funds"); } } }