1. 程式人生 > >【實訓專案】銀行系統

【實訓專案】銀行系統

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