Advertisement
zappy620

Untitled

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