银行系统第一版

职员账户100~104,密码123456

客户账户10000~10100,密码123456

文件链接:http://pan.baidu.com/s/1FIO02

main.cpp

#include<iostream>
#include<fstream>                  //从文件中读数据包含的头文件
#include <windows.h>               //system,Sleep,exit函数的调用需要的头文件
#include<conio.h>                  //调用getch需要用到的头文件
#include"bank.h"
void back_menu()
{
    int iBack;
    cout<<"\n0.返回工作菜单\n";
    while((iBack=getch())!='0')
    {
        ;
    }
}

int main()
{
    Bank a;                                    //定义银行类
    Officer * b=NULL;                          //定义一个职员对象指针,并初始化为NULL
    b=a.find_officer();                          //将find_officer()返回的指针赋值给c
    if(b!=NULL)                                //首先判断b是否为空指针,如果为空直接退出
    {

        bool q=false;
        q=(*b).login();                 //职员登陆
        if(q)                           //如果登陆,进入工作,否者直接退出系统
        {
            a.load_customer_search();              //加载customer的索引
            Sleep(1000);
            system("cls");
            while(1)
            {
                int g=1;
                cout<<"1.工作\t"<<"2.退出"<<endl;
                cin>>g;
                switch(g)
                {
                case 1:
                {
                    Customer *c=NULL;    //定义客户为的指针对象,并初始化为空
                    int i=1;
                    while(i!=10)
                    {
                        system("cls");
                        cout<<"*********程序员银行*********"<<endl;
                        cout<<"                                  当前操作员:"<<(*b).get_name()<<endl;
                        cout<<"1.开户\t"<<"2.存钱"<<endl;
                        cout<<"3.取钱\t"<<"4.信息查询"<<endl;
                        cout<<"5.转账\t"<<"6.改密"<<endl;
                        cout<<"7.挂失\t"<<"8.解挂"<<endl;
                        cout<<"9.销户\t"<<"10.退出"<<endl;
                        cin>>i;
                        switch(i)
                        {
                        case 1:
                        {
                            system("cls");
                            a.open_account();
                            back_menu();
                            break;
                        }
                        case 2:
                        {
                            system("cls");
                            c=new Customer;
                            c=a.find_customer();
                            if(c!=NULL)
                            {
                                (*c).saveMoney();
                                a.save_customer(*c);
                            }
                            else
                            {
                                cout<<"该账户不存在"<<endl;
                            }
                            delete c;
                            back_menu();
                            break;
                        }
                        case 3:
                        {
                            system("cls");
                            c=new Customer;
                            c=a.find_customer();
                            if(c!=NULL)
                            {
                                (*c).drawMoney();
                                a.save_customer(*c);
                            }
                            else
                            {
                                cout<<"该账户不存在"<<endl;
                            }
                            delete c;
                            back_menu();
                            break;
                        }
                        case 4:
                        {
                            system("cls");
                            c=new Customer;
                            c=a.find_customer();
                            if(c!=NULL)
                            {
                                (*c).showCustomer();
                            }
                            else
                            {
                                cout<<"该账户不存在"<<endl;
                            }
                            delete c;
                            back_menu();
                            break;
                        }
                        case 5:
                        {
                            system("cls");
                            c=new Customer;
                            c=a.find_customer();
                            if(c!=NULL)
                            {
                                (*c).transferAccount();
                                a.save_customer(*c);
                            }
                            else
                            {
                                cout<<"该账户不存在"<<endl;
                            }
                            delete c;
                            back_menu();
                            break;
                        }
                        case 6:
                        {
                            system("cls");
                            c=new Customer;
                            c=a.find_customer();
                            if(c!=NULL)
                            {
                                (*c).changePassword();
                                a.save_customer(*c);
                            }
                            else
                            {
                                cout<<"该账户不存在"<<endl;
                            }
                            delete c;
                            back_menu();
                            break;
                        }
                        case 7:
                        {
                            system("cls");
                            c=new Customer;
                            c=a.find_customer();
                            if(c!=NULL)
                            {
                                (*c).turnLost();
                                a.save_customer(*c);
                            }
                            else
                            {
                                cout<<"该账户不存在"<<endl;
                            }
                            delete c;
                            back_menu();
                            break;
                        }
                        case 8:
                        {
                            system("cls");
                            c=new Customer;
                            c=a.find_customer();
                            if(c!=NULL)
                            {
                                (*c).turnUnlost();
                                a.save_customer(*c);
                            }
                            else
                            {
                                cout<<"该账户不存在"<<endl;
                            }
                            delete c;
                            back_menu();
                            break;
                        }
                        case 9:
                        {
                            system("cls");
                            c=new Customer;
                            c=a.find_customer();
                            if(c!=NULL)
                            {
                                a.closeAccount(c);
                            }
                            else
                            {
                                cout<<"该账户不存在"<<endl;
                            }
                            delete c;
                            back_menu();
                            break;
                        }
                        case 10:
                        {
                            system("cls");
                            cout<<"退出中";
                            Sleep(2000);
                            system("cls");
                            exit(1);
                        }
                        }
                    }
                }
                case 2:
                    exit(1);
                }
            }
        }
    }
    else
    {
        cout<<"the account don't exist";
        Sleep(2000);
        exit(1);
    }
}
bank.cpp

#include<iostream>
#include<fstream>
#include <windows.h>               //清屏函数的调用需要的头文件
#include<conio.h>                  //调用getch需要用到的头文件
#include<stdlib.h>
#include"bank.h"
using namespace std;
//**************全局函数************
int get_password()
{
    int password=0;
    int m;
    for(int i=0; i<6;)
    {
        m=getch()-48;
        if(m>=0&&m<=9)
        {
            cout<<'*';
            (password*=10)+=m;
            i++;
        }
    }
    return password;
}
//***************Bank类的成员函数的定义*********************
Bank::Bank()
{
    ifstream infile("officer.txt",ios::ate|ios::binary);
    if(!infile)
    {
        cerr<<"officer information load fail";
        exit(1);
    }
    else
    {
        infile.seekg(ios::beg);
        infile.read((char*)&off_NO,sizeof(off_NO));
        off=new Officer[off_NO];
        int i;
        for(i=0; i<off_NO; i++)
        {
            infile.read((char*)&off[i],sizeof(off[i]));
        }
        infile.close();
    }
    cus=new Customer;
}
Bank::Bank(int a)
{
    off=NULL;
    Bank::load_customer_search();
}
void Bank::open_account()                  //银行的开户函数
{
    char name[20];                                   //输入开户用户的信息
    char idcard[18];
    int password;
    double money=0;
    cout<<"******开户******";
    cout<<"输入姓名:";
    cin>>name;
    cout<<"输入身份证号:";
    int i=0;
    char m;
    while((m=getch())!=13&&i<18)
    {
        cout<<m;
        idcard[i]=m;
        i++;
    }
    cout<<endl<<"输入密码:";
    password=get_password();

    customer_search sear;                             //为开户用户建立索引
    sear.num=search_NO;
    sear.account=search_NO+10000;
    sear.state=true;

    ofstream  outfile1("customersearch.txt",ios::ate|ios::binary|ios::in|ios::out);
    if(!outfile1)
    {
        cerr<<"文件加载失败"<<endl;
        exit(1);
    }
    int NO=search_NO;
    search_NO++;                                                                   //索引的数量增加1

    outfile1.seekp(ios::beg);                                                      //存入索引
    outfile1.write((char*)&search_NO,sizeof(search_NO));
    outfile1.seekp(NO*sizeof(sear),ios::cur);
    outfile1.write((char*)&sear,sizeof(sear));
    outfile1.close();

    delete cus_search;
    load_customer_search();                                                     //将修改后的索引读入

    Customer p(sear.account,name,idcard,password,money,false);                       //建立一个customer类,

    fstream outfile2("customer.txt",ios::ate|ios::binary|ios::in|ios::out);
    if(!outfile2)
    {
        cerr<<"文件加载失败"<<endl;
        exit(1);
    }
    outfile2.seekg(ios::beg);
    outfile2.read((char*)&cus_NO,sizeof(cus_NO));
    cus_NO++;                                                                       //客户的数量增加1

    outfile2.seekp(ios::beg);
    outfile2.write((char*)&cus_NO,sizeof(cus_NO));
    outfile2.seekp(NO*sizeof(p),ios::cur);
    outfile2.write((char*)&p,sizeof(p));
    outfile2.close();
    system("cls");
    cout<<"\n开户成功"<<endl;
    cout<<"账户号码为:"<<sear.account<<endl;

}
void Bank::closeAccount(Customer*a)                      //银行类的销户函数
{
    system("cls");
    bool k;
    k=(*a).login();
    if(k)
    {
        if(!(*a).lost)
        {
            cus_search[cus_cur].num=cus_cur;
            cus_search[cus_cur].account=(*a).account;
            cus_search[cus_cur].state=false;
            if((*a).money!=0)
            {
                cout<<"取出余额:"<<(*a).money<<endl;
            }
            ofstream  outfile1("customersearch.txt",ios::binary|ios::ate|ios::in|ios::out);
            if(!outfile1)
            {
                cerr<<"文件加载失败"<<endl;
                exit(1);
            }
            outfile1.seekp(ios::beg);
            outfile1.seekp(sizeof(int),ios::beg);
            outfile1.seekp(cus_cur*sizeof(cus_search[cus_cur]),ios::cur);
            outfile1.write((char*)&cus_search[cus_cur],sizeof(cus_search[cus_cur]));
            outfile1.close();
            Customer p((*a).account);
            ofstream outfile2("customer.txt",ios::binary|ios::ate|ios::out|ios::in);
            if(!outfile2)
            {
                cerr<<"文件加载失败"<<endl;
                exit(1);
            }
            cus_NO--;
            outfile2.seekp(ios::beg);
            outfile2.write((char*)&cus_NO,sizeof(cus_NO));
            outfile2.seekp(cus_cur*sizeof(p),ios::cur);
            outfile2.write((char*)&p,sizeof(p));
            outfile2.close();
            cout<<"销户成功"<<endl;
        }
        else
            cout<<"当前账号处于挂失状态,不能销户"<<endl;
    }
    else
        cout<<"密码错误"<<endl;
    delete cus;
}
void Bank::load_customer_search()          //加载customer的索引
{
    ifstream infile("customersearch.txt",ios::ate|ios::binary);
    if(!infile)
    {
        cerr<<"customersearch information load fail";
        exit(1);
    }
    else
    {
        int i;
        infile.seekg(ios::beg);
        infile.read((char*)&search_NO,sizeof(search_NO));
        cus_search=new customer_search[search_NO];
        for(i=0; i<search_NO; i++)
        {
            infile.read((char*)&cus_search[i],sizeof(cus_search[i]));
        }
        infile.close();
    }
}
void Bank::save_customer(Customer&a)                 //存储用户信息
{
    ofstream outfile("customer.txt",ios::ate|ios::binary|ios::in|ios::out);
    if(!outfile)
    {
        cerr<<"save file load fail";
        exit(1);
    }
    outfile.seekp(sizeof(int),ios::beg);
    outfile.seekp(cus_cur*sizeof(a),ios::cur);
    outfile.write((char*)&a,sizeof(a));
    outfile.close();
    delete cus;
}
Customer* Bank::find_customer()             //查找客户的函数
{
    cus=new Customer;
    int a;
    cout<<"输入账号:";
    cin>>a;
    int i=0;
    for(i=0; i<search_NO; i++)
    {
        if(cus_search[i].account==a&&cus_search[i].state==1)
        {
            cus_cur=i;
            ifstream infile("customer.txt",ios::binary);
            infile.read((char*)&cus_NO,sizeof(cus_NO));
            infile.seekg(i*sizeof(cus[0]),ios::cur);
            infile.read((char*)&cus[0],sizeof(cus[0]));
            infile.close();
            return cus;
        }
    }
    return NULL;
}
Officer* Bank::find_officer()                //查找职员的函数
{
    int a;
    cout<<"输入账号:";
    cin>>a;
    int i=0;
    for(i=0; i<off_NO; i++)
    {
        if(off[i].account==a)
            return &off[i];
    }
    return NULL;
}




//*********officer类的成员函数**************
Officer::Officer()                                      //对officer的初始化
{
    account=-1;
    *name='\0';
    *idcard='\0';
    password=-1;
}
Officer::Officer(int a,char *b,char *c,int d)
{
    account=a;
    for(int i=0; i<20; i++)
    {
        name[i]=b[i];
    }
    for(int j=0; j<18; j++)
    {
        idcard[j]=c[j];
    }

    password=d;
}
bool Officer::login()                                         //登陆函数
{

    int in_password=0,n=0;
    bool b=false;
    while(n<3&&!b)
    {
        cout<<"输入密码:";
        in_password=get_password();
        if(password==in_password)
        {
            b=true;
            cout<<'\n'<<name<<endl;
            cout<<"登陆成功"<<endl;
        }
        else
        {
            cout<<"\n密码错误"<<endl;
            ++n;
            Sleep(500);
            system("cls");
            cout<<"今日还有"<<3-n<<"次输入机会"<<endl;
        }
    }
    system("cls");
    return b;
}


//***********customer类的成员函数的定义***********
Customer::Customer()
{
    money=0;
    lost=false;
}
Customer::Customer(int a)
{
    account=a;
    *name='\0';
    *idcard='\0';
    password=-1;
    money=0;
    lost=false;

}
void Customer::saveMoney()
{
    double p_money;
    cout<<"输入存款金额:";
    cin>>p_money;
    system("cls");
    cout<<"姓名:"<<name<<endl;
    int i;
    cout<<"存款金额:"<<p_money<<endl;
    cout<< "1.确认       2.取消"<<endl;
    cin>>i;
    system("cls");
    switch(i)
    {
    case 1:
        money+=p_money;
        cout<<"存款成功"<<endl;
        break;
    case 2:
        cout<<"取消成功"<<endl;
        break;
    }
}
void Customer::drawMoney()                   //客户类的取钱函数
{
    bool k;
    k=Customer::login();
    if(k)
    {
        if(!lost)
        {
            double p_money;
            cout<<"输入取款金额:";
            cin>>p_money;
            cout<<endl;
            if(p_money<=money)
            {
                int i;
                system("cls");
                cout<<"取款金额:"<<p_money<<endl;
                cout<< "1.确认       2.取消"<<endl;
                cin>>i;
                system("cls");
                switch(i)
                {

                case 1:
                    money-=p_money;
                    cout<<"取款成功"<<endl;
                    break;
                case 2:
                    cout<<"取消成功"<<endl;
                    break;
                }
            }
            else
            {
                cout<<"余额不足"<<endl;
            }
        }
        else
            cout<<"当前账号处于挂失状态,不能取款"<<endl;
    }
}
void Customer::showCustomer()                      //客户类的信息查询函数
{
    bool k;
    k=Customer::login();
    system("cls");
    if(k)
    {
        cout<<"账号:"<<account<<endl;
        cout<<"姓名:"<<name<<endl;
        cout<<"身份证号码:"<<idcard<<endl;
        cout<<"余额:"<<money<<endl;
        cout<<"状态:";
        if(lost)
            cout<<"挂失"<<endl;
        else
            cout<<"正常"<<endl;
    }


}
void Customer::transferAccount()                    //客户类的转账函数
{
    if(Customer::login())
    {
        if(!lost)
        {
            Bank t(5);
            Customer*k;
            k=new Customer;
            cout<<"转入:\n";
            k=t.find_customer();
            if(k!=NULL)
            {
                double p_money;
                cout<<"输入转账金额:";
                cin>>p_money;
                cout<<endl;
                if(p_money>0)
                {
                    system("cls");
                    if(p_money<=money)
                    {
                        cout<<"转出金额:"<<p_money<<endl;
                        cout<<"转入账号:"<<(*k).get_account()<<endl;
                        int i;
                        cout<<"1.确认       2.取消"<<endl;
                        cin>>i;
                        system("cls");
                        switch(i)
                        {
                        case 1:
                            money-=p_money;
                            (*k).money+=p_money;
                            cout<<"转账成功"<<endl;
                            break;
                        case 2:
                            break;
                        }
                    }
                    else
                        cout<<"余额不足"<<endl;
                }
                else
                    cout<<"转账金额需大于0元"<<endl;
            }
            else
            {
                cout<<"你输入的账户不存在"<<endl;
            }
            t.save_customer(*k);
            delete k;
        }
        else
        {
            cout<<"当前账号处于挂失状态,不能转账"<<endl;
        }
    }
    else
        cout<<"密码错误"<<endl;
}
void Customer::changePassword()               //修改密码的函数
{
    if(!lost)
    {
        int  p,j,k;
        cout<<"输入原密码:";
        p=get_password();
        system("cls");
        if(password==p)
        {
            cout<<"\n输入新密码:";
            j=get_password();
            cout<<"\n再次输入密码:";
            k=get_password();
            system("cls");
            if(j==k)
            {
                password=j;
                cout<<"密码修改成功/n";
            }
            else
                cout<<"\n两次输入的密码不相同修改失败";
        }
        else
            cout<<"\n密码错误";
    }
    else
        cout<<"\n当前用户处于挂失状态无法修改密码";
}
void Customer::turnLost()               //客户类的挂失函数
{
    bool k;
    k=Customer::login();
    if(k)
    {
        if(!lost)
        {
            lost=true;
            cout<<"挂失成功"<<endl;
        }
        else
            cout<<"你账号已挂失"<<endl;
    }
    else
        cout<<"密码错误"<<endl;
}
void Customer::turnUnlost()                //客户类的解挂函数
{
    bool k;
    k=Customer::login();
    if(k)
    {
        if(lost)
        {
            lost=false;
            cout<<"解挂成功"<<endl;
        }
        else
            cout<<"当前账户无需解挂"<<endl;
    }
    else
        cout<<"密码错误";
}
bank.h

#ifndef BANK_H_INCLUDED
#define BANK_H_INCLUDED
#include<cstring>
using namespace std;
class Bank;
class Officer;
class Customer;
//定义的customer索引
struct customer_search
{
    int  num;
    int account;
    bool state;
};
//***********定义的银行类***********
class Bank
{
private:
    int off_NO;                               //已存入的officer数量
    int search_NO;                            //已存入的索引数量
    int cus_NO;                               //已存在的customner账户数
    int cus_cur;                              //当前操作的用户的索引序列
    Customer *cus;                            //定义的Customer指针
    Officer *off;                             //定义的Officer指针
    customer_search *cus_search;              //定义的customer_serach的指针

public:
    Bank();                                    //默认构造函数
    Bank(int a);                               //重载的构造函数
    void open_account();                       // 开户函数
    void closeAccount(Customer*a);             //销户函数
    void save_customer(Customer&a);            //存储用户信息
    void load_customer_search();               //加载customer的索引
    Officer* find_officer();                   //查找职员的函数,
    Customer* find_customer();                 //查找用户的函数,并返回该用户的指针
    ~Bank() {};
};

//***************定义的职员类*****************
class Officer
{
protected:
    int  account;                                        //职员账号
    char name[20];                                           //姓名
    char  idcard[18];                                        //身份证
    int  password;                                       //密码
public:
    Officer();                                                   //无参构造函数
    Officer(int a,char* b,char* c,int d);                        //有参构造函数
    bool login();                                                //用户登录函数
    char* get_name()
    {
        return name;
    }
    ~Officer() {};
    //析构函数用于将保存用户的交易记录,但可行性有待考虑
    friend Bank;                                 //声明为bank类的友元类
};

//**************定义的客户类********
class Customer:public Officer
{
private:
    double money;                           //客户的账户余额
    bool lost;                             //挂失为true,没有挂失为false
public:
    Customer();                            //无参构造函数
    Customer(int a);
    Customer(int a,char* b,char* c,int d,double e,bool f):Officer(a,b,c,d)                //有参构造函数
    {
        money=e,lost=f;
    }
    int get_account()
    {
        return account;
    }
    void saveMoney();                      //存钱函数
    void drawMoney();                      //取钱函数
    void showCustomer();                   //信息查询函数
    void transferAccount();                //转账函数
    void changePassword();                 //修改密码函数
    void turnLost();                       //挂失函数
    void turnUnlost();                     //解挂函数
    void closeAccount();                   //销户函数
    ~Customer() {};                       //析构函数

    friend Bank ;                           //声明为bank的友元类
};

#endif // BANK_H_INCLUDED




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
完整版:https://download.csdn.net/download/qq_27595745/89522468 【课程大纲】 1-1 什么是java 1-2 认识java语言 1-3 java平台的体系结构 1-4 java SE环境安装和配置 2-1 java程序简介 2-2 计算机中的程序 2-3 java程序 2-4 java类库组织结构和文档 2-5 java虚拟机简介 2-6 java的垃圾回收器 2-7 java上机练习 3-1 java语言基础入门 3-2 数据的分类 3-3 标识符、关键字和常量 3-4 运算符 3-5 表达式 3-6 顺序结构和选择结构 3-7 循环语句 3-8 跳转语句 3-9 MyEclipse工具介绍 3-10 java基础知识章节练习 4-1 一维数组 4-2 数组应用 4-3 多维数组 4-4 排序算法 4-5 增强for循环 4-6 数组和排序算法章节练习 5-0 抽象和封装 5-1 面向过程的设计思想 5-2 面向对象的设计思想 5-3 抽象 5-4 封装 5-5 属性 5-6 方法的定义 5-7 this关键字 5-8 javaBean 5-9 包 package 5-10 抽象和封装章节练习 6-0 继承和多态 6-1 继承 6-2 object类 6-3 多态 6-4 访问修饰符 6-5 static修饰符 6-6 final修饰符 6-7 abstract修饰符 6-8 接口 6-9 继承和多态 章节练习 7-1 面向对象的分析与设计简介 7-2 对象模型建立 7-3 类之间的关系 7-4 软件的可维护与复用设计原则 7-5 面向对象的设计与分析 章节练习 8-1 内部类与包装器 8-2 对象包装器 8-3 装箱和拆箱 8-4 练习题 9-1 常用类介绍 9-2 StringBuffer和String Builder类 9-3 Rintime类的使用 9-4 日期类简介 9-5 java程序国际化的实现 9-6 Random类和Math类 9-7 枚举 9-8 练习题 10-1 java异常处理 10-2 认识异常 10-3 使用try和catch捕获异常 10-4 使用throw和throws引发异常 10-5 finally关键字 10-6 getMessage和printStackTrace方法 10-7 异常分类 10-8 自定义异常类 10-9 练习题 11-1 Java集合框架和泛型机制 11-2 Collection接口 11-3 Set接口实现类 11-4 List接口实现类 11-5 Map接口 11-6 Collections类 11-7 泛型概述 11-8 练习题 12-1 多线程 12-2 线程的生命周期 12-3 线程的调度和优先级 12-4 线程的同步 12-5 集合类的同步问题 12-6 用Timer类调度任务 12-7 练习题 13-1 Java IO 13-2 Java IO原理 13-3 流类的结构 13-4 文件流 13-5 缓冲流 13-6 转换流 13-7 数据流 13-8 打印流 13-9 对象流 13-10 随机存取文件流 13-11 zip文件流 13-12 练习题 14-1 图形用户界面设计 14-2 事件处理机制 14-3 AWT常用组件 14-4 swing简介 14-5 可视化开发swing组件 14-6 声音的播放和处理 14-7 2D图形的绘制 14-8 练习题 15-1 反射 15-2 使用Java反射机制 15-3 反射与动态代理 15-4 练习题 16-1 Java标注 16-2 JDK内置的基本标注类型 16-3 自定义标注类型 16-4 对标注进行标注 16-5 利用反射获取标注信息 16-6 练习题 17-1 顶目实战1-单机版五子棋游戏 17-2 总体设计 17-3 代码实现 17-4 程序的运行与发布 17-5 手动生成可执行JAR文件 17-6 练习题 18-1 Java数据库编程 18-2 JDBC类和接口 18-3 JDBC操作SQL 18-4 JDBC基本示例 18-5 JDBC应用示例 18-6 练习题 19-1 。。。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值