home *** CD-ROM | disk | FTP | other *** search
- // BankAccount - a general bank account class
- abstract public class BankAccount
- {
- // interest rate
- private static double m_dCurrentInterestRate;
-
- // balance - the current account's balance
- private double m_dBalance;
-
- // constructor - open account with a balance
- BankAccount(double dInitialBalance)
- {
- m_dBalance = dInitialBalance;
- }
-
- // AccountNo - return the account number
- abstract public int AccountNo();
-
-
- // Rate - inquire or set interest rate
- public static double Rate()
- {
- return m_dCurrentInterestRate;
- }
- public static void Rate(double dNewRate)
- {
- // first a sanity check
- if (dNewRate > 0.0 && dNewRate < 20.0)
- {
- m_dCurrentInterestRate = dNewRate;
- }
- }
-
- // Deposit - add something to the account
- public void Deposit(double dAmount)
- {
- // no negative deposits!
- if (dAmount > 0.0)
- {
- m_dBalance += dAmount;
- }
- }
-
- // Withdrawal - take something out
- public void Withdrawal(double dAmount)
- {
- // negative withdrawals are a sneaky way
- // adding money to the account
- if (dAmount >= 0.0)
- {
- // don't let him withdraw more than he has
- if (dAmount <= m_dBalance)
- {
- m_dBalance -= dAmount;
- }
- }
- }
-
- // Balance - return the current balance rounded off to
- // the nearest cent
- public double Balance()
- {
- int nCents = (int)(m_dBalance * 100 + 0.5);
- return nCents / 100.0;
- }
-
- // Monthly - each month wrack up interest and service
- // fees
- public void Monthly()
- {
- Fee(); // fee first! (reduces interest we pay)
- Interest();
- }
-
- // Fee - wrack up the monthly fee
- public void Fee()
- {
- m_dBalance -= 5.0; // $5.00 per month
- }
-
- // Interest - tack on monthly interest
- public void Interest()
- {
- // 1200 because interest is normally expressed
- // in numbers like 20% meaning .2 and because
- // we are accumulating interest monthly
- m_dBalance += m_dBalance *
- (m_dCurrentInterestRate / 1200.0);
- }
- }
-
- class CheckingAccount extends BankAccount
- {
- private static int m_nNextAccountNo = 800001;
- private int nAccountNo;
-
- // constructor
- CheckingAccount(double dInitialBalance)
- {
- super(dInitialBalance);
-
- nAccountNo = m_nNextAccountNo++;
- }
-
- // AccountNo - return the current account ID
- public int AccountNo()
- {
- return nAccountNo;
- }
-
- // Interest - don't accumulate interest if balance
- // under $500
- public void Interest()
- {
- if (Balance() > 500.0)
- {
- super.Interest();
- }
- }
- }
-
- class SavingsAccount extends BankAccount
- {
- private static int m_nNextAccountNo = 100001;
- private int nAccountNo;
-
- // constructor
- SavingsAccount(double dInitialBalance)
- {
- super(dInitialBalance);
-
- nAccountNo = m_nNextAccountNo++;
- }
-
- // AccountNo - return the current account's id
- public int AccountNo()
- {
- return nAccountNo;
- }
-
- // Fee - don't charge fee if balance over $200
- public void Fee()
- {
- if (Balance() < 200.0)
- {
- super.Fee();
- }
- }
- }
-