home *** CD-ROM | disk | FTP | other *** search
- // BankAccount - implement a bank account like
- // one would see at your friendly neighborhood
- // financial institution
- //
- class BankAccount
- {
- // let's start with the data members
- // interest rate
- double m_dCurrentInterestRate;
-
- // balance - the current account's balance
- double m_dBalance;
-
- // now the member functions
- // CurrentRate/SetRate - inquire or set interest rate
- public double CurrentRate()
- {
- return m_dCurrentInterestRate;
- }
- public void SetRate(double dNewRate)
- {
- // first a sanity check
- if (dNewRate > 0.0 && dNewRate < 20.0)
- {
- m_dCurrentInterestRate = dNewRate;
- }
- }
-
- // Deposit - add something to the account
- void Deposit(double dAmount)
- {
- // no negative deposits!
- if (dAmount > 0.0)
- {
- m_dBalance += dAmount;
- }
- }
-
- // Withdrawal - take something out
- 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
- double Balance()
- {
- int nCents = (int)(m_dBalance * 100 + 0.5);
- return nCents / 100.0;
- }
-
- // Monthly - each month wrack up interest and service
- // fees
- void Monthly()
- {
- Fee(); // fee first! (reduces interest we pay)
- Interest();
- }
-
- // Fee - wrack up the monthly fee
- void Fee()
- {
- m_dBalance -= 5.0; // $5.00 per month
- }
-
- // Interest - tack on monthly interest
- 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);
- }
- }
-