
//  File:  account.cpp

#include <iostream>
using namespace std;
#include "account.h"

// ---------------------------------------------------------------
// ------------   Saving Account Member Functions ----------------  
// ---------------------------------------------------------------

//  The constructor for savings accounts.  The first thing it does,
//  using the ": Account(bal)" statement in the header, is call the
//  constructor for the base class.  This must be done in this fashion
//  so that the base class constructor is called first.  If it is not
//  referred to explicitly, it will be done implicitly.

SavingsAccount::SavingsAccount( double bal,
				double pct ) 
  : Account( bal ), rate(pct/100.0)
{
}

double
SavingsAccount::compound( )
{
  double interest = balance * rate;
  balance += interest;
  return interest;
}
 
double
SavingsAccount::withdraw( double amt )
{
  if ( amt > balance ) {  // attempted overdraft ==> return 0
    return 0.0;
  }
  else {
    balance -= amt;
    return amt;
  }
}


// ---------------------------------------------------------------
// ------------   Checking Account Member Functions ----------------  
// ---------------------------------------------------------------

CheckingAccount::CheckingAccount( double bal,   
				  double lim, 
				  double chg ) : Account(bal)
{
  limit = lim;
  charge = chg;
}


double
CheckingAccount::cash_chk( double amt )
{
  if ( balance < limit && (amt + charge <= balance) ) {
    balance -= amt + charge;
    return amt + charge;
  }
  else if ( balance >= limit && amt <= balance ) {
    balance -= amt;
    return amt;
  }
  else {
    return 0.0;
  }
}


// ---------------------------------------------------------------
// --------------   Time Account Member Functions   --------------  
// ---------------------------------------------------------------

//  Most of the work of the constructor is done by the call to the
//  base class constructor.

TimeAccount::TimeAccount( double bal,
			  double pct ) : SavingsAccount( bal, pct )
{
  funds_avail = 0.0;
}


//  The redefined compounding function.  It actually calls the
//  sav_acct compounding function (note the use of the scope
//  resolution operator :: ) and then adds the interest to the funds
//  available and returns the interest.

double
TimeAccount::compound( )
{
  double interest = SavingsAccount::compound( );
  funds_avail += interest;
  return interest;
}


double
TimeAccount::withdraw( double amt )
{
  if ( amt <= funds_avail ) {
    funds_avail -= amt;
    balance -= amt;
    return amt;
  }
  else {
    return 0.0;
  }
}



