home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / Chap09 / BankAccount / BankAccount.java < prev    next >
Encoding:
Java Source  |  1996-06-20  |  1.1 KB  |  46 lines

  1. // BankAccount - a general bank account class
  2. public class BankAccount
  3. {
  4.     private static int m_AIds = 0;
  5.  
  6.     // id - this is the account id
  7.     private int m_nAccountId;
  8.  
  9.     // balance - the current account's balance
  10.     private double m_dBalance;
  11.  
  12.     // constructor - open account with a balance
  13.     BankAccount(double dInitialBalance)
  14.     {
  15.         m_dBalance = dInitialBalance;
  16.         m_nAccountId = ++m_AIds;
  17.     }
  18.  
  19.     // Id - return the account id
  20.     public int Id()
  21.     {
  22.         return m_nAccountId;
  23.     }
  24.  
  25.     // Balance
  26.     public double Balance()
  27.     {
  28.       return ((int)(m_dBalance * 100.0 + 0.5)) / 100.0;
  29.     }
  30.  
  31.     // Withdrawal - make a withdrawal
  32.     public void Withdrawal(double dAmount)
  33.                   throws  InsufficientFundsException
  34.     {
  35.         // if there's insufficient funds on hand...
  36.         if (m_dBalance < dAmount)
  37.         {
  38.             // ...throw an exception
  39.             throw new InsufficientFundsException(this, dAmount);
  40.         }
  41.  
  42.         // otherwise, post the debit
  43.         m_dBalance -= dAmount;
  44.     } 
  45. }
  46.