码迷,mamicode.com
首页 > 其他好文 > 详细

课程实训(银行系统)

时间:2015-07-17 22:51:46      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:c++ 生活

/*
*copyright(c) 2015,烟台大学计算机学院
*All rights reserved。
*文件名称:第十九周(银行系统)

*作者:王忠
*完成日期:2015.7.17

*版本号:v1.0
*
*问题描述:做一个简单的,与当前银行ATM类似的程序

*输入描述:按照菜单栏提示输入

*程序输出:

#ifndef HEAD_H_INCLUDED
#define HEAD_H_INCLUDED
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <cstring>
using namespace std;
class Bank;
class User
{
public:
    void setuser(int acc,string nam,int pw,double bal,int sta,char id[18],string add);
    void showuser();//显示名字
    void showbalance(string prompt);//显示余额
    bool passwordright();//验证密码
    bool staisnor();//验证状态
    friend class Bank;
private:
    int account;
    string name;
    int password;
    double balance;
    int status;
    char userID[18];
    string address;
};


class Bank
{
public:
    Bank();
    ~Bank();
    void work();//工作
    void openacc();//开户
    void cancelacc();//销户
    void save();//存款
    void withdraw();//取款
    void showacc();//查询余额
    void transferacc();//转账
    void reloss();//挂失
    void cancelloss();//解除挂失
    void repassword();//更改密码
    int getuser();
private:
    int num;//用户数目
    User *users;
};



int inputpassword();//输入密码
int pass();//业务员
int choosemenu();//服务列表


#endif // HEAD_H_INCLUDED


 

#include <iostream>
#include "head.h"
using namespace std;


int main()
{
    cout<<"+----------------------+\n";
    cout<<"+  »¶Ó­¹âÁÙ¿ÓÄãµãÒøÐÐ  +\n";
    cout<<"+----------------------+\n";
    if(pass())
    {
        Bank b;
        b.work();
    }
    return 0;
}


 

#include <iostream>
#include "head.h"
using namespace std;
void User::setuser(int acc,string nam,int pw,double bal,int sta,char id[18],string add)
{
    account=acc;
    name=nam;
    password=pw;
    balance=bal;
    status=sta;
    for(int i=0;i<10;i++)
      userID[i]=id[i];
    address=add;
}
void User::showuser()
{
    cout<<"用户姓名:"<<name<<endl;
}
void User::showbalance(string prompt)
{
    cout<<prompt<<" "<<balance<<"元"<<endl;
}
bool User::passwordright()
{
    bool t=true;
    int pass;
    cout<<"请输入密码:";
    pass=inputpassword();
    if(pass!=password)
    {
        t=false;
        cout<<"密码错误"<<endl;
    }
    return t;
}
bool User::staisnor()
{
    bool nor=true;
    if(status!=0)
    {
        nor=false;
        cout<<"该账户处于"<<(status==1?"挂失":"销户")<<"状态"<<endl;
    }
    return nor;
}


 

#include <iostream>
#include "head.h"
using namespace std;
Bank::Bank()
{
    ifstream infile("account.txt",ios::in);
    if(!infile)
    {
        cerr<<"account open error"<<endl;
        exit(1);
    }
    int i=0;
    users=new User[i+2000];
    int acc;
    string nam;
    int pw;
    double bal;
    int sta;
    char id[18];
    string add;
    while(infile>>acc>>nam>>pw>>bal>>sta>>id>>add)
    {
        users[i].setuser(acc,nam,pw,bal,sta,id,add);
        i++;
    }
    num=i;
    infile.close();
}
 Bank::~Bank()
{
    ofstream outfile("account.txt",ios::out);
    if(!outfile)
    {
        cerr<<"account open error"<<endl;
        exit(1);
    }
    for(int i=0; i<num; i++)
    {
        outfile<<users[i].account<<" ";
        outfile<<users[i].name<<" ";
        outfile<<users[i].password<<" ";
        outfile<<users[i].balance<<" ";
        outfile<<users[i].status<<" ";
        outfile<<users[i].userID<<" ";
        outfile<<users[i].address<<endl;
    }
    outfile.close();
    delete[]users;
}
void Bank::work()//工作
{
    int choose;
    do
    {
        choose=choosemenu();
        switch(choose)
        {
        case 1:
            openacc();
            break;
        case 2:
            cancelacc();
            break;
        case 3:
            save();
            break;
        case 4:
            withdraw();
            break;
        case 5:
            showacc();
            break;
        case 6:
            transferacc();
            break;
        case 7:
            reloss();
            break;
        case 8:
            cancelloss();
            break;
        case 9:
            repassword();
            break;
        case 0:
            cout<<"欢迎下次再来"<<endl;
        }
    }
    while(choose);
}
void Bank::openacc()//开户
{
    Bank();
    int acc;
    string nam;
    int pw;
    double bal;
    int sta;
    char id[18];
    string add;
    cout<<"正在开户···"<<endl;
    acc=10001+num;
    cout<<"账户名:"<<acc<<endl;
    cout<<"用户名:";
    cin>>nam;
    cout<<"身份证号:";
    cin>>id;
    cout<<"家庭住址:";
    cin>>add;
    int pass1,pass2;
    cout<<"请输入密码:";
    pass1=inputpassword();
    cout<<"请确认密码:";
    pass2=inputpassword();
    if(pass1==pass2)
    {
        pw=pass1;
        sta=0;
        cout<<"存入金额:";
        cin>>bal;
        users[num].setuser(acc,nam,pw,bal,sta,id,add);
        num++;
        cout<<"开户成功"<<endl;
    }
    else
        cout<<"两次输入密码不一致,开户不成功"<<endl;

}
void Bank::cancelacc()//销户
{
    int who;
    who=getuser();
    if(who>=0)
    {
        users[who].showuser();
        if(users[who].passwordright())
        {
            users[who].showbalance("余额");
            cout<<"确认销户(y/n)?";
            if(tolower(getchar())=='y')
            {
                users[who].showbalance("销户成功,本次取款金额为:");
                users[who].balance=0;
                users[who].status=2;
            }
            else
                cout<<"用户已取消操作,销户失败."<<endl;
            fflush(stdin);
        }
    }
}
void Bank::save()//存款
{
    int who;
    double money;
    who=getuser();
    if(who>=0)
    {
        if(users[who].status==0)
        {
            users[who].showuser();
            cout<<"请输入存款金额:";
            cin>>money;
            ofstream file("save.txt",ios::app);
            if(!file)
            {
                cerr<<"save open error"<<endl;
                exit(1);
            }
            file<<users[who].account<<"存入"<<money<<endl;
            file.close();
            int d;
            double s;
            cout<<"请输入您要存入的天数:";
            cin>>d;
            s=0.036/365*d*money;
            cout<<"到期后您的利息为:"<<s<<endl;
            money+=s;
            users[who].balance+=money;
            users[who].showbalance("存入成功,余额为:");
        }
        else if(users[who].status==1)
            cout<<"该用户处于挂失状态,存款失败."<<endl;
        else
            cout<<"该用户已经销户,存款失败."<<endl;
    }
    return;
}
void Bank::withdraw()//取款
{
    int who;
    double money;
    who=getuser();
    if(who>=0)
    {
        if(users[who].staisnor())
        {
            users[who].showuser();
            if(users[who].passwordright())
            {
                cout<<"请输入取款额度:";
                cin>>money;
                if(money>users[who].balance)
                    cout<<"余额不足,取款失败."<<endl;
                else
                {
                    users[who].balance-=money;
                    ofstream file("save.txt",ios::app);
                    if(!file)
                    {
                        cerr<<"save open error"<<endl;
                        exit(1);
                    }
                    file<<users[who].account<<"取出"<<money<<endl;
                    file.close();
                    users[who].showbalance("取款后,余额为:");
                }
            }
        }
    }
    return ;
}
void Bank::showacc()//查询
{
    int who,a;
    string rep;
    char b;
    string sta[3]= {"正常","挂失","已经销户"};
    who=getuser();
    if(who>=0)
    {
        users[who].showuser();
        if(users[who].passwordright())
        {
            users[who].showbalance("余额:");
            cout<<"状态:"<<sta[users[who].status]<<endl;
            cout<<"身份证号:"<<users[who].userID<<endl;
            cout<<"家庭住址:"<<users[who].address<<endl;
            cout<<"是否查看账户记录?(y/n)"<<endl;
            cin>>b;
            if(b=='y')
            {
                ifstream file("save.txt",ios::in);
                if(!file)
                {
                    cerr<<"save open error"<<endl;
                    exit(1);
                }
                while(file>>a)
                {
                    if(a==users[who].account)
                    {
                        file>>rep;
                        cout<<rep<<" ";
                    }
                    else
                        file>>rep;
                }
                file.close();
            }
        }
    }
    return ;
}
void Bank::transferacc()//转账
{
    int whoout,whoin;
    double money;
    cout<<"转出账户:";
    whoout=getuser();
    if(whoout>=0)
    {
        if(users[whoout].staisnor())
        {
            users[whoout].showuser();
            if(users[whoout].passwordright())
            {
                cout<<"输入转账金额:";
                cin>>money;
                if(money>users[whoout].balance)
                    cout<<"余额不足,转账失败."<<endl;
                else
                {
                    cout<<"转账到:";
                    whoin=getuser();
                    if(whoin>=0)
                    {
                        if(users[whoin].staisnor())
                        {
                            users[whoout].balance-=money;
                            users[whoin].balance+=money;
                            ofstream file("save.txt",ios::app);
                            if(!file)
                            {
                                cerr<<"save open error"<<endl;
                                exit(1);
                            }
                            file<<users[whoout].account<<"转账给"<<users[whoin].account<<money<<endl;
                            file<<users[whoin].account<<"收到"<<users[whoout].account<<money<<endl;
                            file.close();
                            users[whoout].showbalance("转账后,余额为:");
                        }
                    }
                }
            }
        }
    }
    return;
}
void Bank::reloss()//挂失
{
    int who;
    who=getuser();
    if(who>=0)
    {
        users[who].showuser();
        if(users[who].passwordright())
        {
            if(users[who].status==0)
            {
                users[who].status=1;
                cout<<"挂失成功"<<endl;
                cout<<"如果银行收回此卡,将会将其邮寄至:"<<users[who].address<<",请确认信息,不符请于银行联系."<<endl;
            }
            else if(users[who].status==1)
                cout<<"该账户已经处于挂失状态."<<endl;
            else
                cout<<"该账户已经销户,挂失失败."<<endl;
        }
    }
    return ;
}
void Bank::cancelloss()//解除挂失
{
    int who;
    who=getuser();
    if(who>=0)
    {
        users[who].showuser();
        if(users[who].passwordright())
        {
            if(users[who].status==0)
                cout<<"该账户处于正常状态,无需解除挂失."<<endl;
            else if(users[who].status==1)
            {
                users[who].status=0;
                cout<<"解除挂失成功."<<endl;
            }
            else
                cout<<"该账户已经销户,解除挂失失败."<<endl;
        }
    }
    return ;
}
void Bank::repassword()//更改密码
{
    int who;
    int pass1,pass2;
    who=getuser();
    if(who>=0)
    {
        users[who].showuser();
        if(users[who].passwordright())
        {
            cout<<"新密码:";
            pass1=inputpassword();
            cout<<"再次输入:";
            pass2=inputpassword();
            if(pass1==pass2)
            {
                users[who].password=pass1;
                cout<<"修改密码成功,请牢记新密码."<<endl;
            }
            else
                cout<<"两次密码不同,修改失败."<<endl;
        }
    }
    return ;
}
int Bank::getuser()
{
    int id;
    cout<<"账号:";
    cin>>id;
    int index=-1;
    int low=0,high=num-1,mid;
    while(low<=high)
    {
        mid=(low+high)/2;
        if(users[mid].account==id)
        {
            index=mid;
            break;
        }
        else if(users[mid].account>id)
            high=mid-1;
        else
            low=mid+1;
    }
    if(index<0)
        cout<<"该账户不存在."<<endl;
    return index;
}


 

#include <iostream>
#include "head.h"
using namespace std;
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<<"密码要求全为数字,但不能全为0."<<endl;
            cout<<"请重新输入:";
        }
        else
            break;
    }
    return ipass;
}
int pass()
{
    char namefile[20];
    char passfile[20];
    ifstream infile("password.txt",ios::in);
    if(!infile)
    {
        cout<<"password.txt open error."<<endl;
        exit(1);
    }
    infile>>namefile>>passfile;
    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,passfile)==0&&strcmp(sname,namefile)==0)
        {
            right=1;
            break;
        }
        else
        {
            itry--;
            if(itry>0)
                cout<<"超过三次将退出,你还可以尝试"<<itry<<"次!"<<endl;
            else
                cout<<"对不起,您不能进入系统."<<endl;
        }
    }
    while(itry);
    return right;
}

int choosemenu()
{
    int i;
    while(1)
    {
        cout<<endl<<endl;
        cout<<"+-------------------+"<<endl;
        cout<<"1开户  2销户  3存款"<<endl;
        cout<<"4取款  5查询  6转账"<<endl;
        cout<<"7挂失  8解挂  9改密"<<endl;
        cout<<"       0退出       "<<endl;
        cout<<"请输入操作指令:";
        cout<<endl<<endl;
        cin>>i;
        if(i>=0&&i<=9)
            break;
        else
            cout<<"请重新选择:"<<endl;
    }
    return i;
}


经过一系列的检查,测试,bug,乱码的层层挑战,终于完成了这个简易的程序,其实一点都不简易,哈哈哈哈

技术分享

 

技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

课程实训(银行系统)

标签:c++ 生活

原文地址:http://blog.csdn.net/wangzhongwangmin/article/details/46931491

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!