Advertisement
edensheiko

C++ bank code using linked list version 1

Feb 20th, 2021
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. // node.h
  2. #pragma once
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  
  9. class Node
  10. {
  11. private:
  12.     double _account;
  13.     int _id;
  14.     Node *next;
  15.     string _name;
  16.     static int idGenrator;
  17.  
  18. public:
  19.     friend void print(Node&n);
  20.     Node();
  21.     ~Node();
  22.     Node(string name,double account=0.0);
  23.     static int getidGenrator();
  24.     string Getname()const ;
  25.     double Getaccount()const;
  26.     void Setaccount(double);
  27.     void Setnext( Node &n); // const can be used because we wont change the vlaues
  28.     Node *Getnext() const;
  29. };
  30.  
  31. //Queue.h
  32. #pragma once
  33. #include "Node.h"
  34. class Queue
  35. {
  36. private:
  37.     Node *start;
  38.     Node *end;
  39. public:
  40.     Queue();
  41.     ~Queue();
  42.     void Addnode(Node &n);
  43.     Node & Getnode();
  44. };
  45.  
  46. // node.cpp
  47. #include "Node.h"
  48. #include <iostream>
  49. #include <string>
  50. using namespace std;
  51. int Node::idGenrator = 0;
  52.  
  53. Node::Node()
  54. {
  55. }
  56.  
  57.  
  58. Node::~Node()
  59. {
  60. }
  61.  
  62. Node::Node(string name, double account)
  63. {
  64.     this->_name = name;
  65.     this->_account = account;
  66.     this->_id = getidGenrator();
  67.     this->next = NULL;
  68. }
  69.  
  70.  int Node::getidGenrator()
  71.  {
  72.      ++idGenrator;
  73.      // can be called insted of ++idGenrator
  74.      return idGenrator;
  75.  }
  76.  
  77.  string Node::Getname() const
  78.  {
  79.  
  80.      return this->_name;
  81.  
  82.  }
  83.  
  84.  double Node::Getaccount() const
  85.  {
  86.  
  87.      return this->_id;
  88.  
  89.  }
  90.  
  91.  void Node::Setaccount(double account) //if there is a deposit
  92.  {
  93.      this->_account += account > 0 ? account : 0;
  94.  
  95.  }
  96.  
  97.  void Node::Setnext( Node &n)
  98.  {
  99.      this->next = &n; //get ptr of n
  100.  }
  101.  
  102.  Node* Node::Getnext() const
  103.  {
  104.      return this->next;
  105.  }
  106.  
  107.  
  108.  void print(Node &n)
  109.  {
  110.      cout << "ID:" << n._id << endl;
  111.      cout << "Name:" << n._name << endl;
  112.      cout << "account balance:" << n._account << endl;
  113.  }
  114. // q.h
  115. #include "Queue.h"
  116.  
  117.  
  118.  
  119. Queue::Queue()
  120. {
  121.     start = end = NULL;
  122. }
  123.  
  124.  
  125. Queue::~Queue()
  126. {
  127. }
  128.  
  129. void Queue::Addnode(Node &n)
  130. {
  131.     if (start==NULL)
  132.     {
  133.         start = end = &n;
  134.     }
  135.     else
  136.     {
  137.         end->Setnext(n);
  138.         end = &n;
  139.  
  140.     }
  141.  
  142. }
  143.  
  144. Node & Queue::Getnode()
  145. {
  146.     Node *pn = start;
  147.     if (start==end)
  148.     {
  149.         start = end = NULL;
  150.     }
  151.     else
  152.     {
  153.         start = start->Getnext();
  154.     }
  155.     return *pn;
  156. }
  157. //main.cpp
  158. #include <iostream>
  159. #include "Node.h"
  160. #include "Queue.h"
  161. using namespace std;
  162.  
  163. void main()
  164. {
  165.     //sNode::getidGenrator();
  166.  
  167.     Node v1("eden",2000);
  168.     Node v2("elon mask", 50000);
  169.     Node v3("joe", 40000);
  170.     // Deposit//
  171.     v1.Setaccount(1000);
  172.     cout << "-----------------------------------"<< endl;
  173.     print(v1);
  174.     cout << "-----------------------------------" << endl;
  175.     print(v2);
  176.     cout << "-----------------------------------" << endl;
  177.  
  178.     Queue q1;
  179.     q1.Addnode(v1);
  180.     q1.Addnode(v2);
  181.     q1.Addnode(v3);
  182.  
  183.     Node n10 = q1.Getnode();
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement