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

  1. // BankAccount - a general bank account class
  2. abstract public class BankAccount
  3. {
  4.     // interest rate
  5.     private static double m_dCurrentInterestRate;
  6.  
  7.     // balance - the current account's balance
  8.     private double m_dBalance;
  9.  
  10.     // constructor - open account with a balance
  11.     BankAccount(double dInitialBalance)
  12.     {
  13.         m_dBalance = dInitialBalance;
  14.     }
  15.  
  16.     // AccountNo - return the account number
  17.     abstract public int AccountNo();
  18.  
  19.  
  20.     // Rate - inquire or set interest rate
  21.     public static double Rate()
  22.     {
  23.         return m_dCurrentInterestRate;
  24.     }
  25.     public static void Rate(double dNewRate)
  26.     {
  27.         // first a sanity check
  28.         if (dNewRate > 0.0 && dNewRate < 20.0)
  29.         {
  30.             m_dCurrentInterestRate = dNewRate;
  31.         }
  32.     }
  33.  
  34.     // Deposit - add something to the account
  35.     public void Deposit(double dAmount)
  36.     {
  37.         // no negative deposits!
  38.         if (dAmount > 0.0)
  39.         {
  40.             m_dBalance += dAmount;
  41.         }
  42.     }
  43.  
  44.     // Withdrawal - take something out
  45.     public void Withdrawal(double dAmount)
  46.     {
  47.         // negative withdrawals are a sneaky way
  48.         // adding money to the account
  49.         if (dAmount >= 0.0)
  50.         {
  51.             // don't let him withdraw more than he has
  52.             if (dAmount <= m_dBalance)
  53.             {
  54.                 m_dBalance -= dAmount;
  55.             }
  56.         }
  57.     }
  58.  
  59.     // Balance - return the current balance rounded off to
  60.     //           the nearest cent
  61.     public double Balance()
  62.     {
  63.         int nCents = (int)(m_dBalance * 100 + 0.5);
  64.         return nCents / 100.0;
  65.     }
  66.  
  67.     // Monthly - each month wrack up interest and service
  68.     //           fees
  69.     public void Monthly()
  70.     {
  71.         Fee();     // fee first! (reduces interest we pay)
  72.         Interest();
  73.     }
  74.  
  75.     // Fee - wrack up the monthly fee
  76.     public void Fee()
  77.     {
  78.         m_dBalance -= 5.0;   // $5.00 per month
  79.     }
  80.  
  81.     // Interest - tack on monthly interest
  82.     public void Interest()
  83.     {
  84.         // 1200 because interest is normally expressed
  85.         // in numbers like 20% meaning .2 and because
  86.         // we are accumulating interest monthly
  87.         m_dBalance += m_dBalance *
  88.                  (m_dCurrentInterestRate / 1200.0);
  89.     }
  90. }
  91.  
  92. class CheckingAccount extends BankAccount
  93. {
  94.     private static int m_nNextAccountNo = 800001;
  95.     private int nAccountNo;
  96.  
  97.     // constructor
  98.     CheckingAccount(double dInitialBalance)
  99.     {
  100.         super(dInitialBalance);
  101.  
  102.         nAccountNo = m_nNextAccountNo++;
  103.     }
  104.  
  105.     // AccountNo - return the current account ID
  106.     public int AccountNo()
  107.     {
  108.         return nAccountNo;
  109.     }
  110.  
  111.     // Interest - don't accumulate interest if balance
  112.     //            under $500
  113.     public void Interest()
  114.     {
  115.         if (Balance() > 500.0)
  116.         {
  117.             super.Interest();
  118.         }
  119.     }
  120. }
  121.  
  122. class SavingsAccount extends BankAccount
  123. {
  124.     private static int m_nNextAccountNo = 100001;
  125.     private int nAccountNo;
  126.  
  127.     // constructor
  128.     SavingsAccount(double dInitialBalance)
  129.     {
  130.         super(dInitialBalance);
  131.  
  132.         nAccountNo = m_nNextAccountNo++;
  133.     }
  134.  
  135.     // AccountNo - return the current account's id
  136.     public int AccountNo()
  137.     {
  138.         return nAccountNo;
  139.     }
  140.  
  141.     // Fee - don't charge fee if balance over $200
  142.     public void Fee()
  143.     {
  144.         if (Balance() < 200.0)
  145.         {
  146.             super.Fee();
  147.         }
  148.     }
  149. }
  150.