/* * 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: acctmain.cpp // Written by: Cliff Green, 2002 // Compiler: Metrowerks CodeWarrior Pro 6, g++ 3.0.4 // History: // Modified: Mar. 13, 2002 // By: Cliff Green // Comments: Bank Account main app. //---------------------------------------------------------------------- // This commentheader supports documentation tools such as Doc++ and // Doxygen: http://www.doxygen.org/ //---------------------------------------------------------------------- /// Documentation goes here. /** * Doc here. */ #include "acct.h" #include "money.h" #include #include #include #include #include typedef double MoneyType; // typedef Money MoneyType; typedef Acct AcctType; typedef std::list AcctContainer; // typedef std::vector AcctContainer; void displayAcct (AcctType*); void doDailyInterest(AcctType*); void deleteAcct (AcctType*); void displayAcct (AcctType* acctPtr) { std::cout << *acctPtr << std::endl; } void doDailyInterest(AcctType* acct) { (*acct).calcDailyInterest(); } void deleteAcct (AcctType* acct) { delete acct; } struct TripToRioForCliff { TripToRioForCliff (SavingsAcct& cliffsAcct) : mCliffsAcct(cliffsAcct) { } void operator() (AcctType* acct) { acct->withdraw(MoneyType(0.01)); mCliffsAcct.deposit(MoneyType(0.01)); } SavingsAcct& mCliffsAcct; }; struct DoTransaction { DoTransaction (MoneyType const& amt) : mAmt(amt) { } void operator() (AcctType* acct) { if (mAmt < MoneyType(0.0)) { acct->withdraw(-mAmt); } else { acct->deposit(mAmt); } } MoneyType mAmt; }; struct FindAcct { FindAcct (std::string const& acctId) : mAcctId(acctId) { } bool operator() (AcctType* acct) { return acct->getAcctId() == mAcctId; } std::string mAcctId; }; int main(int argc, char* argv[]) { if (argc != 3) { std::cout << "Two args required: acct file, tran file" << std::endl; return 0; } AcctContainer accts; // fill container, do some test processing on all accounts { std::ifstream ifs(argv[1]); if (!ifs) { std::cout << "Unable to open acct file: " << argv[1] << std::endl; return 0; } while (ifs) { std::string acctType, acctId; MoneyType bal; ifs >> acctType >> acctId >> bal; if (acctType == "Checking") { accts.push_back (new CheckingAcct (acctId, bal)); } if (acctType == "Credit") { double rate; ifs >> rate; accts.push_back (new CreditAcct (acctId, bal, rate)); } if (acctType == "Savings") { double rate; ifs >> rate; accts.push_back (new SavingsAcct (acctId, bal, rate)); } } } // display account objects std::cout << "\nList of accts:" << std::endl; std::for_each (accts.begin(), accts.end(), displayAcct); // calculate daily interest std::for_each (accts.begin(), accts.end(), doDailyInterest); std::cout << "\nDaily interest calculated, results:" << std::endl; std::for_each (accts.begin(), accts.end(), displayAcct); // add 10 to all accounts std::for_each (accts.begin(), accts.end(), DoTransaction(MoneyType(10.0))); std::cout << "\n10 Money units deposited to each acct, results:" << std::endl; std::for_each (accts.begin(), accts.end(), displayAcct); // subtract 20 from all accounts std::for_each (accts.begin(), accts.end(), DoTransaction(MoneyType(-20.0))); std::cout << "\n20 Money units withdrawn from each acct, results:" << std::endl; std::for_each (accts.begin(), accts.end(), displayAcct); // save for my trip to Rio SavingsAcct rioBoundYeah ("Rio1", MoneyType(100.0), 10.5); std::for_each (accts.begin(), accts.end(), TripToRioForCliff(rioBoundYeah)); std::cout << "\n\nRio account processing, acct results:" << std::endl; std::for_each (accts.begin(), accts.end(), displayAcct); std::cout << "Cliffs acct: " << rioBoundYeah << std::endl << std::endl; // Now apply each transaction to the appropriate account { std::ifstream ifs(argv[2]); if (!ifs) { std::cout << "Unable to open trans file: " << argv[2] << std::endl; return 0; } std::string acctId; while (ifs >> acctId) { MoneyType transAmt; ifs >> transAmt; AcctContainer::iterator i (find_if(accts.begin(), accts.end(), FindAcct(acctId))); if (i != accts.end()) { DoTransaction doTrans(transAmt); doTrans(*i); } } // end while } // end local block for processing transactions std::cout << "\nTransactions applied, results:" << std::endl; std::for_each (accts.begin(), accts.end(), displayAcct); std::cout << "Cleanup, deleting each account object\n" << std::endl; std::for_each (accts.begin(), accts.end(), deleteAcct); return 0; }