Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Header file
- #pragma once
- #ifndef Header_h
- #define Header_h
- #include <iostream>
- #include <string>
- using namespace std;
- class Account
- {
- public:
- Account()
- {
- }
- Account(double bal, double add, double minus, double charge) :accountBal(bal), deposit(add), withdraw(minus), fee(charge)
- {
- if (accountBal < 0.0)
- {
- cout << "Balance cannot be less than 0" << endl;
- accountBal = 0.0;
- }
- }
- //Account Member Variables
- double accountBal;
- double deposit;
- double withdraw;
- double fee;
- void credit();
- virtual bool debit();
- virtual void calculateInterest();
- };
- class SavingsAccount : public Account
- {
- public:
- SavingsAccount(double bal, double add, double minus, double charge, double rate) : Account(bal, add, minus, charge), interestrate(0.05)
- {
- }
- double interestrate;
- double interest;
- void calculateInterest() override;
- };
- class CheckingAccount : public Account
- {
- public:
- CheckingAccount(double bal, double add, double minus, double charge) : Account(bal, add, minus, charge)
- {
- }
- bool debit() override;
- };
- #endif
- //Implementation file
- #include <iostream>
- #include <string>
- #include "Header.h"
- using namespace std;
- //Base Class Functions
- void Account::calculateInterest()
- {
- }
- void Account::credit() //Deposit
- {
- accountBal = accountBal + deposit;
- }
- bool Account::debit() //Withdraw
- {
- accountBal = accountBal - withdraw;
- return true;
- }
- //SavingsAccount Functions
- void SavingsAccount::calculateInterest()
- {
- interest = (accountBal * interestrate);
- accountBal = accountBal + interest;
- }
- //Checking Account Functions
- bool CheckingAccount::debit() //Withdraw
- {
- if (withdraw > 0)
- {
- accountBal = accountBal - withdraw - fee;
- return true;
- }
- else
- {
- accountBal = accountBal - withdraw;
- return false;
- }
- }
- //Main file
- #include <iostream>
- #include <string>
- #include <vector>
- #include "Header.h"
- using namespace std;
- int main()
- {
- int choice;
- SavingsAccount y(1000, 0, 0, 5, 0.05); //Manually initializing the data members
- CheckingAccount z(1000, 0, 0, 5);
- Account* acc1ptr = &y;
- Account* acc2ptr = &z;
- Account* Accounts[2] = { acc1ptr, acc2ptr };
- cout << "Savings Account(1) or Checking Account(2)?" << endl;
- cin >> choice;
- if (choice == 1)
- {
- cout << "Account Balance = $" << acc1ptr->accountBal << endl;
- cout << "How much do you want to deposit?" << endl;
- cin >> acc1ptr->deposit;
- cout << "Depositing $" << acc1ptr->deposit << endl;
- acc1ptr->credit();
- cout << "New Account Balance = $" << acc1ptr->accountBal << endl;
- cout << "How much do you want to withdraw?" << endl;
- cin >> acc1ptr->withdraw;
- cout << "Withdrawing $" << acc1ptr->withdraw << endl;
- acc1ptr->debit();
- cout << "New Account Balance = $" << acc1ptr->accountBal << endl;
- //SavingAccount Outputs
- cout << "Interest Rate = " << y.interestrate << endl;
- acc1ptr->calculateInterest();
- cout << "Interest = $" << y.interest << endl;
- cout << "New Account Balance = $" << acc1ptr->accountBal << endl;
- }
- else
- {
- cout << "*Note that the transaction fee for Checking Accounts is $" << acc2ptr->fee << endl;
- cout << "Account Balance = $" << acc2ptr->accountBal << endl;
- cout << "How much do you want to deposit?" << endl;
- cin >> acc2ptr->deposit;
- cout << "Depositing $" << acc2ptr->deposit << endl;
- acc2ptr->credit();
- cout << "New Account Balance = $" << acc2ptr->accountBal << endl;
- cout << "How much do you want to withdraw?" << endl;
- cin >> acc2ptr->withdraw;
- cout << "Withdrawing $" << acc2ptr->withdraw << " + $" << acc2ptr->fee << " fee" << endl;
- acc2ptr->debit();
- cout << "New Account Balance = $" << acc2ptr->accountBal << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement