Created
March 5, 2011 07:01
-
-
Save anonymous/856194 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef ACCOUNT_H | |
| #define ACCOUNT_H | |
| #include <iostream> | |
| using namespace std; | |
| class Account | |
| { | |
| public: | |
| Account(double startBalance = 0.0) | |
| { | |
| if(startBalance < 0.0) | |
| { | |
| cout << "Starting balance is invalid." << endl; | |
| } | |
| if (startBalance > 0.0) | |
| { | |
| balance = startBalance; | |
| } | |
| else | |
| { | |
| balance = 0.0; | |
| } | |
| } | |
| virtual double getBalance() | |
| { | |
| return balance; | |
| } | |
| virtual void debit(double i = 0.0) | |
| { | |
| if(i > balance) | |
| { | |
| cout << "Debit amount exceeded account balance." << endl; | |
| } | |
| else | |
| { | |
| balance -= i; | |
| } | |
| } | |
| virtual void credit(double j = 0.0) | |
| { | |
| balance += j; | |
| } | |
| protected: | |
| double balance; | |
| }; | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| #include "Account.h" | |
| class CheckingAccount : public Account | |
| { | |
| public: | |
| CheckingAccount(double startBalance = 0.0, double startFee = 0.0) | |
| { | |
| if(startBalance < 0.0) | |
| { | |
| cout << "Starting balance is invalid." << endl; | |
| } | |
| if (startBalance > 0.0) | |
| { | |
| balance = startBalance; | |
| } | |
| else | |
| { | |
| balance = 0.0; | |
| } | |
| if (startFee > 0.0) | |
| { | |
| interestFee = startFee; | |
| } | |
| else | |
| { | |
| startFee = 0.0; | |
| } | |
| } | |
| void debit(double i = 0.0) | |
| { | |
| if(i > balance) | |
| { | |
| cout << "Debit amount exceeded balance." << endl; | |
| } | |
| else | |
| { | |
| balance -= i + interestFee; | |
| cout << "Transaction fee charged: " << interestFee << "." << endl; | |
| } | |
| } | |
| void credit(double j = 0.0) | |
| { | |
| balance += j - interestFee; | |
| cout << "Transaction fee charged: " << interestFee << "." << endl; | |
| } | |
| private: | |
| double interestFee; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using std::cout; | |
| using std::cin; | |
| using std::endl; | |
| #include <iomanip> | |
| using std::setprecision; | |
| using std::fixed; | |
| #include <vector> | |
| using std::vector; | |
| #include "Account.h" // Account class definition | |
| #include "SavingsAccount.h" // SavingsAccount class definition | |
| #include "CheckingAccount.h" // CheckingAccount class definition | |
| int main() | |
| { | |
| // create vector accounts | |
| vector < Account * > accounts( 4 ); | |
| // initialize vector with Accounts | |
| accounts[ 0 ] = new SavingsAccount( 25.0, .03 ); | |
| accounts[ 1 ] = new CheckingAccount( 80.0, 1.0 ); | |
| accounts[ 2 ] = new SavingsAccount( 200.0, .015 ); | |
| accounts[ 3 ] = new CheckingAccount( 400.0, .5 ); | |
| cout << fixed << setprecision( 2 ); | |
| // loop through vector, prompting user for debit and credit amounts | |
| for ( size_t i = 0; i < accounts.size(); i++ ) | |
| { | |
| cout << "Account " << i + 1 << " balance: $" | |
| << accounts[ i ]->getBalance(); | |
| double withdrawalAmount = 0.0; | |
| cout << "\nEnter an amount to withdraw from Account " << i + 1 | |
| << ": "; | |
| cin >> withdrawalAmount; | |
| accounts[ i ]->debit( withdrawalAmount ); // attempt to debit | |
| double depositAmount = 0.0; | |
| cout << "Enter an amount to deposit into Account " << i + 1 | |
| << ": "; | |
| cin >> depositAmount; | |
| accounts[ i ]->credit( depositAmount ); // credit amount to Account | |
| // downcast pointer | |
| SavingsAccount *savingsAccountPtr = | |
| dynamic_cast < SavingsAccount * > ( accounts[ i ] ); | |
| // if Account is a SavingsAccount, calculate and add interest | |
| if ( savingsAccountPtr != 0 ) | |
| { | |
| double interestEarned = savingsAccountPtr->calculateInterest(); | |
| cout << "Adding $" << interestEarned << " interest to Account " | |
| << i + 1 << " (a SavingsAccount)" << endl; | |
| savingsAccountPtr->credit( interestEarned ); | |
| } // end if | |
| cout << "Updated Account " << i + 1 << " balance: $" | |
| << accounts[ i ]->getBalance() << "\n\n"; | |
| } // end for | |
| system("pause"); | |
| return 0; | |
| } // end main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| #include "Account.h" | |
| class SavingsAccount : public Account | |
| { | |
| public: | |
| SavingsAccount(double startBalance = 0.0, double startRate = 0.0) | |
| { | |
| if(startBalance < 0.0) | |
| { | |
| cout << "Starting balance is invalid." << endl; | |
| } | |
| if (startBalance > 0.0) | |
| { | |
| balance = startBalance; | |
| } | |
| else | |
| { | |
| balance = 0.0; | |
| } | |
| if (startRate > 0.0) | |
| { | |
| interestRate = startRate; | |
| } | |
| else | |
| { | |
| startRate = 0.0; | |
| } | |
| } | |
| double calculateInterest() | |
| { | |
| double interest = interestRate * balance; | |
| return interest; | |
| } | |
| private: | |
| double interestRate; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment