Source of BankAccountModel.java


  1: //BankAccountModel.java
  2: //Adapted from an example by Tony Sintes.
  3: //The BankAccountModel manages the account's state and behavior.
  4: //It also updates any object interested in receiving state change 
  5: //notifications, each time the state (i.e., the balance) changes.

  7: import java.util.Observable;
  8: import java.util.Observer;

 10: public class BankAccountModel extends Observable
 11: {
 12:     private double balance;
 13:     
 14:     public BankAccountModel(double initDeposit)
 15:     {
 16:         setBalance(initDeposit);
 17:     }

 19:     protected void setBalance(double newBalance)
 20:     {
 21:         balance = newBalance;
 22:         setChanged();
 23:         notifyObservers();
 24:     }
 25:     
 26:     public double getBalance()
 27:     {
 28:         return balance;
 29:     }

 31:     public void depositFunds(double amount)
 32:     {
 33:         setBalance(getBalance() + amount);
 34:     }

 36:     public void withdrawFunds(double amount)
 37:     {
 38:         if (amount > getBalance()) amount = getBalance();
 39:         setBalance(getBalance() - amount);
 40:     }

 42:     public void addObserver(Observer o)
 43:     {
 44:         super.addObserver(o);
 45:         setChanged();
 46:         notifyObservers();
 47:     }
 48: }