Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Main File
- #include <iostream>
- #include <string>
- #include <vector>
- #include "Account.h"
- using namespace std;
- struct Accountstruct
- {
- //added ID===================
- int id;
- string name;
- SavingsAccount savings;
- CheckingAccount checking;
- };
- struct Node
- {
- Accountstruct accStruct;
- struct Node* next;
- };
- //================================================================
- //functions for linklist nodes
- //================================================================
- void push(struct Node** head, Accountstruct node_data)
- {
- /* 1. create and allocate node */
- struct Node* newNode = new Node;
- /* 2. assign data to node */
- newNode->accStruct = node_data;
- /* 3. set next of new node as head */
- newNode->next = (*head);
- /* 4. move the head to point to the new node */
- (*head) = newNode;
- };
- void displayList(struct Node* node)
- {
- //traverse the list to display each node
- while (node != NULL)
- {
- cout << node->accStruct.name << " --> ";
- node = node->next;
- }
- if (node == NULL)
- cout << "null";
- };
- //newly added==========================================================================
- Accountstruct search(struct Node* node, int id)
- {
- //searches based on ID
- while (node != NULL)
- {
- if (node->accStruct.id == id)
- {
- return node->accStruct;
- }
- node = node->next;
- }
- //ID will always be -1 if it's an error==========================================
- return {-1, "", SavingsAccount(), CheckingAccount()};
- }
- /* insert new node at the end of the linked list */
- void append(struct Node** head, Accountstruct node_data)
- {
- /* 1. create and allocate node */
- struct Node* newNode = new Node;
- struct Node* last = *head; /* used in step 5*/
- /* 2. assign data to the node */
- newNode->accStruct = node_data;
- /* 3. set next pointer of new node to null as its the last node*/
- newNode->next = NULL;
- /* 4. if list is empty, new node becomes first node */
- if (*head == NULL)
- {
- *head = newNode;
- return;
- }
- /* 5. Else traverse till the last node */
- while (last->next != NULL)
- last = last->next;
- /* 6. Change the next of last node */
- last->next = newNode;
- return;
- }
- //=================================================================
- //end of functions
- //=================================================================
- int main()
- {
- int choice=1;
- //Manually initializing the data members
- struct Node* head = NULL;
- //the account the customer will have access to
- Account* acc;
- //initilization of the Accountstruct accStruct to test
- Accountstruct accStruct = {1, "yes", SavingsAccount(1000, 0, 0, 5, 0.05), CheckingAccount(5000, 0, 0, 5) };
- //linked lists does First In, Last Out
- //so whatever is pushed in first is going to be last one to be listed
- //pushed accStruct to head of linkedlist to populate it
- push(&head, accStruct);
- //pushed "user input" to head of linkedlist to populate it
- push(&head, {2, "No", SavingsAccount(3000, 0, 0, 5, 0.05), CheckingAccount(6000, 0, 0, 5) });
- //the user selects to check savings, checking or if they're new
- /*
- cout << "Savings Account(1) or Checking Account(2)?" << endl;
- cin >> choice;
- */
- //dynamically allocates memory for the account the customer is about to read
- acc = new Account;
- //if either checking or savings, pointer acc reads either or
- //grabs the head of the linked-list and goes to the next node
- //since it's First In, Last out; the last person is "Yes"
- accStruct = search(head, 1);
- //accStruct.id Will be a -1 if there's an error
- if (accStruct.id > 0)
- {
- *acc = accStruct.checking;
- }
- else
- {
- *acc = head->accStruct.checking;
- }
- //debug to display the current account it's pointing to in memory
- cout << acc->accountBal << endl;
- //deletes temporary acc information after customer is done reading
- delete acc;
- //debug to display all items in list
- displayList(head);
- 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