home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / Chap03 / BankAccount / BankAccount.java < prev    next >
Encoding:
Text File  |  1996-06-19  |  2.2 KB  |  86 lines

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