银行业务办理

来源:互联网 发布:seo 关键字 编辑:程序博客网 时间:2024/06/02 09:53

代码如下:

#ifndef __BANK_H  #define __BANK_H  #include "User.h"  #include <cstring>  #include <string>  #include <vector>  using namespace std;  const int upNum = 1000;  class Bank  {  public:      Bank();//从文件中读数据到数组中      ~Bank();//写入文件      void work();//业务驱动      void openAccount();//开户      void cancelAccount();//注销账户      void save();//存款      void withdraw();//取款      void showAccount();//查询余额      void transferAccounts();//转账      void reportLoss();//挂失      void cancelLoss();//解挂      void updataPassword();//更改密码      int getUser();//输入账号查询用户  private:      int N;//用户数目      User users[upNum];  };  int pass();  int chooseInMenu();  int inputPassword();  #endif  [html] view plain copy#pragma once  #include <string>  #include <iostream>  using namespace std;  class bank;  class User  {  public:      User()      {}      void setUser(int acc, string nam, int pw, double bal, int sta);      void showName();      void showBalance(string prompt);      bool passwordIsright();      bool isNormalUser();      friend class Bank;  private:      int account;//账号      int password;//密码      string name;//用户名      double balance;//账户余额      int status;//0.正常 1.挂失 2 销户  };  [html] view plain copy#include "User.h"  #include "bank.h"  void User::setUser(int acc, string nam, int pw, double bal, int sta)  {      this->account = acc;      this->name = nam;      this->password = pw;      this->balance = bal;      this->status = sta;  }  void User::showName()  {      cout << "账户姓名:" << name << endl;  }  void User::showBalance(string prompt)  {      cout << prompt << "余额:" << balance << endl;  }  bool User::passwordIsright()  {      int i = 0;      while (password / 10 > 10)          i++;      if (i > 0)          return true;      else      {          return false;      }  }  bool User::isNormalUser()  {      if (status == 0)          return true;      else          return false;  }  [html] view plain copy#include "bank.h"    #include<iostream>  #include<fstream>  #include<cstdlib>  #include<string>  #include<ctype.h>  using namespace std;  Bank::Bank()  {      ifstream infile("account.txt", ios::in);      if (!infile)      {          cerr << "open error" << endl;          exit(1);      }      int i = 0;      int acc;      string nam;      int pw;      double bal;      int sta;      while (infile >> acc >> nam >> pw >> bal >> sta)      {          users[i].setUser(acc, nam, pw, bal, sta);          i++;      }      N = i;      infile.close();  }  Bank::~Bank()  {      ofstream outfile("account.txt", ios::out);      if (!outfile)      {          cerr << "open error" << endl;          exit(1);      }      int i;      for (i = 0; i < N; i++)      {          outfile << users[i].account << " ";          outfile << users[i].name << " ";          outfile << users[i].password << " ";          outfile << users[i].balance << " ";          outfile << users[i].status << " ";      }      outfile.close();  }  void Bank::work()  {      int iChoice;      while (1)      {          iChoice = chooseInMenu();          switch (iChoice)          {          case 1:openAccount();break;          case 2:cancelAccount();break;          case 3:save();break;          case 4:withdraw();break;          case 5:getUser();break;          case 6:transferAccounts();break;          case 7:reportLoss();break;          case 8:cancelLoss();break;          case 9:updataPassword();break;          default:              break;          }      }  }  void Bank::openAccount()  {      if (N >= upNum)      {          cout << "用户已达上限" << endl;          return;      }      int acc;      string nam;      int pw;      double bal;      int sta;      cout << "正在开户" << endl;      acc = 1001 + N;      cout << "账号: " << acc << endl;      cout << "户主姓名:";      cin >> nam;      int iPass1, iPass2;      cout << "密码:";      iPass1 = inputPassword();      cout << "确认密码";      iPass2 = inputPassword();      if (iPass1 != iPass2)      {          cout << "密码输入不一致" << endl;          return;      }      pw = iPass1;      cout << "存入金额";      cin >> bal;      users[N].account = acc;      users[N].password = pw;      users[N].status = 0;      users[N].balance = bal;      cout << "开户成功" << endl;      N++;  }  void Bank::cancelAccount()  {      int i;      char ch;      while ((i = getUser()) == -1)      {          cout << "查无此账号,请重新输入:" << endl;      }      cout << "确认销户(y/n)" << endl;      cin >> ch;      if (ch == 'y')      {          cout << "取款1000元,销户成功" << endl;          users[i].status = 2;          users[i].balance = 0;      }      else {          return;      }  }  int Bank::getUser()  {      int acc;      int i;      int flag = 0;      cout << "请输入账号:";      cin >> acc;      for (i = 0; i < N; i++)      {          if (acc == users[i].account)          {              cout << "姓名:"<<users[i].name << endl;              cout << "账户余额:"<<users[i].balance << endl;              cout << "当前状态:"<<users[i].status << endl;              flag = i;          }      }      if (flag < i)      {          return flag;      }      else          return -1;  }  void Bank::save()  {      int i;      char ch;      int bal;      while ((i = getUser()) == -1)      {          cout << "查无此账号,请重新输入:" << endl;      }      cout << "请输入要存的金额:";      cin >> bal;      users[i].balance += bal;      cout << "存钱成功" << endl;  }  void Bank::withdraw()  {      int i;      char ch;      int bal;      while ((i = getUser()) == -1)      {          cout << "查无此账号,请重新输入:" << endl;      }      cout << "请输入要取的金额:";      cin >> bal;      if (users[i].balance < bal)      {          cout << "余额不足" << endl;          return;      }      else      {          users[i].balance -= bal;      }      cout << "取钱成功" << endl;  }  void Bank::transferAccounts()  {      int i;      char ch;      int bal;      while ((i = getUser()) == -1)      {          cout << "查无此账号,请重新输入:" << endl;      }      cout << "请输入要转的金额:";      cin >> bal;      users[i].balance += bal;      cout << "转账成功" << endl;  }  void Bank::reportLoss()  {      int i;      int pass;      char ch;      int bal;      while ((i = getUser()) == -1)      {          cout << "查无此账号,请重新输入:" << endl;      }      cout << "请输入密码" << endl;      cin >> pass;      if (users[i].password == pass)      {          cout << "挂失成功" << endl;          users[i].status = 1;      }      else {          cout << "密码输入错误" << endl;      }  }  void Bank::cancelLoss()  {      int pass;      char ch;      int bal;      while ((i = getUser()) == -1)      {          cout << "查无此账号,请重新输入:" << endl;      }      cout << "请输入密码" << endl;      cin >> pass;      if (users[i].password == pass)      {          cout << "解挂成功" << endl;          users[i].status = 0;      }      else {          cout << "密码输入错误" << endl;      }  }  void Bank::updataPassword()  {      int i;      char ch;      int bal;      while ((i = getUser()) == -1)      {          cout << "查无此账号,请重新输入:" << endl;      }      int iPass1, iPass2;      cout << "新密码:";      iPass1 = inputPassword();      cout << "确认密码";      iPass2 = inputPassword();      if (iPass1 != iPass2)      {          cout << "密码输入不一致" << endl;          return;      }      else      {          users[i].password = iPass1;          cout << "密码输入成功" << endl;      }  }  [html] view plain copy#include<iostream>      #include<fstream>  #include<conio.h>  #include<cstdlib>  #include<cstring>  #include<ctype.h>  #include"bank.h"  using namespace std;  int pass()//验证用户密码,正确返回1,错误返回0  {      char sNameInfile[20];      char sPassInfile[20];      ifstream infile("password.txt", ios::in);      if (!infile)      {          cout << "password file cannot open!" << endl;          system("pause");          exit(1);      }      infile >> sNameInfile >> sPassInfile;      infile.close();      char sName[20];      char sPass[20];      char ch;      int iTry = 3;      int right = 0;      do      {          cout << "请输入业务员用户名:";          cin >> sName;          cout << "请输入密码:";          int i = 0;          while ((ch = getch()) != '\r')          {              sPass[i++] = ch;              putchar('*');          }          sPass[i] = '\0';          fflush(stdin);          cout << endl;          if (strcmp(sPass, sPassInfile) == 0 && strcmp(sName, sNameInfile) == 0)          {              right = 1;              break;          }          else          {              iTry--;              if (iTry < 0)                  cout << "对不起,您已输入超过三次 " << endl;          }      } while (iTry);      return right;  }  int chooseInMenu()  {      int i;      while (1)      {          cout << endl;          cout << "+------------------------------------+" << endl;          cout << "+       1 开户  2 销户  3 存款        +" << endl;          cout << "+       4 取款  5 查询  6 转账        +" << endl;          cout << "+       7 挂失  8 解挂  9 改密        +" << endl;          cout << "+                      0 退出        +" << endl;          cout << "+------------------------------------+" << endl;          cout << "请输入操作指令:";          cin >> i;          if (i >= 0 && i <= 9)              break;          else              cout << "请重新输入" << endl;      }      return i;  }  int inputPassword()  {      char ch;      int iPass = 0;      int i;      while (1)      {          for (i = 0; i < 6; i++)          {              ch = getch();              putchar('*');              if (isdigit(ch))                  iPass = iPass * 10 + (ch - '0');              else              {                  iPass = 0;                  break;              }          }          fflush(stdin);          cout << endl;          if (iPass == 0)          {              cout << "密码要求全为数字" << endl;              cout << "请重新输入" << endl;          }          else              break;      }      return iPass;  }  [html] view plain copy#include<iostream>  #include"bank.h"  using namespace std;  int main()  {      cout << "+-------------------------------------------+" << endl;      cout << "               欢迎光临csdn银行              " << endl;      cout << "+-------------------------------------------+" << endl;      Bank b;      if (pass())      {          Bank b;          b.work();      }  }  
原创粉丝点击