/* * Copyright (c) 2002 by Cliff Green. All rights reserved. Individual files * may be covered by other copyrights (as noted in the file itself). * * Redistribution and use in source and binary forms are permitted * provided that this entire copyright notice is duplicated in all such * copies. * * This software is provided "as is" and without any expressed or implied * warranties, including, without limitation, the implied warranties of * merchantibility and fitness for any particular purpose. */ //---------------------------------------------------------------------- // Source file: acct.h // Written by: Cliff Green, 2002 // Compiler: Metrowerks CodeWarrior Pro 6, g++ 3.0.4 // History: // Modified: Mar. 13, 2002 // By: Cliff Green // Comments: Account class declarations (all). //---------------------------------------------------------------------- // This commentheader supports documentation tools such as Doc++ and // Doxygen: http://www.doxygen.org/ //---------------------------------------------------------------------- /// Acct class declarations, Acct base, CheckingAcct, CreditAcct, SavingsAcct. /** * Simple inheritance hierarchy with virtual functions. There is an abstract * base class, along with three concrete classes. The Money type is templatized, * and has been tested with both double and a Money value class (results are * identical, although the tests don't do anything with accuracy). */ #ifndef ACCT_H #define ACCT_H #include #include template class Acct { public: Acct (std::string const& acctId, Money balance) : mAcctId(acctId), mBalance(balance) { } virtual ~Acct() { } std::string const& getAcctId () const { return mAcctId; } Money const getBalance () const { return mBalance; } bool deposit (Money depositAmt); virtual bool withdraw (Money withdrawAmt) = 0; bool operator== (Acct const& rhs ) const { return mAcctId == rhs.mAcctId; } virtual void calcDailyInterest() = 0; virtual std::ostream& streamOut (std::ostream& ) const = 0; static Money const calcDailyPercent(Money amt, double intRate) { return Money(amt * intRate * 0.01 * (1.0/365.0)); } protected: void setBalance (Money newBalance) { mBalance = newBalance; } private: std::string const mAcctId; Money mBalance; private: Acct (Acct const&); // disable copy ctor Acct& operator=(Acct const&); // disable copy assignment }; template class CheckingAcct : public Acct { public: CheckingAcct (std::string const& acctId, Money balance) : Acct(acctId, balance) { } virtual ~CheckingAcct() { } virtual bool withdraw (Money withdrawAmt); virtual void calcDailyInterest() { } virtual std::ostream& streamOut (std::ostream& ) const; }; template class SavingsAcct : public Acct { public: SavingsAcct (std::string const& acctId, Money balance, double interestRate) : Acct(acctId, balance), mInterestRate(interestRate) { } virtual ~SavingsAcct() { } virtual void calcDailyInterest(); virtual bool withdraw (Money withdrawAmt); virtual std::ostream& streamOut (std::ostream& ) const; private: double mInterestRate; }; template class CreditAcct : public Acct { public: CreditAcct (std::string const& acctId, Money balance, double interestRate) : Acct(acctId, balance), mInterestRate(interestRate) { } virtual ~CreditAcct() { } virtual void calcDailyInterest(); virtual bool withdraw (Money withdrawAmt); virtual std::ostream& streamOut (std::ostream& ) const; private: double mInterestRate; }; // Move these member function implementations to .cpp when 'export' // is supported in most compilers template std::ostream& operator<< (std::ostream&, Acct const&); template std::ostream& operator<< (std::ostream& os, Acct const& acct) { return acct.streamOut(os); } template bool Acct::deposit(Money depAmt) { if (depAmt < Money(0.0)) { return false; } mBalance += depAmt; // optional: check for large amounts, report to IRS return true; } template bool CheckingAcct::withdraw(Money withdrawAmt) { if (getBalance() - withdrawAmt < Money(0.0)) { return false; } setBalance(getBalance() - withdrawAmt); return true; } template std::ostream& CheckingAcct::streamOut(std::ostream& os) const { return (os << "[Checking] " << getAcctId() << ", $" << getBalance()); } template bool SavingsAcct::withdraw(Money withdrawAmt) { if (getBalance() - withdrawAmt < Money(0.0)) { return false; } setBalance(getBalance() - withdrawAmt); return true; } template void SavingsAcct::calcDailyInterest() { deposit(calcDailyPercent(getBalance(), mInterestRate)); } template std::ostream& SavingsAcct::streamOut(std::ostream& os) const { return (os << "[Savings] " << getAcctId() << ", $" << getBalance() << " [" << mInterestRate << "%]"); } template bool CreditAcct::withdraw(Money withdrawAmt) { setBalance(getBalance() - withdrawAmt); return true; } template void CreditAcct::calcDailyInterest() { if (getBalance() < Money(0.0)) { withdraw(calcDailyPercent(getBalance()*Money(-1.0), mInterestRate)); } } template std::ostream& CreditAcct::streamOut(std::ostream& os) const { return (os << "[Credit] " << getAcctId() << ", $" << getBalance() << " [" << mInterestRate) << "%]"; } #endif // end of header file