Advertisement
Ixninth

account main

Dec 13th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.30 KB | None | 0 0
  1. //Main File
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include "Account.h"
  6.  
  7. using namespace std;
  8. struct Accountstruct
  9. {
  10.     //added ID===================
  11.     int id;
  12.     string name;
  13.     SavingsAccount savings;
  14.     CheckingAccount checking;
  15. };
  16. struct Node
  17. {
  18.     Accountstruct accStruct;
  19.     struct Node* next;
  20. };
  21. //================================================================
  22. //functions for linklist nodes
  23. //================================================================
  24. void push(struct Node** head, Accountstruct node_data)
  25. {
  26.     /* 1. create and allocate node */
  27.     struct Node* newNode = new Node;
  28.  
  29.     /* 2. assign data to node */
  30.     newNode->accStruct = node_data;
  31.  
  32.     /* 3. set next of new node as head */
  33.     newNode->next = (*head);
  34.  
  35.     /* 4. move the head to point to the new node */
  36.     (*head) = newNode;
  37. };
  38. void displayList(struct Node* node)
  39. {
  40.     //traverse the list to display each node
  41.     while (node != NULL)
  42.     {
  43.         cout << node->accStruct.name << " --> ";
  44.         node = node->next;
  45.     }
  46.  
  47.     if (node == NULL)
  48.         cout << "null";
  49. };
  50. //newly added==========================================================================
  51. Accountstruct search(struct Node* node, int id)
  52. {
  53. //searches based on ID
  54.     while (node != NULL)
  55.     {
  56.         if (node->accStruct.id == id)
  57.         {
  58.             return node->accStruct;
  59.         }
  60.         node = node->next;
  61.     }
  62. //ID will always be -1 if it's an error==========================================
  63.     return {-1, "", SavingsAccount(), CheckingAccount()};
  64. }
  65. /* insert new node at the end of the linked list */
  66. void append(struct Node** head, Accountstruct node_data)
  67. {
  68.     /* 1. create and allocate node */
  69.     struct Node* newNode = new Node;
  70.  
  71.     struct Node* last = *head; /* used in step 5*/
  72.  
  73.     /* 2. assign data to the node */
  74.     newNode->accStruct = node_data;
  75.  
  76.     /* 3. set next pointer of new node to null as its the last node*/
  77.     newNode->next = NULL;
  78.  
  79.     /* 4. if list is empty, new node becomes first node */
  80.     if (*head == NULL)
  81.     {
  82.         *head = newNode;
  83.         return;
  84.     }
  85.  
  86.     /* 5. Else traverse till the last node */
  87.     while (last->next != NULL)
  88.         last = last->next;
  89.  
  90.     /* 6. Change the next of last node */
  91.     last->next = newNode;
  92.     return;
  93. }
  94. //=================================================================
  95. //end of functions
  96. //=================================================================
  97. int main()
  98. {
  99.  
  100.     int choice=1;
  101.  
  102.     //Manually initializing the data members
  103.     struct Node* head = NULL;
  104.  
  105.     //the account the customer will have access to
  106.     Account* acc;
  107.  
  108.     //initilization of the Accountstruct accStruct to test
  109.     Accountstruct accStruct = {1, "yes", SavingsAccount(1000, 0, 0, 5, 0.05), CheckingAccount(5000, 0, 0, 5) };
  110.  
  111.     //linked lists does First In, Last Out
  112.     //so whatever is pushed in first is going to be last one to be listed
  113.     //pushed accStruct to head of linkedlist to populate it
  114.     push(&head, accStruct);
  115.     //pushed "user input" to head of linkedlist to populate it
  116.     push(&head, {2, "No", SavingsAccount(3000, 0, 0, 5, 0.05), CheckingAccount(6000, 0, 0, 5) });
  117.    
  118.  
  119.     //the user selects to check savings, checking or if they're new
  120.     /*
  121.     cout << "Savings Account(1) or Checking Account(2)?" << endl;
  122.     cin >> choice;
  123.     */
  124.     //dynamically allocates memory for the account the customer is about to read
  125.     acc = new Account;
  126.     //if either checking or savings, pointer acc reads either or
  127.     //grabs the head of the linked-list and goes to the next node
  128.     //since it's First In, Last out; the last person is "Yes"
  129.     accStruct = search(head, 1);
  130.     //accStruct.id Will be a -1 if there's an error
  131.     if (accStruct.id > 0)
  132.     {
  133.         *acc = accStruct.checking;
  134.     }
  135.     else
  136.     {
  137.         *acc = head->accStruct.checking;
  138.     }
  139.    
  140.  
  141.     //debug to display the current account it's pointing to in memory
  142.     cout << acc->accountBal << endl;
  143.     //deletes temporary acc information after customer is done reading
  144.     delete acc;
  145.     //debug to display all items in list
  146.     displayList(head);
  147.     if (choice == 1)
  148.     {
  149.         /*
  150.         cout << "Account Balance = $" << acc1ptr->accountBal << endl;
  151.         cout << "How much do you want to deposit?" << endl;
  152.         cin >> acc1ptr->deposit;
  153.         cout << "Depositing $" << acc1ptr->deposit << endl;
  154.         acc1ptr->credit();
  155.         cout << "New Account Balance = $" << acc1ptr->accountBal << endl;
  156.         cout << "How much do you want to withdraw?" << endl;
  157.         cin >> acc1ptr->withdraw;
  158.         cout << "Withdrawing $" << acc1ptr->withdraw << endl;
  159.         acc1ptr->debit();
  160.         cout << "New Account Balance = $" << acc1ptr->accountBal << endl;
  161.  
  162.         //SavingAccount Outputs
  163.         cout << "Interest Rate = " << y.interestrate << endl;
  164.         acc1ptr->calculateInterest();
  165.         cout << "Interest = $" << y.interest << endl;
  166.         cout << "New Account Balance = $" << acc1ptr->accountBal << endl;*/
  167.     }
  168.     else
  169.     {
  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