银行取款机c++实现

在学习unity烦躁之时,自己用c++实现了一个很简单的银行取款系统。功能不是很完善,属于测试版。测试版和最终版本之间的差距还是巨大的,安全问题对于目前的我还是无法解决的。
账号:1008600
密码:000000
以下是银行取款机的源代码:

// 银行ATM取款机.cpp : 定义控制台应用程序的入口点。
//设计者:GooDtoLivE
//来自:科技大学
//学院:信息工程学院
//年级:二年级
//设计时间:2015年8月10

#include "stdafx.h"
#include
#include "string.h"
#include "stdlib.h"
using namespace std;

class consumer;

class ATM                                  // ATM取款机
{
public:
    ATM(consumer& cn) :cnsm(cn) {}
    void welcome();                         // 登陆界面
    bool check_passwd(char n[], char pwd[]);// 核对密码
    void change_passwd();                   // 修改密码
    void fetchmoney();                      // 取款
    void information();                      // 查询信息
    void functionshow();                    // 功能界面
    void guashi();                          //  挂失
    void zhuanzhang();                     // 转账
    void exitATM();                       // 退出系统
    void lock();                           // 锁机
private:
    int times;                          // 记录密码输入次数
    char  cardID[20];
    char psw[10];
    float money;
    consumer&cnsm;
};

class consumer// 用户
{
public:
    friend class ATM;
    consumer(char Name[], char Num[], char Nun[], float Money, char Password[]);
protected:
    char* get_name();                // 取得姓名
    char* get_num();                // 取得卡号
    char* get_nun();                //身份证
    char* get_passwd();             // 取得密码
    float get_money();              // 取得余额
    void set_passwd(char pwd[]);     // 设置密码
    void set_money(float m);        // 取钱
private:
    char passwd[8];                // 用户密码
    char name[20];                // 用户姓名
    char num[20];
    char nun[20];
    float money;
};

consumer::consumer(char Name[], char Num[], char Nun[], float Money, char Password[])
{
    strcpy_s(name, Name);
    strcpy_s(num, Num);
    strcpy_s(nun, Nun);
    money = Money;
    strcpy_s(passwd, Password);
}

char* consumer::get_name()
{
    return name;
}

char* consumer::get_num()
{
    return num;
}

char* consumer::get_nun()
{
    return nun;
}

char* consumer::get_passwd()
{
    return passwd;
}

float consumer::get_money()
{
    return money;
}

void consumer::set_passwd(char pwd[])
{
    strcpy_s(passwd, pwd);
}

void consumer::set_money(float m)
{
    money -= m;
}

void ATM::welcome()
{
    times = 0;
    cout << "\t\t\t****************************\t" << endl;
    cout << endl;
    cout << "\t\t\t建设银行欢迎您\t" << endl;
    cout << ' ' << endl;
    cout << "\t\t\t欢迎使用建设银行ATM取款机" << endl;
    cout << ' ' << endl;
    char pwd[8], num[20], ch;
    int i = 0;
    int count = 0;
    do
    {
        i = 0;
        cout << endl << "\t\t\t请输入卡号:";
        do
        {
            cin.get(ch);
            num[i++] = ch;
        } while (ch != '\n');
        num[i - 1] = '\0';
        i = 0;
        cout << "\t\t\t请输入密码:";
        do
        {
            cin.get(ch);
            pwd[i++] = ch;
        } while (ch != '\n');
        pwd[i - 1] = '\0';
        if (check_passwd(num, pwd) == false)
        {
            count++;
            if (count == 3)
            {
                ATM::lock();
            }
            else
            {
                cout << "\t\t\t请仔细核对账户及密码" << endl;
                cout << endl;
                cout << "\t\t\t您还有" << 3 - count << "次机会" << endl;
            }
        }
        else
        {
            system("cls");
            cout << "\t\t\t恭喜李先生,登录成功" << endl;
            ATM::functionshow();
        }
    } while (true);//times == 2);
}

bool ATM::check_passwd(char num[], char pwd[])
{
    if (strcmp(num, cnsm.get_num()) == 0 && strcmp(pwd, cnsm.get_passwd()) == 0)
        return true;
    else
        return false;
}

void ATM::change_passwd()
{
    char pwd[8], repwd[8];
    times = 0;
    do
    {
        cout << endl << "请输入旧密码:";
        cin >> pwd;
        if (!check_passwd(cnsm.get_num(), pwd))times++;
        else
            break;
    } while (times = 3);
    if (times == 3)lock();
    int t = 0;
    do
    {
        cout << "请输入新密码:";
        cin >> pwd;
        cout << "请再输入一次新密码:";
        cin >> repwd;
        if ((t = strcmp(pwd, repwd)) != 0)cout << "你输入的两次密码不一样,请重新输入!" << endl;
    } while (t != 0);

    cnsm.set_passwd(pwd);
    cout << "密码修改成功,请牢记!" << endl;
}

void ATM::fetchmoney()
{
    float m;
    char ch;
    do
    {
        system("cls");
        cout << endl << "\t\t\t你要取多少钱:" << endl;
        cin >> m;
        while (m <= 0)
        {
            cout << "\t\t\t请输入正确的数字!" << endl;
            cin >> m;
        }
        if (cnsm.get_money() - m<0)
        {
            cout << "\t\t\t对不起,你的余额不足!" << endl;
        }
        else
        {
            cout << endl << "\t\t\t操作成功,请收好钱!" << endl;
            cnsm.set_money(m);
        }
        cout << "\t\t\t是否要继续该项操作:(Y/N) " << endl;
        cin >> ch;
        while (ch != 'n'&&ch != 'N'&&ch != 'Y'&&ch != 'y')
        {
            cin >> ch;
        }
    } while (ch == 'y' || ch == 'Y');
}

void ATM::information()
{
    cout << "\t\t\t**********************************" << endl;
    cout << "\t\t\t*" << endl;
    cout << "\t\t\t* 用户姓名:" << cnsm.get_name() << endl;
    cout << "\t\t\t* 卡号: " << cnsm.get_num() << endl;
    cout << "\t\t\t* 身份证: " << cnsm.get_nun() << endl;
    cout << "\t\t\t* 余额: " << cnsm.get_money() << endl;
    cout << "\t\t\t**********************************" << endl;
    cout << "\t\t\t";
    system("pause");
}

void ATM::functionshow()
{
    int n;
    do
    {
        cout << endl << "\t\t\t请您输入相应的操作序号进行操作:" << endl;
        cout << "\t\t\t1) 修改密码 " << endl;
        cout << "\t\t\t2) 实时取款 " << endl;
        cout << "\t\t\t3) 查看用户信息 " << endl;
        cout << "\t\t\t4) 挂失银行卡 " << endl;
        cout << "\t\t\t5) 用户转账" << endl;
        cout << "\t\t\t6) 退出系统" << endl;
        cin >> n;
        while (n<0 || n>7)
        {
            cout << "\t\t\t请输入正确的操作序号:" << endl;
            cin >> n;
            system("cls");
        }
        switch (n)
        {
        case 1:
            change_passwd();
            break;
        case 2:
            fetchmoney();
            break;
        case 3:
            information();
            break;
        case 4:
            guashi();
            break;
        case 5:
            zhuanzhang();
            system("pause");
            break;
        case 6:
            exitATM();
            break;
        }
        system("cls");
    } while (true);
}

void ATM::guashi()
{
    char ch;
    cout << "\t\t\t您确定要挂失吗?(Y/N) " << endl;
    cin >> ch;
    if (ch == 'Y'&&ch == 'y')
    {
        cout << "\t\t\t挂失成功!请您尽快补办新卡!" << endl;
        cout << endl << "\t\t\t感谢你对本银行的支持,欢迎下次光临!" << endl;
        cout << "\t\t\t系统将自动退出";
        system("pause");
        exit(0);
    }
    else if (ch == 'n'&&ch == 'N')
    {
        cout << "\t\t\t挂失操作已经取消";
        system("pause");
        ATM::functionshow();
    }
    else if ((ch != 'N'&&ch != 'n') || (ch != 'Y'&&ch != 'y'))
    {
        cout << "\t\t\t由于某种原因,您的银行卡未能挂失" << endl;
        cout << "\t\t\t系统将自动退出";
        system("pause");
        exit(0);
    }
}

void ATM::zhuanzhang()
{
    long ID2;
    float money1;
    cout << "\t\t\t请输入您要转账的卡号:" << endl;
    cin >> ID2;
    cout << "\t\t\t你的余额为:" << cnsm.get_money() << endl;
    cout << "\t\t\t请输入您要转账的金额:" << endl;
    cin >> money1;

    if (cnsm.get_money() >= money1)
    {
        cnsm.set_money(money1);
        cout << "\t\t\t转账成功,您卡上余额为:" << cnsm.get_money() << endl;
    }
    else
    {
        cout << "\t\t\t金额超出账户余额,请重新输入取款金额:";
        cin >> money1;
        cnsm.set_money(money1);
        cout << "\t\t\t转账成功,您卡上余额为:" << cnsm.get_money() << endl;
    }
}

void ATM::lock()
{
    cout << endl << "\t\t\t由于您的账号和密码累计3次错误,你的卡已经被没收。 " << endl;
    cout << endl;
    cout << "\t\t\t请尽快带领证件到银行激活您的银行卡" << endl;
    cout << "\t\t\t系统将自动退出" << endl;
    system("\t\t\tpause");
    exit(0);
}

void ATM::exitATM()
{
    cout << endl << "\t\t\t感谢你对本银行的支持,欢迎下次光临!" << endl;
    exit(0);
}

int  main()
{
    consumer c1("李先生", "1008600", "612132199512245613", 450.8, "000000");
    // 构造一个用户
    ATM atm(c1);
    atm.welcome();
    //cout << "这是一个测试程序";
    return 0;
}



这个程序还是有很多bug,自己能力还是很不足的,希望大家多多指正并且提出好的建议

你可能感兴趣的:(C++)