银行系统

来源:互联网 发布:淘宝好的女装店 编辑:程序博客网 时间:2024/04/27 13:54



bank.h


#ifndef BANK_H_INCLUDED
#define BANK_H_INCLUDED
#include <cstring>
using namespace std;
const int upNum=2000; //系统最多容纳的用户数
class Bank;
class User
{
public:
    void setUser(int acc, string nam, int pw, double bal,int sta);
    void showName();
    void showBalance(string prompt); //显示余额,前面加上提示词prompt
    bool passwordIsRight();   //校验密码,输入的密码正确则返回true
    bool isNormalUser(); //存款、取款、转帐等功能,需要账户处于正常姿态,处于正常状态时返回true,其他情形返回false并提示
    friend class Bank;   //将Bank声明为友元类,方便其访问数据成员
private:
    int account; //账号
    int password;  //密码
    string name;   //用户名
    double balance;  //账户余额
    int status;  //状态 0-正常  1-挂失  2-销户
};
 
class Bank
{
public:
    Bank();  //开始前从文件中读数据,存在数组中
    ~Bank();   //程序结束前,将数组中的数据写入到文件中
    void work();  //业务驱动
    void openAccount(); //开户
    void cancelAccount();  //注销账户
    void save();   //存款
    void withdraw();   //取款
    void showAccount(); //查询余额
    void transferAccounts();  //转账
    void reportLoss();  //挂失
    void cancelLoss();  //解除挂失
    void updatePassword();   //更改密码
    int getUser();  //输入账号查询用户,返回用户在对象数组中的下标
private:
    int N;  //实际的用户数目
    User users[upNum];  //User数组,耗空间啊!改进方案:1.对象指针数组;2. 动态数组;3. 链表
};
 
int pass();  //业务员登录
int chooseInMenu(); //显示菜单并由业务员选择
int inputPassword();   //返回键盘输入的密码
 
#endif // BANK_H_INCLUDED
 

main.cpp

#include <iostream>
#include "bank.h"
using namespace std;
/*主函数:*/
int main()
{
    cout<<"+----------------------+\n";
    cout<<"+    欢迎光临CSDN银行  +\n";
    cout<<"+----------------------+\n";
    Bank b;
    if (pass())
    {
        //Bank b;   //原参考中多创建了一个Bank对象,造成一个诡异的Bug,详细解释见下面“注”处的链接
        b.work();
    }
    return 0;
}

#include <iostream>
#include <fstream>
#include <cstdlib>
#include "bank.h"
#include <ctype.h>
using namespace std;
Bank::Bank()
{
    ifstream infile("account.dat",ios::in);
    if(!infile)    //测试文件打开操作是否成功,不成功则提示后退出。
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    int i=0;
    int acc;   //账号
    string nam;   //姓名
    string pw;   //密码
    double bal;   //金额
    int sta;   //状态
    string num;//手机号
    string addr;//家庭地址
    N=number();
    int a;
    cout<<"需要的开户数:";
    cin>>a;
    user=new User[N+1+a];
    while(infile>>acc>>nam>>pw>>bal>>sta>>num>>addr)   //当读取成功……
    {
        user[i].setUser(acc,nam,pw,bal,sta,num,addr);
        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<<user[i].account<<" ";
        outfile<<user[i].name<<" ";
        outfile<<user[i].password<<" ";
        outfile<<user[i].balance<<" ";
        outfile<<user[i].status<<" ";
        outfile<<user[i].number<<" ";
        outfile<<user[i].address<<" ";
        outfile<<endl;
    }
    outfile.close();
}


int Bank::number()
{
    int i=0;
    int acc;   //账号
    string nam;   //姓名
    string pw;   //密码
    double bal;   //金额
    int sta;   //状态
    string num;//手机号
    string addr;//家庭地址
    ifstream infile("account.dat",ios::in);
    if(!infile)    //测试文件打开操作是否成功,不成功则提示后退出。
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    while(infile>>acc>>nam>>pw>>bal>>sta>>num>>addr)   //当读取成功……
    {
        i++;
    }
    infile.close();
    return i;
}
void Bank::work()
{
    int choice;
    do
    {
        choice = chooseInMenu();
        switch(choice)
        {
        case 1:
            openAccount();
            break;
        case 2:
            cancelAccount();
            break;
        case 3:
            withdraw();
            break;
        case 4:
            drawmoney();
            break;
        case 5:
            showbalance();
            break;
        case 6:
            transferAccounts();
            break;
        case 7:
            reportLoss();
            break;
        case 8:
            cancelLoss();
            break;
        case 9:
            change_Password();
            break;
        case  0:
            cout<<"欢迎下次再来办理业务"<<endl;
        }
    }
    while(choice);
    return;
}

/*功能:开户

说明:在进入系统时,在读入数据过程中,已经记录了用户数为N,在数组中对应下标为0~N-1 

  开户时要增加一个用户,只要为下标为N的数组元素置值,并在成功之后令N++即可。 

  1.   账号由系统自动生成(10001+序号),保证其连续,这样在顺序增加的时候,就保证了其有序。 
  2. */  

void Bank::openAccount()
{
    int acc;   //账号
    string nam;   //姓名
    string pw1,pw2;   //密码
    double bal;   //金额
    int sta;   //状态
    string num;
    string addr;
    cout<<"正在开户"<<endl;
    acc=10001+N;
    cout<<"账号:"<<acc<<endl;
    cout<<"姓名:";
    cin>>nam;
    cout<<"密码:";
    pw1=inputPassword();  //输入密码1
    cout<<"确认密码:";
    pw2=inputPassword();  //输入密码2
    if(pw1==pw2)
    {
        cout<<"金额:";
        cin>>bal;
        sta=0;//正常
        cout<<"户主手机号:"<<num;
        cin>>num;
        cout<<"户主家庭地址:"<<addr;
        cin>>addr;
        user[N].setUser(acc,nam,pw1,bal,sta,num,addr);
        N++;
        cout<<"开户成功!!!!"<<endl;
    }
    else
    {
        cout<<"两次密码不一致,未成功开户!"<<endl; //没有N++,则读入的值无效
    }


}

//销户

  1. /* 
  2. 功能:注销账户 
  3. 说明:找到账户,并将其状态改为2-注销即可。 
  4. 注销前应该检查余额,应该先取款再注销 
  5. */  

void Bank::cancelAccount()
{
    char s;
    int i;
    i=getuser();
    if(i>=0)
    {
        cout<<"确认销户(y/n)?"<<endl;
        cin>>s;
        if(s=='y')
        {
            user[i].showBalance("销户成功!本次取款金额为");
            user[i].balance=0;  //取款后余额变0
            user[i].status=2;  //状态变为注销
        }
        else
            cout<<"销户不成功";
    }
}
//查询余额
void Bank::showbalance()
{
    int i;
    i=getuser();
    if(i>=0)   //说明id账户存在
    {
        user[i].show();
        cout<<"余额:"<<user[i].balance<<endl;
    }
}
//存款
void Bank::drawmoney()
{
    int i;
    i=getuser();
    if(i>=0)
    {
        if(user[i].status==0)
        {
            user[i].show();
            double money;
            cout << "请输入存款金额:";
            cin >> money;
            user[i].balance+=money; //取款成功
            cout<<"存款后,您的余额是:"<<user[i].balance<<"元。"<<" 存款成功"<<endl;
            cout<<"您是否需要选择存款年限?(y/n)"<<endl;
            char a;
            cin>>a;
            if(a=='y')
                calculate_interest();
            else
                cout<<"请您继续操作:"<<endl;
        }
        else if(user[i].status==1)
        {
            cout<<"该用户处于挂失状态,存款失败!"<<endl;
        }
        else
        {
            cout<<"该用户已经销户,存款失败!"<<endl;
        }


    }
    return;


}
//取款
void Bank::withdraw()
{
    int i;
    i=getuser();
    if(i>=0)
    {
        if(user[i].status==0)
        {
            user[i].show();
            double money;
            cout<<"请输入取款金额:";
            cin>>money;
            if(money<=user[i].balance)
            {
                user[i].balance-=money; //取款成功
                cout<<"取款后,您的余额是:"<<user[i].balance<<"元。"<<endl;
            }
            else
            {
                cout<<"您的余额不足,取款失败"<<endl;
            }
        }
        else
            cout<<"该账户无法取款"<<endl;
    }
}
void Bank::transferAccounts()//转账
{
    int in;
    int out;
    out=getuser();
    if(out>=0)
    {
        if(user[out].status==0)
        {
            if(out>=0)
            {
                if(user[out].status==0)
                {
                    cout<<"转到: ";
                    in=getuser();
                    if(in>=0)
                    {
                        if(user[in].isNormalUser())
                        {
                            double money;;
                            cout << "请输入转帐金额:";
                            cin >> money;
                            if(money<=user[out].balance)
                            {
                                user[out].balance-=money;//转账成功
                                user[in].balance+=money;
                                cout<<"转给"<<user[in].account<<"后,您的余额是:"<<user[out].balance<<"元。"<<endl;


                            }
                        }
                    }


                }
                else
                {
                    cout<<"您的余额不足,转账失败。"<<endl;
                }
            }


        }
    }
}
//挂失
void Bank::reportLoss()
{
    int i;
    i=getuser();
    if(i>=0)
    {
        if(user[i].status==0)
        {
            user[i].status=1;
            cout<<"挂失成功"<<endl;
        }
        else if(user[i].status==1)
        {
            cout<<"该账户已经处于挂失状态"<<endl;
        }
        else
        {
            cout<<"该账户已销户,不能挂失"<<endl;
        }
    }
    return;
}
//解挂
void Bank::cancelLoss()
{
    {
        int i;
        i= getuser();  //根据账号查询用户,返回用户的下标
        if(i>=0)   //说明id账户存在
        {
            if(user[i].status==0)
            {
                cout<<"该账户处于正常状态,不需要解除挂失"<<endl;
            }
            else if(user[i].status==1)
            {
                user[i].status=0;
                cout<<"解除挂失成功"<<endl;
            }
            else
            {
                cout<<"该账户已销户,操作无效"<<endl;
            }
        }
        return;
    }
}
//改密
void Bank::change_Password()
{
    int i;
    i=getuser();
    if(i>=0)
    {
        if(user[i].passwordIsRight())
        {
            string p1,p2;
            cout << "请输入新密码:";
            p1=inputPassword();
            cout << "请确认新密码:";
            p2=inputPassword();
            if(p1==p2)//两次输入相符
            {
                user[i].password=p1;
                cout<<"密码修改成功!"<<endl;
            }
            else
            {
                cout<<"两次输入不一致,密码修改失败。"<<endl;
            }


        }
        else
        {
            cout<<"忘记密码时可以验证手机号"<<endl;
            cout<<"请输入手机号:";
            string phonenumber;
            cin>>phonenumber;
            if(phonenumber==user[i].number)
            {
                string p1,p2;
                cout << "请输入新密码:";
                p1=inputPassword();
                cout << "请确认新密码:";
                p2=inputPassword();
                if(p1==p2)//两次输入相符
                {
                    user[i].password=p1;
                    cout<<"密码修改成功!"<<endl;
                }
                cout<<"请您记住使用新密码";
            }
        }


    }
}
void Bank::calculate_interest()
{
    double period,rate;
    char type;
    int i;
    i=getuser();
    if(i>=0)
    {
        if(user[i].status==0)
        {
            cout << "欢迎使用利息计算器!" << endl;
            cout << "======存款期限======" << endl;
            cout << "1. 3个月 " << endl;
            cout << "2. 6个月" << endl;
            cout << "3. 一年 " << endl;
            cout << "4. 二年" << endl;
            cout << "5. 三年" << endl;
            cout << "6. 五年" << endl;
            cout << "请输入存款期限的代号:";
            cin >> type;
            if (type >='1'&& type <='6')
            {
                switch (type)
                {
                case '1':
                    period = 0.25;
                    rate = 0.031;
                    break;
                case '2':
                    period = 0.5;
                    rate = 0.033;
                    break;
                case '3':
                    period = 1;
                    rate = 0.035;
                    break;
                case '4':
                    period = 2;
                    rate = 0.044;
                    break;
                case '5':
                    period = 3;
                    rate = 0.05;
                    break;
                case '6':
                    period = 5;
                    rate = 0.055;
                    break;
                }
                user[i].interest = user[i].balance * period * rate;
                cout << "到期利息为"<<user[i].interest << "元,本息合计共" <<user[i].interest+user[i].balance << "元。"<<endl;
            }
            else
                cout << "选择存款类型错误!" << endl;
            cout << "感谢您的使用,欢迎下次光临!" << endl;


        }
    }

}


  1. /* 
  2. 功能:输入账号查询用户,返回用户在数组中的下标 
  3. 入口参数:要查询用户的账号 
  4. 返回值:如果该用户存在,返回该用户在数组中的下标,否则,返回一个负数(-1) 
  5. 说明: 
  6.   由账号自动产生,使按账号有序,本模块采用二分查找 
  7. */  

int Bank::getuser()
{
    int id;
    cout<<"账号:";
    cin>>id;
    int index=-1;
    int low=0, high=N-1, mid;
    while(low<=high)
    {
        mid=(low+high)/2;
        if(user[mid].account==id)
        {
            index=mid;
            break;
        }
        else if (user[mid].account>id)
            high=mid-1;
        else
            low=mid+1;
    }
    if(index<0)
        cout<<"用户不存在"<<endl;
    return index;

}



#include <iostream>
#include <fstream>
#include <conio.h>
#include <cstdlib>
#include <cstring>
#include <ctype.h>


