home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / Java++ / VJ / SAMPLES / JAVANOW / CHAP04 / BANKACCOUNT / BANKACCOUNT.JAVA < prev    next >
Encoding:
Java Source  |  1996-06-19  |  2.1 KB  |  81 lines

  1. // BankAccount - a general bank account class
  2. public class BankAccount
  3. {
  4.     // interest rate
  5.     static private double m_dCurrentInterestRate;
  6.  
  7.     // balance - the current account's balance
  8.     private double m_dBalance;
  9.  
  10.     // Rate - inquire or set interest rate
  11.     public static double Rate()
  12.     {
  13.         return m_dCurrentInterestRate;
  14.     }
  15.     public static void Rate(double dNewRate)
  16.     {
  17.         // first a sanity check
  18.         if (dNewRate > 0.0 && dNewRate < 20.0)
  19.         {
  20.             m_dCurrentInterestRate = dNewRate;
  21.         }
  22.     }
  23.  
  24.     // Deposit - add something to the account
  25.     public void Deposit(double dAmount)
  26.     {
  27.         // no negative deposits!
  28.         if (dAmount > 0.0)
  29.         {
  30.             m_dBalance += dAmount;
  31.         }
  32.     }
  33.  
  34.     // Withdrawal - take something out
  35.     public void Withdrawal(double dAmount)
  36.     {
  37.         // negative withdrawals are a sneaky way
  38.         // adding money to the account
  39.         if (dAmount >= 0.0)
  40.         {
  41.             // don't let him withdraw more than he has
  42.             if (dAmount <= m_dBalance)
  43.             {
  44.                 m_dBalance -= dAmount;
  45.             }
  46.         }
  47.     }
  48.  
  49.     // Balance - return the current balance rounded off to
  50.     //           the nearest cent
  51.     public double Balance()
  52.     {
  53.         int nCents = (int)(m_dBalance * 100 + 0.5);
  54.         return nCents / 100.0;
  55.     }
  56.  
  57.     // Monthly - each month wrack up interest and service
  58.     //           fees
  59.     public void Monthly()
  60.     {
  61.         Fee();     // fee first! (reduces interest we pay)
  62.         Interest();
  63.     }
  64.  
  65.     // Fee - wrack up the monthly fee
  66.     public void Fee()
  67.     {
  68.         m_dBalance -= 5.0;   // $5.00 per month
  69.     }
  70.  
  71.     // Interest - tack on monthly interest
  72.     public void Interest()
  73.     {
  74.         // 1200 because interest is normally expressed
  75.         // in numbers like 20% meaning .2 and because
  76.         // we are accumulating interest monthly
  77.         m_dBalance += m_dBalance *
  78.                  (m_dCurrentInterestRate / 1200.0);
  79.     }
  80. }
  81.