C ++中的Visa交易模拟程序

时间:2015-04-30 14:59:33

标签: c++ oop transactions linked-list

我正在做一个数据结构项目,编写我自己的模板链表类实现很重要,它要求: 每个签证账户都可以使用签证卡进行交易,交易记录在他的账户中,包括到期日和金额。

我的步骤:

  1. 列出系统中的帐户。

  2. 每个帐户都有自己的卡片列表,因为需要签发另一张不同号码但需要同一帐户的签证卡。

  3. 为每个签证制作一份交易清单。

  4. 我的问题:

    1. 该卡进行的交易未保存在卡类中定义的交易列表中。您可以检查函数settrans以查看我是如何实现它的。我该如何解决这个问题?
    2. 2.余额"所有交易的总和"没有更新,我稍后需要它让用户付款。

      注意:主要没有完成,还有另外一个数据,我需要做其他功能尚未完成,所以没有问题,如果你发现多余的参数。 我还没有提前或后期条件。

      包含所有函数和类的头文件:

      #include <iostream>
      #include <string>
      using namespace std;
      template<class T> class list;
      class account;
      class card;
      class transaction;
      
      //Node class
      template<class T> class node
      {
      public:
          node() : data(0), next(NULL){} 
          T getdata() { return data; }
          node * getnext(){ return next; }
          void setnext(node * n) { next = n; }
          node(T v) : data(v), next(NULL) {}
          friend list < T >;
      
      private:
          T data;
          node * next;
      };
      
      template<class T> class list
      {
          node<T> * head;
      public:
          list() : head(NULL){} 
          void add(T data)
          {
              node <T> *temp = new node<T>(data);
              temp->next = head;
              head = temp;
          }
          void printaccounts();
          void printcards();
          void printtransactions();
          bool checkaccount(string n, string p);
          account getaccount(string n, string p);
          bool checkcard(int i, string p);
          card getcard(int i, string p);
      
      
      };
      
      
      //Transaction Class
      class transaction
      {
      public:
          void pushtrans(int ch, int a, int cat, int m, int d, string co);
          void newtrans();
          int getcharge() { return charge; }
          int getage(){ return age; }
          string getcompany(){ return company; }
          int getcategory(){ return category; }
      
      private:
          int charge;
          string company;
          int month;
          int day;
          int category;
          int age;
      
      };
      
      void transaction::pushtrans(int ch, int a, int cat, int m, int d, string co)
      {
          charge = ch;
          age = a;
          category = cat;
          month = m;
          day = d;
          company = co;
      
      
      }
      
      //Card Class
      class card
      {
      public:
          card();
          void pushcard(string p, int m, int d, int x);
          int getid(){ return id; }
          int getm(){ return month; }
          int getd(){ return day; }
          int getb(){ return balance; }
          void settrans();
          string getpassword(){ return password;}
          transaction gettransobj(){ return t; }
          list<transaction> getlist(){ return tl; }
      
      
      private:
          int id =0;
          string password;
          int balance =0;
          int month;
          int day;
          list<transaction> tl;
          transaction t;
      
      };
      
      
      card::card()
      {
          password = "";
          balance = 0;
          month = 0;
          day = 0;
      }
      
      void card::pushcard(string p, int m, int d, int x)
      {
      
          password = p;
          month = m;
          day = d;
          id = x;
      
      }
      
      //Account Class
      class account
      {
      public:
          void pushaccount(string n, string p);
          string getname(){ return name; }
          string getaccpassword() { return passwrod; }
          card getcard(){ return c; }
          list<card> getclist() { return cl; }
          void setcard();
      
      private:
          string name;
          string passwrod;
          card c;
          list<card> cl;
      
      };
      
      void account::pushaccount(string n, string p)
      {
          name = n;
          passwrod = p;
      }
      
      void account::setcard()
      {
          string p; int m, d,x;
          cout << "\n _Please enter your Password: ";
          cin >> p;
          cout << "\n _Please enter the Month: ";
          cin >> m;
          cout << "\n _Please enter your Date: ";
          cin >> d;
          x = c.getid() + 1;
          cout << "Your ID is : " << x;
          c.pushcard(p, m, d, x);
          cl.add(c);
      }
      
      template<class T>
      void list<T>:: printaccounts()
      {
          cout << "The Accounts: \n";
          node <T> *temp = head;
          while (temp != nullptr)
          {
              cout << temp->data.getname() << endl;
              temp = temp->next;
          }
      }
      
      template<class T>
      bool list<T>::checkaccount(string n, string p)
      {
          bool c = false;
          node <T> * temp = head;
          while (temp != nullptr)
          {
              if (temp->data.getname() == n && temp->data.getaccpassword() == p)
              {
                  c = true;
                  return c;
              }
              temp = temp->next;
          }
          return c;
      }
      
      template<class T>
      account list<T>::getaccount(string n, string p)
      {
          node <T> * temp = head;
          account a;
          while (temp != nullptr)
          {
              if (temp->data.getname() == n && temp->data.getaccpassword() == p)
              {
                  a = temp->getdata();
                  return a;
              }
              temp = temp->next;
          }
          return a;
      
      }
      
      template<class T>
      void list<T>::printcards()
      {
          cout << "\nThe Cards: \n";
          node <T> *temp = head;
          while (temp != nullptr)
          {
              cout << temp->data.getid() << endl;
              temp = temp->next;
          }
      
      }
      
      template<class T>
      bool list<T>::checkcard(int i, string p)
      
      {
          bool c = false;
          node <T> * temp = head;
          while (temp != nullptr)
          {
              if (temp->data.getid() == i && temp->data.getpassword() == p)
              {
                  c = true;
                  return c;
              }
              temp = temp->next;
          }
          return c;
      
      }
      
      template<class T>
      card list<T>::getcard(int i, string p)
      {
          node <T> * temp = head;
          card c;
          while (temp != nullptr)
          {
              if (temp->data.getid() == i && temp->data.getpassword() == p)
              {
                  c = temp->getdata();
                  return c;
              }
              temp = temp->next;
          }
          return c;
      }
      
      void card::settrans()
      {
          string s; int m, d,a,c,p;
          cout << "\n _Please enter charge: ";
          cin >> p;
          cout << "\n _Please enter the Month: ";
          cin >> m;
          cout << "\n _Please enter your Date: ";
          cin >> d;
          cout << "\n _Choose the Category: \n";
          cout << "|--------------|" << endl;
          cout << "| 1.Food       |" << endl;
          cout << "| 2.Clothes    |" << endl;
          cout << "| 3.Medicine   |" << endl;
          cout << "|--------------|" << endl;
          cin >> c;
          cout << "\n _Please enter the Company/Shop: ";
          cin >> s;
          cout << "\n _Please enter your age: ";
          cin >> a;
          balance += p;
          cout << "Balance = " << balance;
          t.pushtrans(p, a, c, m, d, s);
          tl.add(t);
      
      }
      
      template<class T>
      void list<T>::printtransactions()
      {
          int i = 1;
          cout << "\nThe Transactions: \n";
          node <T> *temp = head;
          while (temp != nullptr)
          {
              cout << i << "." << temp->data.getcompany() << "\tCHARGE: " << temp->data.getcharge() << endl;
              temp = temp->next;
              i++;
          }
      
      }
      

      主要:

      #include <iostream>
      #include <string>
      #include "Header1.h"
      using namespace std;
      
      void main()
      {
          list<account> A;
          account acc;
          //int x;
          while (true)
          {
              int choice, choice2, i , m, d;
              string n, p;
      
              cout << "*** Welcome to ASU Bank ***" << endl;
              cout << "|-------------------------|" << endl;
              cout << "| 1. Get a New Account    |" << endl;
              cout << "| 2. Open My Account      |" << endl;
              cout << "| 3. Statistics           |" << endl;
              cout << "|-------------------------|" << endl;
      
              cin >> choice;
              switch (choice)
              {
              case 1:
                  cout << "Please enter your Name: ";
                  cin >> n;
                  cout << "\n Please enter your Password: ";
                  cin >> p;
                  acc.pushaccount(n, p);
                  A.add(acc);
                  A.printaccounts();
                  break;
      
              case 2:
                  cout << "-Please enter your Name: ";
                  cin >> n;
                  cout << "\n -Please enter your Password: ";
                  cin >> p;
      
                  if (A.checkaccount(n, p))
                  {
                      acc = A.getaccount(n, p);
                      list<card> C;
                      list<transaction> T;
                      card c;
                      transaction t;
      
                      while (true)
                      {
      
                          cout << "\n|-------------------------|" << endl;
                          cout << "| 1. Get a New Card       |" << endl;
                          cout << "| 2. Make Transaction     |" << endl;
                          cout << "| 3. Print Transactions   |" << endl;
                          cout << "| 4. Pay                  |" << endl;
                          cout << "| 5. Companies Record     |" << endl;
                          cout << "| 6. Get Balance          |" << endl;
                          cout << "| 7. Main Menu            |" << endl;
                          cout << "|-------------------------|" << endl;
                          cin >> choice2;
                          switch (choice2)
                          {
                          case 1:
                              //ba2a ye3ml cards with different ids and push them in the list YEAY :D
                              acc.setcard();
                              C = acc.getclist();
                              C.printcards();
                              break;
      
                          case 2:
                              cout << "-Please enter your card ID: ";
                              cin >> i;
                              cout << "\n -Please enter your card Password: ";
                              cin >> p;
                              if (C.checkcard(i, p))
                              {
                                  //bey3ml add fy list gedida w mesh bey7seb balance..same error
                                  c = C.getcard(i, p);
                                  c.settrans();
                                  c.getlist().printtransactions();
      
                              }
                              else cout << "Invalid Entery !\n";
                              break;
      
                          case 3:
                              cout << "-Please enter your card ID: ";
                              cin >> i;
                              cout << "\n -Please enter your card Password: ";
                              cin >> p;
                              if (acc.getclist().checkcard(i, p))
                              {
                                  c = acc.getclist().getcard(i, p);
                                  c.getlist().printtransactions();
      
      
                              }
                              else cout << "Invalid Entery !\n";
                              break;
      
                          case 4:
      
      
      
      
      
                          default:
                              break;
                          }
                      }
      
                  }
                  else
                      cout << "The Account doesn't exist please check your info.\n";
      
                  break;
              default:
                  break;
              }
      
          }
      }
      

      编辑部分: 1.将节点类中的所有数据更改为指向数据的指针

      2

      card * list<T>::getcard(int i, string p)
      {
          node <T> * temp = head;
          card* c;
          while (temp != nullptr)
          {
              if (temp->data.getid() == i && temp->data.getpassword() == p)
              {
                  c = temp;
                  return &c;
              }
              temp = temp->next;
          }
          return &c;
      }
      

      解决部分是有一个事务列表,其中保存了不同的事务,但帐户列表和卡列表有问题: 如果我将约翰添加到帐户然后乔 它将有两个名为joe的帐户。 和卡片列表相同的问题

0 个答案:

没有答案