Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Header File
- #pragma once
- #ifndef Header_h
- #define Header_h
- #include <iostream>
- #include <string>
- using namespace std;
- class Account
- {
- public:
- Account()
- {
- }
- Account(double bal, double add, double minus, double charge) :accountBal(bal), deposit(add), withdraw(minus), fee(charge)
- {
- if (accountBal < 0.0)
- {
- cout << "Balance cannot be less than 0" << endl;
- accountBal = 0.0;
- }
- }
- //Account Member Variables
- double accountBal;
- double deposit;
- double withdraw;
- double fee;
- void credit();
- virtual bool debit();
- virtual void calculateInterest();
- };
- class SavingsAccount : public Account
- {
- public:
- SavingsAccount()
- {
- }
- SavingsAccount(double bal, double add, double minus, double charge, double rate) : Account(bal, add, minus, charge), interestrate(0.05)
- {
- }
- double interestrate;
- double interest;
- void calculateInterest() override;
- };
- class CheckingAccount : public Account
- {
- public:
- CheckingAccount()
- {
- }
- CheckingAccount(double bal, double add, double minus, double charge) : Account(bal, add, minus, charge)
- {
- }
- bool debit() override;
- };
- #endif
- =======================================================================================================================================
- //Implementation File
- #include <iostream>
- #include <string>
- #include "Header.h"
- using namespace std;
- //Base Class Functions
- void Account::calculateInterest()
- {
- }
- void Account::credit() //Deposit
- {
- accountBal = accountBal + deposit;
- }
- bool Account::debit() //Withdraw
- {
- accountBal = accountBal - withdraw;
- return true;
- }
- //SavingsAccount Functions
- void SavingsAccount::calculateInterest()
- {
- interest = (accountBal * interestrate);
- accountBal = accountBal + interest;
- }
- //Checking Account Functions
- bool CheckingAccount::debit() //Withdraw
- {
- if (withdraw > 0)
- {
- accountBal = accountBal - withdraw - fee;
- return true;
- }
- else
- {
- accountBal = accountBal - withdraw;
- return false;
- }
- }
- =======================================================================================================================================
- #include <iostream>
- #include <string>
- #include <vector>
- #include "Header.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";
- };
- Accountstruct search(struct Node* node, int id)
- {
- while (node != NULL)
- {
- if (node->accStruct.id == id)
- {
- return node->accStruct;
- }
- node = node->next;
- }
- 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement