【实训项目】银行系统

main.cpp

/***********************************************************
* 版权所有 (C)2015, Lingle
*
* 文件名称: main.cpp
* 文件标识:无
* 内容摘要:主文件。开启银行系统。
* 其它说明:无
* 当前版本: V1.0
* 完成日期: 20150717
*
* 修改记录:/
* 修改日期: /
* 版本号: /
* 修改人: /
* 修改内容:/
**********************************************************/
#include <iostream>
#include "Bank.h"
using namespace std;


int main()
    {
    cout<<"+---------------------------------------+"<<endl;
    cout<<"+\t欢迎使用neemaby银行系统 \t+"<<endl;
    cout<<"+---------------------------------------+"<<endl<<endl;

    Bank b;
    if (pass())
        {
        Bank b;
        b.work();
        }
    return 0;
    }


Bank.h

/***********************************************************
* 版权所有 (C)2015, Lingle
*
* 文件名称: Bank.h
* 文件标识:无
* 内容摘要:头文件集合,包括用户类User、业务记录类Task、银行业务类Bank等声明。
* 其它说明:无
* 当前版本: V1.0
* 完成日期: 20150717
*
* 修改记录:/
* 修改日期: /
* 版本号: /
* 修改人: /
* 修改内容:/
**********************************************************/
#ifndef BANK_H_INCLUDED
#define BANK_H_INCLUDED
#include <cstring>
#include <iostream>
#include <ctime>
using namespace std;

int choosemenu();
int days(int date_year,int date_month,int date_day);
int pass();//通过业务员账号
int inputcode();//使密码以*号显示

class User//用户
    {
public:
    User() {}
    void set_user(int acc, string nam, int pw,double bal,int sta,int y=0,int m=0,int d=0);
    void show_name();//显示用户信息
    void show_balance();//显示余额
    void interes();//计算利息,改变余额

    bool passwordIsRight();//判断密码和账户是否相符
    bool isNormalUser();//判断状态是否正常
    friend class Bank;
protected:
    int ACCOUNT;//用户账户
    string NAME;//用户的名字
    int PASSWORD;//用户密码
    double BALANCE;//用户余额
    int STATUS;//用户状态

    int LA_YEAR;//存储上一次改变余额的时间
    int LA_MONTH;
    int LA_DAY;
    };

class Task
    {
public:
    string ymd;//年月日
    string hms;//时分秒
    string ywinfor;//业务信息
    string lius;//流水账号
    };
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();//输入账号查询用户,返回用户在对象数组中的下标
    void getDate(int getU);//获取今天的日期

//与文件相关的函数,在user_file,cpp中
    void openfile();//打开user.txt文件
    void closefile();//关闭user.txt文件
    int countfile();//进行计数
    void record(int N,string infor);//记录业务到work.txt
    void readout();//从work.txt读取业务
    void filerefer();//输入流水号查询进行过的操作

protected:
    User *users;//user users[upNum];改为动态数组
    Task yw[1000];
    int nownum;//实际用户数目
    };


#endif // BANK_H_INCLUDED


Bank.cpp

/***********************************************************
* 版权所有 (C)2015, Lingle
*
* 文件名称: Bank.cpp
* 文件标识:无
* 内容摘要:实现Bank类中除文件类的函数。
* 其它说明:无
* 当前版本: V1.0
* 完成日期: 20150717
*
* 修改记录:/
* 修改日期: /
* 版本号: /
* 修改人: /
* 修改内容:/
**********************************************************/
#include "Bank.h"
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
using namespace std;


/*********************************************************
* 功能描述:根据choosemenu中的提示选择要实现的业务。
* 输入参数: iChoice-菜单选项
* 输出参数: 1.openAccount()-开户
    2.cancelAccount()-注销账户
    3.save()-存款
    4.withdraw()-取款
    5.showAccount()-查询账户
    6.transferAccounts()-/转账
    7.reportLoss()-挂失
    8.cancelLoss()-解挂
    9.updatePassword()-更改密码
	10.filerefer()-查询流水业务
* 返回值:/
* 其它说明:退出输入0,不打开函数。
************************************************************/
void Bank::work()
    {

    int iChoice;//选择
    do
        {
        iChoice = choosemenu();
        openfile();
        readout();
        switch (iChoice)
            {
        case 1:
            openAccount(); //开户
            break;
        case 2:
            cancelAccount();  //注销账户
            break;
        case 3:
            save();  //存款
            break;
        case 4:
            withdraw();   //取款
            break;
        case 5:
            showAccount(); //查询余额
            break;
        case 6:
            transferAccounts();  //转账
            break;
        case 7:
            reportLoss();  //挂失
            break;
        case 8:
            cancelLoss();  //解除挂失
            break;
        case 9:
            updatePassword();   //更改密码
            break;
        case 10:
            filerefer();//输入流水号查询业务
            break;
        case 0:
            cout<<"欢迎您再来. "<<endl;
            }
        closefile();
        }
    while(iChoice);
    }


/*********************************************************
* 功能描述:开户
* 输入参数: nam-户主姓名
    pw1-密码
    pw2-确定密码
    bal-存款金额
* 输出参数: acc-账号
* 返回值:/
* 其它说明:将开户时间保存到User类数据成员LA_YEAR、LA_MONTH、LA_DAY中。
************************************************************/
void Bank::openAccount()//开户
    {
    int acc;   //账号
    string nam;   //姓名
    int pw1,pw2;   //密码
    double bal;   //金额
    int sta;   //状态

    time_t t;
    tm *lt;
    t = time(NULL);
    lt = localtime(&t);
    double y=lt->tm_year+1900;
    double m=lt->tm_mon+1;
    double d=lt->tm_mday;

    cout<<"开户中…"<<endl;
    acc=10001+nownum;
    cout<<"账号:"<<acc<<endl;
    cout<<"户主姓名:";
    cin>>nam;
    cout<<"密码:";
    pw1=inputcode();  //输入密码1
    cout<<"确认密码:";
    pw2=inputcode();  //输入密码2
    if(pw1==pw2)
        {
        sta=0; //状态“正常”
        cout<<"存款金额:";
        cin>>bal;
        users[nownum].set_user(acc, nam, pw1,bal,sta,y,m,d);
        getDate(nownum);
        ++nownum; //正式用户数增加1
        fflush(stdin); //清除键盘缓存区中已经有的输入
        cout<<"开户成功!"<<endl;
        record(acc,"进行开户,开户成功。");
        }
    else
        {
        cout<<"两次密码不一致,未成功开户!"<<endl; //没有N++,则读入的值无效
        }

    }


/*********************************************************
* 功能描述:注销账户
* 输入参数: getU-账号
    pw-密码
* 输出参数: NAME-账户姓名
   BALANCE-账户余额
* 返回值:/
* 其它说明:注销成功则将STATUS由0变为2(“注销”状态),余额清零即全部取出。
************************************************************/
void Bank::cancelAccount()//注销账户
    {
    int getU;
    getU = getUser();  //根据账号查询用户,返回用户的下标
    if(getU>=0)   //说明id账户存在
        {
        users[getU].show_name();
        if(users[getU].passwordIsRight())
            {
            cout<<"余额:";
            users[getU].interes();
            getDate(getU);
            users[getU].show_balance();   //显示金额
            cout<<"是否确认销户(y/n)?";
            if(tolower(getchar())=='y')
                {
                cout<<"销户成功!您的余额";
                users[getU].show_balance();
                cout<<"将被全部取出!"<<endl;
                users[getU].BALANCE=0;  //取款后余额变0
                users[getU].STATUS=2;  //状态变为注销
                record(getU+10001,"进行销户,销户成功。");
                getDate(getU);
                users[getU].interes();
                }
            else
                {
                cout<<"你取消了操作,销户失败!"<<endl;
                }
            fflush(stdin);  //清除了getchar()时在键盘缓存中的遗留,以免影响后续操作
            }
        }
    }


/*********************************************************
* 功能描述:存款
* 输入参数: getU-账号
    pw-密码
    money-存款额
* 输出参数: NAME-账户姓名
   BALANCE-账户余额
* 返回值:/
* 其它说明:需要判断账户状态是否为0即“正常”。存款显示余额前先计算从上次改变余额后到今天所获利息再显示余额。
************************************************************/
void Bank::save()//存款
    {
    int getU;
    double money;
    getU = getUser();  //根据账号查询用户,返回用户的下标
    users[getU].interes();
    users[getU].interes();
    if(getU >=0)   //说明id账户存在
        {
        if(users[getU].STATUS==0)
            {
            users[getU].show_name();
            cout<<"输入存款额:";
            cin>>money;
            users[getU].BALANCE+=money;
            cout<<"存款后,您的账户有余额:";
            getDate(getU);
            users[getU].interes();
            users[getU].show_balance();
            record(getU+10001,"进行存款,存款成功。");
            }
        else if(users[getU].STATUS==1)
            {
            cout<<"该用户处于挂失状态,存款失败!"<<endl;
            record(getU+10001,"进行存款,存款失败。失败原因:用户处于挂失状态。");
            }
        else
            {
            cout<<"该用户已经销户,存款失败!"<<endl;
            record(getU+10001,"进行存款,存款失败。失败原因:用户已销户。");
            }
        }
    return;
    }


/*********************************************************
* 功能描述:取款
* 输入参数: getU-账号
    pw-密码
    money-取款额
* 输出参数: NAME-账户姓名
   BALANCE-账户余额
* 返回值:/
* 其它说明:需要判断账户状态是否为0即“正常”。存款显示取额前先计算从上次改变余额后到今天所获利息再显示余额。
************************************************************/
void Bank::withdraw()//取款
    {

    int getU ;
    double money;
    getU = getUser();  //根据账号查询用户,返回用户的下标
    if(getU >=0)   //说明id账户存在
        {
        if(users[getU ].isNormalUser())
            {
            users[getU ].show_name();
            if(users[getU ].passwordIsRight())
                {
                cout<<"输入取款额:";
                cin>>money;
                if(money>users[getU ].BALANCE)  //亲,不玩透支
                    {
                    cout<<"余额不足,取款失败!"<<endl;
                    record(getU+10001,"进行取款,取款失败。失败原因:用户余额不足。");
                    }
                else
                    {
                    users[getU ].BALANCE-=money;
                    cout<<"取款后,余额为:";
                    users[getU].interes();
                    getDate(getU);
                    users[getU ].show_balance();
                    record(getU+10001,"进行存款,存款成功。");
                    }
                }
            }

        }
    return;
    }


/*********************************************************
* 功能描述:查询账户
* 输入参数: getU-账号
    pw-密码
    money-存款额
* 输出参数: NAME-账户姓名
   BALANCE-账户余额
   STA-账户状态
* 返回值:/
* 其它说明:显示信息前先计算从上次改变余额后到今天所获利息再显示余额。
************************************************************/
void Bank::showAccount()//查询账户
    {
    int getU ;
    string sta[3]= {"正常","挂失","已经销户"};
    getU = getUser();  //根据账号查询用户,返回用户的下标
    if(getU >=0)   //说明id账户存在
        {
        users[getU ].show_name();

        if(users[getU ].passwordIsRight())
            {
            cout<<"--------------账户信息如下--------------\n";
            cout<<"状态:"<<sta[users[getU].STATUS]<<endl;
            cout<<"账户余额:";
            users[getU].interes();
            getDate(getU);
            users[getU ].show_balance();
            record(getU+10001,"进行查询,查询成功。");
            cout<<"----------------------------------------"<<endl;
            }

        }
    return;
    }


/*********************************************************
* 功能描述:转账
* 输入参数: Wout-由该账户转出
    pw-Wout密码
    money-转款额
    Win-由该账户接收
* 输出参数: NAME-Wout和Wout各自账户姓名
   BALANCE-Wout账户余额
* 返回值:/
* 其它说明:需要判断账户状态是否为0即“正常”。Wout转账前先计算从上次改变余额后到今天所获利息再显示余额,Win接收前也要先计算从上次改变余额后到今天所获利息。
    不能转账给自己且不能透支。
************************************************************/
void Bank::transferAccounts()//转账
    {
    int Win,Wout;
    double money;
    cout<<"转账自";
    Wout= getUser();
    if(Wout>=0)
        {
        if(users[Wout].isNormalUser())
            {
            users[Wout].show_name();
            if(users[Wout].passwordIsRight())
                {
                cout<<"输入转账金额:";
                cin>>money;
                if(money>users[Wout].BALANCE)  //防止透支
                    {
                    cout<<"余额不足,转账失败!"<<endl;
                    record(Wout+10001,"进行转账,转账失败。失败原因:用户余额不足。");
                    }
                else
                    {
                    users[Wout].interes();
                    getDate(Wout);
                    cout<<"转出到";
                    Win = getUser();
                    users[Win].interes();
                    getDate(Win);
                    if(Win>=0)
                        {

                        if(users[Win].isNormalUser())
                            {
                            if (Win==Wout)//不能转给自己
                                {
                                cout<<"不能给自己转账,操作失败!"<<endl;
                                record(Wout+10001,"进行转账,转账失败。失败原因:用户向自己转账。");
                                }
                            else
                                {
                                users[Wout].BALANCE-=money;
                                users[Win].BALANCE+=money;
                                cout<<"转账后,您的余额为:";
                                users[Wout].interes();
                                users[Wout].show_balance();
                                users[Win].interes();
                                record(Wout+10001,"进行转账,转账成功。");
                                }

                            }
                        }

                    }
                }
            }
        }
    }



/*********************************************************
* 功能描述:挂失
* 输入参数: getU-账号
    pw-密码
* 输出参数: NAME-账户姓名
* 返回值:/
* 其它说明:需先判断是否已处于挂失或销户状态。
************************************************************/
void Bank::reportLoss()//挂失
    {
    int getU;
    getU  = getUser();  //根据账号查询用户,返回用户的下标
    if(getU >=0)   //说明id账户存在
        {
        users[getU ].show_name();
        if(users[getU ].passwordIsRight())
            {
            if(users[getU ].STATUS==0)
                {
                users[getU ].STATUS=1;
                cout<<"挂失成功"<<endl;
                record(getU+10001,"进行挂失,挂失成功。");
                }
            else if(users[getU ].STATUS==1)
                {
                cout<<"该账户已经处于挂失状态"<<endl;
                record(getU+10001,"进行挂失,挂失失败。失败原因:用户处于挂失状态。");
                }
            else
                {
                cout<<"该账户已销户,不能挂失"<<endl;
                record(getU+10001,"进行挂失,挂失失败。失败原因:用户已销户。");
                }
            }
        }
    return;
    }


/*********************************************************
* 功能描述:解挂
* 输入参数: getU-账号
    pw-密码
* 输出参数: NAME-账户姓名
* 返回值:/
* 其它说明:需先判断是否已处于挂失或销户状态。
************************************************************/
void Bank::cancelLoss()//解挂
    {
    int getU;
    getU=getUser();
    if (getU>=0)
        {
        users[getU].show_name();
        if(users[getU ].passwordIsRight())
            {
            if(users[getU ].STATUS==1)
                {
                users[getU ].STATUS=0;
                cout<<"解挂成功"<<endl;
                record(getU+10001,"进行解挂,解挂成功。");
                }
            else if(users[getU ].STATUS==0)
                {
                cout<<"该账户无需解挂"<<endl;
                record(getU+10001,"进行解挂,解挂失败。失败原因:用户无需解挂。");
                }
            else
                {
                cout<<"该账户已销户,不能挂失"<<endl;
                record(getU+10001,"进行解挂,解挂失败。失败原因:用户已销户。");
                }
            }
        }
    return;
    }

/*********************************************************
* 功能描述:更改密码
* 输入参数: getU-账号
    pw-旧密码
    pw1-新密码第一次输入
    pw2-新密码重复输入
* 输出参数: NAME-账户姓名
* 返回值:/
* 其它说明:需先判断是否已开户。
************************************************************/
void Bank::updatePassword()//更改密码
    {
    int getU,pw1,pw2;
    getU=getUser();
    if (getU>=0)
        {
        users[getU].show_name();
        if(users[getU ].passwordIsRight())
            {
            cout<<"请输入新密码:";
            pw1=inputcode();
            cout<<"请重复密码:";
            pw2=inputcode();
            if (pw1==pw2)
                {
                users[getU].PASSWORD=pw1;
                cout<<"密码修改成功!"<<endl;
                record(getU+10001,"进行改密,改密成功。");
                }
            else
                {
                cout<<"两次密码不一致,未成功改密!"<<endl;
                record(getU+10001,"进行改密,改密失败。失败原因:用户输入密码不一致。");
                }
            }
        }
    return;
    }


/*********************************************************
* 功能描述:查询用户
* 输入参数: id-账号
* 输出参数: /
* 返回值:用户在对象数组中的下标。
* 其它说明:使用二分法查找。
************************************************************/
int Bank::getUser()//输入账号查询用户,返回用户在对象数组中的下标
    {
    //使用二分查找法查找账户
    int id,mid;
    int ind=-1,high=nownum-1,low=0;
    cout<<"账号:";
    cin>>id;
    while(low<=high)
        {
        mid=(low+high)/2;
        if (users[mid].ACCOUNT==id)
            {
            ind=mid;
            break;
            }
        else if (users[mid].ACCOUNT>id)
            high=mid-1;
        else
            low=mid+1;
        }
    if(ind<0)
        cout<<"该用户不存在,本次操作失败!"<<endl;
    return ind; //若找到,其值在0~N-1间,否则,保持-1
    }


/*********************************************************
* 功能描述:获取今日日期
* 输入参数: getU-账号
* 输出参数:/
* 返回值:/
* 其它说明:本身LA_YEAR、LA_MONTH、LA_DAY在本函数执行前保存的是上次改变余额的日期,便于计算利息(User::interes())
************************************************************/
void Bank::getDate(int getU)//获取今天的日期
    {
    time_t t;
    tm *lt;
    t = time(NULL);
    lt = localtime(&t);
    users[getU].LA_YEAR=lt->tm_year+1900;
    users[getU].LA_MONTH=lt->tm_mon+1;
    users[getU].LA_DAY=lt->tm_mday;
    }


user_file.cpp

/***********************************************************
* 版权所有 (C)2015, Lingle
*
* 文件名称: user_file.cpp
* 文件标识:无
* 内容摘要:实现Bank类中有关文件的函数。
* 其它说明:无
* 当前版本: V1.0
* 完成日期: 20150717
*
* 修改记录:/
* 修改日期: /
* 版本号: /
* 修改人: /
* 修改内容:/
**********************************************************/
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include "Bank.h"
static int worknum=100000000;
using namespace std;


/*********************************************************
* 功能描述:打开文件users.txt
* 输入参数:/
* 输出参数: /
* 返回值:/
* 其它说明:用动态数组开辟新空间存储新User信息。
************************************************************/
void Bank::openfile()
{
    ifstream infile("users.txt",ios::in);
    int i=0;
    if(!infile)
    {
        cerr<<"open user.txt error!"<<endl;
        exit(1);
    }
    nownum=countfile();
    users=new User[nownum+1];
    while(infile>>users[i].ACCOUNT>>users[i].NAME>>users[i].PASSWORD>>users[i].BALANCE>>users[i].STATUS>>users[i].LA_YEAR>>users[i].LA_MONTH>>users[i].LA_DAY)
    {
        ++i;
    }
    infile.close();
}


/*********************************************************
* 功能描述:存储并关闭文件users.txt
* 输入参数:/
* 输出参数: /
* 返回值:/
* 其它说明:将原来的文件清空写入新数据。
************************************************************/
void Bank::closefile()
{
    ofstream outfile1("users.txt",ios::trunc);
    outfile1.close();//用于清空原数据
    ofstream outfile2("users.txt",ios::out);
    if(!outfile2)
    {
        cerr<<"open sers.txt error!"<<endl;
        exit(1);
    }
    for(int i=0; i<nownum; ++i)
    {
        outfile2<<Bank::users[i].ACCOUNT<<'\t'<<Bank::users[i].NAME<<'\t'<<Bank::users[i].PASSWORD<<'\t'<<Bank::users[i].BALANCE<<'\t'<<Bank::users[i].STATUS<<" "<<Bank::users[i].LA_YEAR<<" "<<Bank::users[i].LA_MONTH<<" "<<Bank::users[i].LA_DAY<<endl;
    }
    outfile2.close();
    delete []users; //释放空间,等下一次打开文件时重新开辟
    return;
}


/*********************************************************
* 功能描述:计算users.txt中已有成员数,便于开辟新空间存储新成员
* 输入参数:/
* 输出参数: /
* 返回值:i-已有成员数
* 其它说明:/
************************************************************/
int Bank::countfile()
{
    int acc;   //账号
    string nam;   //姓名
    int pw;   //密码
    double mon;   //金额
    int sta;   //状态
    int y,m,d;
    int i=0;
    int nownum;
    ifstream infile("users.txt");
    while(infile>>acc>>nam>>pw>>mon>>sta>>y>>m>>d)
    {
        ++i;
    }
    infile.close();
    return i;
}


/*********************************************************
* 功能描述:记录操作到文件work.txt
* 输入参数:N-用户账号
    infor-操作名称
* 输出参数: worknum-流水账号,便于查询。
* 返回值:/
* 其它说明:记录操作时间。
************************************************************/
void Bank::record(int N,string infor)//记录业务到work.txt
{
    time_t t;
    tm *lt;
    t = time(NULL);
    lt = localtime(&t);
    ofstream outfile("work.txt",ios::app);
    if(!outfile)
    {
        cerr<<"open work.txt error!"<<endl;
        exit(1);
    }
    outfile<<lt->tm_year+1900<<"/"<<lt->tm_mon+1<<"/"<<lt->tm_mday<<" "<<lt->tm_hour<<":"<<lt->tm_min<<":"<<lt->tm_sec<<'\t';
    outfile<<"用户"<<N<<infor<<" "<<"流水账号为:"<<++worknum<<endl;
    outfile.close();
    delete []yw;
    cout<<"流水账号:"<<worknum<<endl;
}

/*********************************************************
* 功能描述:计算work.txt中已有成员数,便于开辟新空间存储新成员
* 输入参数:/
* 输出参数: /
* 返回值:i-已有成员数
* 其它说明:/
************************************************************/
int Bank::countfile2()//进行work.txt计数
{
    int i=0;
    Task t;
    ifstream infile("work.txt",ios::in);
    if(!infile)
    {
        cerr<<"open work.txt error!"<<endl;
        exit(1);
    }
    while(infile>>t.ymd>>t.hms>>t.ywinfor>>t.lius)
    {
        ++i;
    }
    infile.close();
    return i;
}
/*********************************************************
* 功能描述:存储并关闭文件work.txt
* 输入参数:/
* 输出参数:/
* 返回值:/
* 其它说明:/
************************************************************/
void Bank::readout()//从work.txt读取业务
{
    int i=0;
    ifstream infile("work.txt",ios::in);
    if(!infile)
    {
        cerr<<"open work.txt error!"<<endl;
        exit(1);
    }
    yw=new Task[countfile2()+1];
    while(infile>>yw[i].ymd>>yw[i].hms>>yw[i].ywinfor>>yw[i].lius)
    {
        ++i;
    }
    infile.close();
    worknum=i+100000;
}


/*********************************************************
* 功能描述:查询流水账号
* 输入参数:i-流水账号
* 输出参数: /
* 返回值:/
* 其它说明:/
************************************************************/
void Bank::filerefer()//查询流水账号
{
    int i=1;
    while (i>=0)
    {
        cout<<"请输入流水账号(输入0可退出查询):";
        cin>>i;
        if(i<=worknum&&i>100000)
            cout<<i<<":\t"<<yw[i-100001].ymd<<" "<<yw[i-100001].hms<<" "<<yw[i-100001].ywinfor<<endl;
        else if(i<=0)
            break;
        else
            cout<<"输入错误!";
    }

}


User.cpp

/***********************************************************
* 版权所有 (C)2015, Lingle
*
* 文件名称: User.cpp
* 文件标识:无
* 内容摘要:实现User类中的函数。
* 其它说明:无
* 当前版本: V1.0
* 作 者:刘凌歌
* 完成日期: 20150717
*
* 修改记录:/
* 修改日期: /
* 版本号: /
* 修改人: /
* 修改内容:/
**********************************************************/
#include "Bank.h"
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <ctime>
using namespace std;


/*********************************************************
* 功能描述:与1980.0.0相差天数计算
* 输入参数: date_year-预置时间的年
   date_month-预置时间的月
    date_day-预置时间的日
* 输出参数: days_1980-预置时间与1980.0.0相差天数
* 返回值:days_1980
* 其它说明:需要考虑到闰年闰月。
************************************************************/
int days(int date_year,int date_month,int date_day)
    {
    int i,days_1980=0;
    int day[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
    for ( i=1980; i<date_year; i++)
        {
        days_1980+=365;
        if ((i%4==0&&i%100!=0)||i%400==0)
            days_1980++;
        }
    if ((date_year%4==0&&date_year%100!=0)||date_year%400==0)
        day[2]=29;
    else day[2]=28;
    for (i=0; i<date_month; i++)
        days_1980+=day[i];
    days_1980+=date_day;
    return days_1980;
    }


/*********************************************************
* 功能描述:用户信息存储
* 输入参数:/
* 输出参数: 用户类User的私有数据成员ACCOUNT、NAME等。
* 返回值:/
* 其它说明:/
************************************************************/
void User::set_user(int acc,string nam,int pw,double bal,int sta,int y,int m,int d)
    {
    ACCOUNT=acc;
    NAME=nam;
    PASSWORD=pw;
    BALANCE=bal;
    LA_YEAR=y;
    LA_MONTH=m;
    LA_DAY=d;
    }


/*********************************************************
* 功能描述:显示姓名
* 输入参数:/
* 输出参数: 用户类User的私有数据成员NAME。
* 返回值:/
* 其它说明:/
************************************************************/
void User::show_name()
    {
    cout<<"姓名:"<<NAME<<endl;
    }


/*********************************************************
* 功能描述:显示余额
* 输入参数:/
* 输出参数: 用户类User的私有数据成员BALANCE。
* 返回值:/
* 其它说明:/
************************************************************/
void User::show_balance()//显示余额
    {
    cout<<BALANCE<<" 元"<<endl;
    }


/*********************************************************
* 功能描述:计算利息,将余额改变为本金+利息。
* 输入参数:LA_YEAR-上一次余额改变的时间年
    LA_MONTH-上一次余额改变的时间月
    LA_DAY-上一次余额改变的时间日
* 输出参数: /
* 返回值:/
* 其它说明:无论是Bank中需要开户、查询账户(showAccount())、存取款、转账都要调用该函数。
************************************************************/
void User::interes()//计算利息,改变余额
    {
    time_t t;
    tm *lt;
    t = time(NULL);
    lt = localtime(&t);
    int days1= days(lt->tm_year+1900,lt->tm_mon+1,lt->tm_mday);
    int days2= days(LA_YEAR,LA_MONTH,LA_DAY);
    int d=days1-days2;
    BALANCE=BALANCE*(1+0.35*d/360);
    }


/*********************************************************
* 功能描述:校验密码
* 输入参数:PW-密码
* 输出参数: /
* 返回值:right-密码正确为true,错误为false。
* 其它说明:/
************************************************************/
bool User::passwordIsRight()//校验密码,正确返回true
    {
    int PW;
    bool right=true;
    cout<<"输入密码:";
    PW=inputcode();
    if(PW!=PASSWORD)
        {
        right = false;
        cout<<"输入密码错误,不能继续操作!"<<endl;
        }
    return right;
    }


/*********************************************************
* 功能描述:判断状态是否处于“正常”
* 输入参数:STATUS-状态
* 输出参数:STATUS-为0即正常,为1挂失,为2销户。
* 返回值:normal-“正常”时为true,否则为false。
* 其它说明:/
************************************************************/
bool User::isNormalUser()//判断是否处于“正常”,是则返回true,否则返回false
    {
    bool normal=true;
    if(STATUS!=0)
        {
        normal=false;
        cout<<"该账户处于"<<(STATUS==1?"挂失":"销户")<<"状态,不能继续操作!"<<endl;
        }
    return normal;
    }


work.cpp

/***********************************************************
* 版权所有 (C)2015, Lingle
*
* 文件名称: work.cpp
* 文件标识:无
* 内容摘要:实现菜单选择choosemenu、管理人员登陆pass、输入密码时输出*号inputcode三个函数。
* 其它说明:无
* 当前版本: V1.0
* 完成日期: 20150717
*
* 修改记录:/
* 修改日期: /
* 版本号: /
* 修改人: /
* 修改内容:/
**********************************************************/
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <conio.h>  //getch要用
#include <cstring>
#include "Bank.h"
using namespace std;


/*********************************************************
* 功能描述:选择菜单
* 输入参数:i-操作指令
* 输出参数: /
* 返回值:i-操作指令
* 其它说明:/
************************************************************/
int choosemenu()
{
    int i;
    while(1)
    {
        cout<<endl;;
        cout<<"+--------------------------------------+"<<endl;
        cout<<"+ 1 开户\t2 销户\t\t3 存款 +"<<endl;
        cout<<"+ 4 取款\t5 查询\t\t6 转账 +"<<endl;
        cout<<"+ 7 挂失\t8 解挂\t\t9 改密 +"<<endl;
        cout<<"+10 查询流水业务\t\t0 退出 +"<<endl;
        cout<<"+--------------------------------------+"<<endl;
        cout<<"请输入操作指令:";
        cin>>i;
        if(i>=0 && i<=10)
            break;
        else
            cout<<"请重新选择功能"<<endl;;
    }
    return i;
}

/*********************************************************
* 功能描述:管理员账号验证
* 输入参数:workacc-管理员用户名
   ch-输入字符型密码均已*输出
* 输出参数: /
* 返回值:right-0为不正确,1为正确。
* 其它说明:需要调用文件“password.txt”,在里面可预设管理员信息。
************************************************************/
int pass()
{
    char Wacc[20];   //由文件中读出的业务员用户名
    char Wpass[20];  //文件中保存的密码,这一版本中,用字符保存密码
    char workacc[20];   //业务员登录时输入的用户名
    char workpass[20];  //业务员登录时输入的密码
    char ch;
    int iTry=3;   //进入系统时尝试的次数
    int right = 0;  //要返回的结果:0-不正确 1-正确

    ifstream infile("password.txt",ios::in);
    if(!infile)
    {
        cout<<"open password.txt error!"<<endl;
        exit(1);
    }
    infile>>Wacc>>Wpass;
    infile.close();

    //进入系统,密码三次不对将退出
    while(iTry)
    {
        cout<<"请输入业务员用户名:";
        cin>>workacc;
        cout<<"请输入密码:";
        int i=0;
        while((ch=getch())!='\r')  //getch在接受输入后,不在屏幕上显示
        {
            workpass[i++]=ch;
            putchar('*');   //接受任何字符,屏幕上只显示*
        }
        workpass[i]='\0';
        fflush(stdin);
        cout<<endl;;
        if(strcmp(workpass,Wpass)==0&&strcmp(workacc,Wacc)==0)
        {
            right = 1;
            break;
        }
        else
        {
            iTry--;
            if(iTry>0)
                cout<<"超过3次将退出,你还可以尝试"<<iTry<<"次!"<<endl;
            else
            {
                cout<<"对不起,你不能进入系统."<<endl;;
            }
        }
    }
    return right;
}


/*********************************************************
* 功能描述:输入密码显示*
* 输入参数:pw-密码字符,转为整数型变量CODE来记录。
* 输出参数: CODE-若输入不全为数字,CODE=0,退出输入;若输入全为0,重新输入。
* 返回值:/
* 其它说明:/
************************************************************/
int inputcode()
{
    char pw;
    int CODE=0;
    while (1)
    {
        for (int i=0; i<6; i++)
        {
            pw=getch();
            putchar('*');//输入密码显示*
            if (isdigit(pw))//isdigital()用于判断输入是否为数字,isalpha()用于判断是否输入为英文字母
            {
                CODE=CODE*10+(pw-'0');//CODE用来表示六位长度的数。若不加“-'0'”则CODE记录的不是原密码
            }
            else
            {
                string ymd;
                string hms;
                string ywinfor;
                string lius;
                CODE=0;
                break;//若输入不全为数字,CODE=0,退出输入
            }
        }
        cout<<endl;
        fflush(stdin); //清除键盘缓存区中已经有的输入
        if(CODE==0)//即输入六个0或输入了数字以外的字符
        {
            cout<<"密码需为六位数字组成且不能全为0!"<<endl<<"请重新输入密码:"<<endl;
        }
        else
            break;//这句很重要!
    }
    return CODE;
}



运行结果

开户

管理员登陆失败

管理员登陆成功及成功开户



开户失败



销户(成功、失败)



存款



取款

密码错误及成功取款



取款失败



查询



转账

为了测试利息的计算功能,将10003与100004的记录日期改为2015 7 10:



执行转账操作后的useers.txt:





挂失



挂失失败



解挂(正常、销户、挂失状态下)



改密



失败



查询流水业务



第一笔流水是2015/7/13 11:29:26,这流水记录也是我们的实训记录啊~




退出



happy ending~害羞


第一次编辑:20150717 16:32

第二次编辑:20150717 21:05

第三次编辑:20150718 08:41


  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值