#include "bank.h"
using namespace std;
bool pass()
{
    bool ipass=false;
    char nameinfile[10];
    char passinfile[10];
    ifstream infile("password.dat",ios::in);
    if(!infile)
    {
        cout<<"password file cannot open!"<<endl;
        exit(1);
    }
    infile>>nameinfile>>passinfile;
    infile.close();
    char name[10];
    char password[10];
    int Try=3;   //进入系统时尝试的次数
    do
    {
        int i=0;
        char ch;
        cout<<"请输入业务员用户名:";
        cin>>name;
        cout<<"请输入密码:";
        while((ch=getch())!='\r')  //getch在接受输入后,不在屏幕上显示
        {
            password[i++]=ch;
            putchar('*');
        }
        password[i]='\0';
        cout<<endl;
        if((strcmp(nameinfile,name)==0)&&(strcmp(passinfile,password)==0))
        {
            ipass=true;
            break;
        }
        else
        {
            --Try;
            if(Try>0)
                cout<<"密码输入错误,您还有"<<Try<<"次输入机会"<<endl;
            else
                cout<<"对不起,您无法进入银行系统";
        }
    }
    while(Try);
    return ipass;
}


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;  //退出for循环后,再次接受
            }
            fflush(stdin); //清除键盘缓存区中已经有的输入
        }
        cout<<endl;
        if(iPass==0)  //此条件成立可能由两种情况引起:输入了非数字字符被直接重置为0,或6位全0后正常退出for循环
        {
            cout<<"密码要求全为数字,且不能全0!"<<endl;;
            cout<<"请重新输入密码: ";
        }
        else
            break;
    }
    return iPass;
}
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;
}



#include <iostream>
#include "bank.h"
using namespace std;
void User::setUser(int acc, string nam, string pw, double bal,int sta,string num,string addr)
{
    account=acc;
    name=nam;
    password=pw;
    balance=bal;
    status=sta;
    number=num;
    address=addr;
}
void User::show()
{
    cout<<"户主姓名:"<<name<<endl;
    cout<<"户主手机号:"<<number<<endl;
    cout<<"户主住址:"<<address<<endl;
    cout<<"状态"<<status<<endl;
}
bool User::passwordIsRight()
{
    bool bpass=false;
    string ipass;
    int num=1;
    do
    {
        cout<<"请输入密码:";
        if(num>1)
            cout<<"这是第"<<num<<"次输入密码,3次不对将吞卡"<<endl;
        ipass=inputPassword();
        num++;
        if(ipass==password)
            bpass=true;
    }
    while(!bpass&&num<4);
    return bpass;
}
void User::showBalance(string prompt)
{
    cout<<prompt<<" "<<balance<<" 元"<<endl;
}
bool User::isNormalUser()
{
    bool normal=true;
    if(status!=0)
    {
        normal=false;
        cout<<"该账户处于"<<(status==1?"挂失":"销户")<<"状态,不能继续操作"<<endl;
    }
    return normal;
}

  1. /* 
  2. 关于getch()的一点说明: 
  3. 所在头文件:conio.h 
  4. 函数用途:从控制台读取一个字符,但不显示在屏幕上 
  5. 函数原型:int getch(void) 
  6. 返回值:读取的字符 
  7. 在不同平台,输入回车,getch()将返回不同数值,而getchar()统一返回10(即\n) 
  8. 1)windows平台下ENTER键会产生两个转义字符 \r\n,因此getch返回13(\r)。 
  9. 2)unix、 linux系统中ENTER键只产生 \n ,因此getch返回10(\n)。 
  10. 3)MAC OS中ENTER键将产生 \r ,因此getch返回13(\r)。 
  11. 为避免键盘缓存区中未读出的字符影响程序,用fflush(stdin);清除输入缓存区 
  12. */  
  13.    


#include <cstring>
#ifndef BANK_H_INCLUDED
#define BANK_H_INCLUDED
using namespace std;


class Bank;
class User
{
public:
    void setUser(int acc, string nam, string pw, double bal,int sta,string num,string addr);
    void show();
    void showBalance(string prompt); //显示余额
    bool passwordIsRight();   //输入的密码正确则返回true
    bool isNormalUser(); //存款、取款、转帐等功能,需要账户处于正常姿态,处于正常状态时返回true,其他情形返回false并提示
    friend class Bank;   //将Bank声明为友元类,方便其访问数据成员
private:
    int account; //账号
    string password;  //密码
    string name;   //用户名
    double balance;  //账户余额
    string number;//手机号
    string address;//家庭住址
    double interest;
    int status;  //状态 0-正常  1-挂失  2-销户
};
class Bank:public User
{
public:
    Bank();  //开始前从文件中读数据,存在数组中
    ~Bank();   //程序结束前,将数组中的数据写入到文件中
    void work();  //业务驱动
    void openAccount(); //开户
    void cancelAccount();  //注销账户
    void drawmoney();   //存款
    void withdraw();   //取款
    void showbalance(); //查询余额
    void transferAccounts();  //转账
    void reportLoss(); //挂失
    void cancelLoss();  //解除挂失
    void change_Password();   //更改密码
    void calculate_interest();//计算利息
    int getuser();  //输入账号查询用户,返回用户在对象数组中的下标
    int number();
private:
    int N;
    User *user;
};
int inputPassword();
bool pass();
int chooseInMenu();


#endif // BANK_H_INCLUDED






0 0