Advertisement
zappy620

Untitled

Dec 13th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 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 // Base Class
  13. {
  14. public:
  15. Account()
  16. {
  17. }
  18.  
  19. //Parameter Values from main //Initializing the member values of the class
  20. Account(string customer) :name(customer)
  21. {
  22. accountBal = 0.0;
  23.  
  24. }
  25.  
  26. //Account Member Variables
  27. string name; //Get from main (customer)
  28. double accountBal;
  29. double deposit; //Get from main (customer)
  30. double withdraw; //Get from main (customer)
  31.  
  32. void credit(); //Credit Member Function
  33. virtual bool debit(); //Debit Member Function
  34. virtual void calculateInterest();
  35.  
  36. };
  37.  
  38. class SavingsAccount : public Account //Derived Class
  39. {
  40. public:
  41. SavingsAccount(double accountBal, double add, double minus, double charge, double rate) : Account(add, minus), interestrate(0.05)
  42. {
  43. }
  44.  
  45. //Saving Account Member Variables
  46. double interestrate;
  47. double interest;
  48.  
  49.  
  50. void calculateInterest() override;
  51.  
  52.  
  53.  
  54. };
  55.  
  56. class CheckingAccount : public Account //Derived Class
  57. {
  58. public:
  59. double fee = 2.0;
  60.  
  61. CheckingAccount(double bal, double add, double minus, double charge) : Account(bal, add, minus, charge)
  62. {
  63. }
  64. bool debit() override;
  65.  
  66. };
  67.  
  68.  
  69. #endif
  70.  
  71. =======================================================================================================================================
  72. //Implemenation File
  73. #include <iostream>
  74. #include <string>
  75. #include "Header.h"
  76.  
  77.  
  78. using namespace std;
  79. //Base Class Functions
  80.  
  81. void Account::calculateInterest()
  82. {
  83. }
  84.  
  85. void Account::credit() //Deposit
  86. {
  87. accountBal = accountBal + deposit;
  88. }
  89.  
  90. bool Account::debit() //Withdraw
  91. {
  92. accountBal = accountBal - withdraw;
  93. return true;
  94. }
  95.  
  96.  
  97. //SavingsAccount Functions
  98.  
  99. void SavingsAccount::calculateInterest()
  100. {
  101. interest = (accountBal * interestrate);
  102. accountBal = accountBal + interest;
  103. }
  104.  
  105.  
  106. //Checking Account Functions
  107.  
  108. bool CheckingAccount::debit() //Withdraw
  109. {
  110. if (withdraw > 0)
  111. {
  112.  
  113. accountBal = accountBal - withdraw - fee;
  114. return true;
  115. }
  116. else
  117. {
  118. accountBal = accountBal - withdraw;
  119. return false;
  120. }
  121.  
  122. }
  123. =======================================================================================================================================
  124. #include <iostream>
  125. #include <string>
  126. #include <vector>
  127. #include "Header.h"
  128.  
  129. using namespace std;
  130. struct Accountstruct
  131. {
  132. //added ID===================
  133. int id;
  134. string name;
  135. SavingsAccount savings;
  136. CheckingAccount checking;
  137. };
  138. struct Node
  139. {
  140. Accountstruct accStruct;
  141. struct Node* next;
  142. };
  143. //================================================================
  144. //functions for linklist nodes
  145. //================================================================
  146. void push(struct Node** head, Accountstruct node_data)
  147. {
  148. /* 1. create and allocate node */
  149. struct Node* newNode = new Node;
  150.  
  151. /* 2. assign data to node */
  152. newNode->accStruct = node_data;
  153.  
  154. /* 3. set next of new node as head */
  155. newNode->next = (*head);
  156.  
  157. /* 4. move the head to point to the new node */
  158. (*head) = newNode;
  159. };
  160. void displayList(struct Node* node)
  161. {
  162. //traverse the list to display each node
  163. while (node != NULL)
  164. {
  165. cout << node->accStruct.name << " --> ";
  166. node = node->next;
  167. }
  168.  
  169. if (node == NULL)
  170. cout << "null";
  171. };
  172. Accountstruct search(struct Node* node, int id)
  173. {
  174. while (node != NULL)
  175. {
  176. if (node->accStruct.id == id)
  177. {
  178. return node->accStruct;
  179. }
  180. node = node->next;
  181. }
  182. return { -1, "", SavingsAccount(), CheckingAccount() };
  183. }
  184. /* insert new node at the end of the linked list */
  185. void append(struct Node** head, Accountstruct node_data)
  186. {
  187. /* 1. create and allocate node */
  188. struct Node* newNode = new Node;
  189.  
  190. struct Node* last = *head; /* used in step 5*/
  191.  
  192. /* 2. assign data to the node */
  193. newNode->accStruct = node_data;
  194.  
  195. /* 3. set next pointer of new node to null as its the last node*/
  196. newNode->next = NULL;
  197.  
  198. /* 4. if list is empty, new node becomes first node */
  199. if (*head == NULL)
  200. {
  201. *head = newNode;
  202. return;
  203. }
  204.  
  205. /* 5. Else traverse till the last node */
  206. while (last->next != NULL)
  207. last = last->next;
  208.  
  209. /* 6. Change the next of last node */
  210. last->next = newNode;
  211. return;
  212. }
  213. //=================================================================
  214. //end of functions
  215. //=================================================================
  216. int main()
  217. {
  218.  
  219. int choice = 1;
  220.  
  221. //Manually initializing the data members
  222. struct Node* head = NULL;
  223.  
  224. //the account the customer will have access to
  225. Account* acc;
  226.  
  227. //initilization of the Accountstruct accStruct to test
  228. Accountstruct accStruct = { 1, "yes", SavingsAccount(1000, 0, 0, 5, 0.05), CheckingAccount(5000, 0, 0, 5) };
  229.  
  230. //linked lists does First In, Last Out
  231. //so whatever is pushed in first is going to be last one to be listed
  232. //pushed accStruct to head of linkedlist to populate it
  233. push(&head, accStruct);
  234. //pushed "user input" to head of linkedlist to populate it
  235. push(&head, { 2, "No", SavingsAccount(3000, 0, 0, 5, 0.05), CheckingAccount(6000, 0, 0, 5) });
  236.  
  237.  
  238. //the user selects to check savings, checking or if they're new
  239. /*
  240. cout << "Savings Account(1) or Checking Account(2)?" << endl;
  241. cin >> choice;
  242. */
  243. //dynamically allocates memory for the account the customer is about to read
  244. acc = new Account;
  245. //if either checking or savings, pointer acc reads either or
  246. //grabs the head of the linked-list and goes to the next node
  247. //since it's First In, Last out; the last person is "Yes"
  248. accStruct = search(head, 1);
  249. //accStruct.id Will be a -1 if there's an error
  250. if (accStruct.id > 0)
  251. {
  252. *acc = accStruct.checking;
  253. }
  254. else
  255. {
  256. *acc = head->accStruct.checking;
  257. }
  258.  
  259.  
  260. //debug to display the current account it's pointing to in memory
  261. cout << acc->accountBal << endl;
  262. //deletes temporary acc information after customer is done reading
  263. delete acc;
  264. //debug to display all items in list
  265. displayList(head);
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement