Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. //Header file
  2. #pragma once
  3.  
  4. #ifndef Header_h
  5. #define  Header_h
  6.  
  7. #include <iostream>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. class Account
  13. {
  14. public:
  15.     Account()
  16.     {
  17.     }
  18.     Account(double bal, double add, double minus, double charge) :accountBal(bal), deposit(add), withdraw(minus), fee(charge)
  19.     {
  20.         if (accountBal < 0.0)
  21.         {
  22.             cout << "Balance cannot be less than 0" << endl;
  23.             accountBal = 0.0;
  24.         }
  25.     }
  26.  
  27.     //Account Member Variables
  28.     double accountBal;
  29.     double deposit;
  30.     double withdraw;
  31.     double fee;
  32.  
  33.     void credit();
  34.     virtual bool debit();
  35.     virtual void calculateInterest();
  36.  
  37. };
  38.  
  39. class SavingsAccount : public Account
  40. {
  41. public:
  42.     SavingsAccount(double bal, double add, double minus, double charge, double rate) : Account(bal, add, minus, charge), interestrate(0.05)
  43.     {
  44.     }
  45.     double interestrate;
  46.     double interest;
  47.  
  48.    
  49.     void calculateInterest() override;
  50.    
  51.  
  52.  
  53. };
  54.  
  55. class CheckingAccount : public Account
  56. {
  57. public:
  58.     CheckingAccount(double bal, double add, double minus, double charge) : Account(bal, add, minus, charge)
  59.     {
  60.     }
  61.     bool debit() override;
  62.  
  63. };
  64.  
  65. #endif
  66.  
  67.  
  68.  
  69. //Implementation file
  70. #include <iostream>
  71. #include <string>
  72. #include "Header.h"
  73.  
  74.  
  75. using namespace std;
  76. //Base Class Functions
  77.  
  78. void Account::calculateInterest()
  79. {
  80. }
  81.  
  82. void Account::credit() //Deposit
  83. {
  84.     accountBal = accountBal + deposit;
  85. }
  86.  
  87. bool Account::debit() //Withdraw
  88. {
  89.     accountBal = accountBal - withdraw;
  90.     return true;
  91. }
  92.  
  93.  
  94. //SavingsAccount Functions
  95.  
  96. void SavingsAccount::calculateInterest()
  97. {
  98.     interest = (accountBal * interestrate);
  99.     accountBal = accountBal + interest;
  100. }
  101.  
  102.  
  103. //Checking Account Functions
  104.  
  105. bool CheckingAccount::debit() //Withdraw
  106. {
  107.     if (withdraw > 0)
  108.     {
  109.  
  110.         accountBal = accountBal - withdraw - fee;
  111.         return true;
  112.     }
  113.     else
  114.     {
  115.         accountBal = accountBal - withdraw;
  116.         return false;
  117.     }
  118.    
  119. }
  120.  
  121.  
  122.  
  123.  
  124.  
  125. //Main file
  126. #include <iostream>
  127. #include <string>
  128. #include <vector>
  129. #include "Header.h"
  130.  
  131. using namespace std;
  132.  
  133. int main()
  134. {
  135.  
  136.     int choice;
  137.  
  138.     SavingsAccount y(1000, 0, 0, 5, 0.05); //Manually initializing the data members
  139.     CheckingAccount z(1000, 0, 0, 5);
  140.  
  141.     Account* acc1ptr = &y;
  142.     Account* acc2ptr = &z;
  143.     Account* Accounts[2] = { acc1ptr, acc2ptr };
  144.  
  145.  
  146.     cout << "Savings Account(1) or Checking Account(2)?" << endl;
  147.     cin >> choice;
  148.     if (choice == 1)
  149.     {
  150.  
  151.         cout << "Account Balance = $" << acc1ptr->accountBal << endl;
  152.         cout << "How much do you want to deposit?" << endl;
  153.         cin >> acc1ptr->deposit;
  154.         cout << "Depositing $" << acc1ptr->deposit << endl;
  155.         acc1ptr->credit();
  156.         cout << "New Account Balance = $" << acc1ptr->accountBal << endl;
  157.         cout << "How much do you want to withdraw?" << endl;
  158.         cin >> acc1ptr->withdraw;
  159.         cout << "Withdrawing $" << acc1ptr->withdraw << endl;
  160.         acc1ptr->debit();
  161.         cout << "New Account Balance = $" << acc1ptr->accountBal << endl;
  162.        
  163.         //SavingAccount Outputs
  164.         cout << "Interest Rate = " << y.interestrate << endl;
  165.         acc1ptr->calculateInterest();
  166.         cout << "Interest = $" << y.interest << endl;
  167.         cout << "New Account Balance = $" << acc1ptr->accountBal << endl;
  168.     }
  169.     else
  170.     {
  171.         cout << "*Note that the transaction fee for Checking Accounts is $" << acc2ptr->fee << endl;
  172.         cout << "Account Balance = $" << acc2ptr->accountBal << endl;
  173.         cout << "How much do you want to deposit?" << endl;
  174.         cin >> acc2ptr->deposit;
  175.         cout << "Depositing $" << acc2ptr->deposit << endl;
  176.         acc2ptr->credit();
  177.         cout << "New Account Balance = $" << acc2ptr->accountBal << endl;
  178.         cout << "How much do you want to withdraw?" << endl;
  179.         cin >> acc2ptr->withdraw;
  180.         cout << "Withdrawing $" << acc2ptr->withdraw << " + $" << acc2ptr->fee << " fee" << endl;
  181.         acc2ptr->debit();
  182.         cout << "New Account Balance = $" << acc2ptr->accountBal << endl;
  183.    
  184.     }
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement