欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C++模拟ATM柜员机程序

程序员文章站 2024-02-27 21:28:03
...

课程题目:模拟ATM柜员机程序
内容要求:使用文本命令行设计模拟ATM柜员机程序。
要求使用文本命令行界面。当输入给定的卡号和密码(初始卡号和密码为123456)时,系统能登录ATM柜员机系统,用户可以按照以下规则进行:
(1)查询余额:初始余额为10000元
(2)ATM取款:每次取款金额为100的倍数,总额不超过5000元,支取金额不允许透支。
(3)ATM存款:不能出现负存款。
(4)修改密码:新密码长度不小于6位,不允许出现6位完全相同的情况,只有旧密码正确,新密码符合要求,且两次输入相同的情况下才可以成功修改密码。

代码如下:

#include <iostream>
#include<windows.h>
#include <string>
using namespace std;
class User
{
private:
    string userName = "123456";
    double balance = 10000;
    string password = "123456";
public:
    void menu();
    void inquire();
    void save();
    void draw();
    void change();
};
void User::inquire()
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
    cout << "                          查询成功!您的余额为:" << balance << "元" << endl << endl;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
        FOREGROUND_BLUE | FOREGROUND_RED);
}
void User::save()
{
    double a;
part5:
    cout << "请输入您的存款金额:";
    cin >> a;
    if (a > 0)
    {
        balance += a;
        cout << endl;
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
        cout << "                  存款成功,您已存入" << a << "元。" << "  " << "卡内余额为:" << balance << "元!" << endl << endl << endl;
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
            FOREGROUND_BLUE | FOREGROUND_RED);
    }
    else
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
        cout << endl << "                      对不起,存款金额不可为负值,请重新输入" << endl << endl;
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
            FOREGROUND_BLUE | FOREGROUND_RED);
        goto part5;

    }
}
void User::draw()
{
    int b;
 part6:
    cout << "请输入您要取款的金额(注:取款金额需为100的倍数):";
    cin >> b;
    if (b > 0 && b % 100 != 0)
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
        cout << endl << "                输入错误,请重新输入!(注:取款金额需为100的倍数)" << endl << endl;
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
            FOREGROUND_BLUE | FOREGROUND_RED);
        draw();
    }
    else
    {
        if (b > balance)
        {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
            cout << endl << "                           对不起,此卡不可透支!" << endl << endl;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
                FOREGROUND_BLUE | FOREGROUND_RED);
            return;
        }
        if (b >= 0 && b <= 5000)
        {
            balance -= b;
            cout << endl;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
            cout << "                  取款成功,您已取出" << b << "元!" << "  " << "卡内余额为:" << balance << "元!" << endl << endl << endl;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
                FOREGROUND_BLUE | FOREGROUND_RED);
        }
        else if (b < 0)
        {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
            cout << endl << "                   取款失败,取款金额不可为负值,请重新输入!" << endl << endl;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
                FOREGROUND_BLUE | FOREGROUND_RED);
            goto part6;
        }
        else if (b > 5000)
        {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
            cout << endl << "            取款失败,每次取款额不得超过5000元!" << endl << endl;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
                FOREGROUND_BLUE | FOREGROUND_RED);
        }
    }
}
void User::change()
{
    string p0, p1, p2;

    cout << "请输入旧密码:";
    cin >> p0;
    if (p0 == password)
    {
    part3:
        cout << "请输入新密码(注:密码长度不低于6且不能为6个相同的数):";
        cin >> p1;
        bool flag = false;
        for (int i = 0; i < p1.length() - 1; i++)
            if (p1[i] != p1[i + 1])
            {
                flag = true;
                break;
            }
        if (p1.length() < 6 || flag == false)
        {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
            cout << endl << endl << "                         输入格式有误,请重新输入!" << endl << endl;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
                FOREGROUND_BLUE | FOREGROUND_RED);
            goto part3;
        }
        else
        {
        part4:
            cout << "请再次输入新密码:";
            cin >> p2;
            if (p2.length() < 6)
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
                cout << endl << endl << "                           输入格式有误,请重新输入!" << endl << endl;
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
                    FOREGROUND_BLUE | FOREGROUND_RED);
                goto  part4;
            }
        }

        if (p1 == p2)
        {
            password = p2;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
            cout << endl << "                         密码修改成功,请重新登录" << endl << endl;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
                FOREGROUND_BLUE | FOREGROUND_RED);
            User::menu();
        }
        else
        {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
            cout << endl << "                        两次密码输入不同,密码修改失败!" << endl << endl << endl;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
                FOREGROUND_BLUE | FOREGROUND_RED);
        }
    }
    else
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
        cout << endl << "                                    密码错误!" << endl << endl;
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
            FOREGROUND_BLUE | FOREGROUND_RED);
    }
}
void User::menu()
{
    int c;
    string n, p;
part1:
    cout << "                                请输入卡号:";
    cin >> n;
    if (n == userName)
    {
    part2:
        cout << "                                请输入密码:";
        cin >> p;
        cout << endl;

        if (p == password)
        {
            system("cls");
            cout << "-------------------------------欢迎登录ATM柜员机--------------------------------" << endl;
            while (1)
            {


                cout << "         - - - - - - -  请选择您所需要的服务,并按回车确认- - - - - - - - " << endl;
                cout << "        |                         【1】查询余额                         |" << endl;
                cout << "        |                         【2】取款                             |" << endl;
                cout << "        |                         【3】存款                             |" << endl;
                cout << "        |                         【4】修改密码                         |" << endl;
                cout << "        |                         【0】退出                             |" << endl;
                cout << "         - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " << endl;
                cin >> c;
                switch (c)
                {
                case 0:
                    return;
                case 1:
                    inquire();
                    break;
                case 2:
                    draw();
                    break;
                case 3:
                    save();
                    break;
                case 4:
                    change();
                    break;
                default:
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
                    cout << "                              无此项,请重新选取!" << endl << endl;
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
                        FOREGROUND_BLUE | FOREGROUND_RED);
                    break;
                }

            }

        }
        else
        {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
            cout << endl << "                         密码错误,请核对后重新输入!" << endl << endl; ;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
                FOREGROUND_BLUE | FOREGROUND_RED);
            goto part2;
        }
    }
    else
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
        cout << endl << "                        用户不存在,请核对后重新输入" << endl << endl;
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
            FOREGROUND_BLUE | FOREGROUND_RED);
        goto part1;
    }

}
int main()
{
    char a = 1;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
        FOREGROUND_BLUE | FOREGROUND_RED);
    User user;
    cout << "                       " << "欢迎使用长春理工大学学生ATM系统" << endl << endl;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
        FOREGROUND_BLUE | FOREGROUND_RED);
    user.menu();
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
    cout << "                       ";
    for (int i = 0; i < 3; i++)
        cout << a << "  ";
    cout << "很高兴为您服务,再见!";
    for (int i = 0; i < 3; i++)
        cout << a << "  ";
    cout << endl << endl;;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
        FOREGROUND_BLUE | FOREGROUND_RED);
    system("pause");
    return 0;
}

注:建议用VS或者codeblocks编译,不建议用VC++6.0,不然你会怀疑人生。

运行如下:
C++模拟ATM柜员机程序
C++模拟ATM柜员机程序
C++模拟ATM柜员机程序
C++模拟ATM柜员机程序
C++模拟ATM柜员机程序