C++课堂设计作业银行储蓄管理系统

1.题目描述:设计一个银行储蓄管理系统,以菜单方式工作。
2.功能需求:
(1)设计一个账户类Account, 包括户名、密码、电话、余额等信息。
(2)为新用户开户。
(3)为老用户修改信息,销户、挂失。
(4)可以办理存取款換作。
(5)可以查询每笔历史交易记录。
(6)使用外部文件存储相关数据。
进阶:考虑利息结算、短息服务费等

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string Record[100];//交易记录
struct UserLogin //用户结构体
{
    string Name; //户名
    int ID; //账号
    int Cipher; //密码
    double Money; //余额
    int Phone; //电话
    int stata; //状态
    string record; //交易记录
}ul[100];

class Account
{
private:
    int m; //记录用户数量
public:
    fstream mfile;
    Account()
    {
        m = 0;
    }
    void Open() //用户开户
    {
        ofstream userfile;
        userfile.open("D:\C++2526\Bank\BankData.txt",ios::out|ios::app);
        char a;
        cout << "请输入户名: "; cin >> ul[m].Name; 
    c:  cout << "请输入账号: "; cin >> ul[m].ID;
        for (int i = 1; i < m; i++)
        {
            if (ul[m].ID == ul[i].ID)
            {
                cout << "您输入的账号已存在请重新输入!!!" << endl;
                goto c;
            }
        }
        cout << "请输入密码: "; cin >> ul[m].Cipher; 
        cout << "请输入电话: "; cin >> ul[m].Phone; 
        cout << "是否进行预存款?(Y/N): "; cin >> a;
        if (a == 'Y' || a == 'y')
        {
            cout << "请输入想要预存的金额: "; cin >> ul[m].Money;
            cout << "恭喜,您已经成功开户!" << endl;
        }
        else
        {
            cout << "恭喜,您已经成功开户!" << endl;
            ul[m].Money = 0;
        }
        userfile << ul[m].Name << " " << ul[m].ID << " "<< ul[m].Cipher << " " << ul[m].Phone << " " << ul[m].Money << " ";
        ul[m].stata = 1; userfile << ul[m].stata << endl;
        m++;
        mfile.open("D:\C++2526m.txt", ios::out | ios::app);
        mfile << m;
        mfile.close();
        userfile.close();
    }

    void text() //数据导入
    {
        //用户数量
        mfile.open("D:\C++2526m.txt", ios::in);
        mfile >> m;
        mfile.close();
        //用户信息
        ifstream userfile;
        userfile.open("D:\C++2526\Bank\BankData.txt", ios::in);
        for (int i = 0; i < m; i++)
        {
            userfile >> ul[i].Name >> ul[i].ID >> ul[i].Cipher >> ul[i].Phone >> ul[i].Money >> ul[i].stata;
        }
        userfile.close();
    }

    void take() //用户取款
    {
        ofstream recordfile;
        recordfile.open("D:C++record.txt", ios::out | ios::app);
        int id, cipher;
        double money;
        cout << "请输入账号: "; cin >> id;
        cout << "请输入密码: "; cin >> cipher;
        for (int i = 0; i < m; i++)
        {
            if (id == ul[i].ID && cipher == ul[i].Cipher)
            {
                if (ul[i].stata == 0)
                {
                    cout << "此账户已挂失,无法进行任何操作!\n";
                    break;
                }
                else if (ul[i].stata == 2)
                {
                    cout << "此账户已销户,无法进行任何操作!\n";
                    break;
                }
                else
                {
                    cout << "请输入取款金额: "; cin >> money;
                    string n = to_string(money);
                    Record[i] += "-"+n;
                    recordfile << " " << ul[i].Name << Record[i] << endl;
                    if (money > ul[i].Money)
                    {
                        cout << "抱歉,您的余额不足!\n" << "您账户的可用金额为: " << ul[i].Money << endl;  
                    }
                    else
                    {
                        ul[i].Money = ul[i].Money - money;
                        cout << "恭喜,您以成功取款!\n" << "您账户的可用金额为:" << ul[i].Money << endl;
                    }
                }
            }
        }
        recordfile.close();
    }

    void save()//用户存款
    {
        ofstream recordfile;
        recordfile.open("D:C++record.txt", ios::out | ios::app);
        int id, cipher;
        double money;
        cout << "请输入账号: "; cin >> id;
        cout << "请输入密码: "; cin >> cipher;
        for (int i = 0; i < m; i++)
        {
            if (id == ul[i].ID && cipher == ul[i].Cipher)
            {
                if (ul[i].stata == 0)
                {
                    cout << "您的账户已挂失,无法进行任何操作!\n";
                    break;
                }
                else if (ul[i].stata == 2)
                {
                    cout << "此账户已销户,无法进行任何操作!\n";
                    break;
                }
                else
                {
                    cout << "请输入存入金额: "; cin >> money;
                    string n = to_string(money);
                    Record[i] += "+"+n;
                    recordfile << " " << ul[i].Name << Record[i] << endl;
                    ul[i].Money = ul[i].Money + money;
                    cout << "恭喜,您成功存款!\n" << "您账户的可以金额为:" << ul[i].Money << endl;
                    break;
                }  
            }
        }
        recordfile.close();
    }

    void message() //查询信息
    {
        ifstream userfile;
        int id, cipher;
        cout << "请输入账号: "; cin >> id;
        cout << "请输入密码: "; cin >> cipher;
        for (int i = 0; i < m; i++)
        {
            if (id == ul[i].ID && cipher == ul[i].Cipher)
            {    
                if (ul[i].stata == 0)
                {
                    cout << "您的账户已挂失,无法进行任何操作!\n";
                    break;
                }
                else if (ul[i].stata == 2)
                {
                    cout << "此账户已销户,无法进行任何操作!\n";
                    break;
                }
                else
                {
                    cout << "**********以下就是您的信息**********" << endl;
                    cout << "户名 : " << ul[i].Name << endl;
                    cout << "账号 : " << ul[i].ID << endl;
                    cout << "密码 : " << ul[i].Cipher << endl;
                    cout << "电话 : " << ul[i].Phone << endl;
                    cout << "余额 : " << ul[i].Money << endl;
                    cout << "状态(1为正常0为挂失) : " << ul[i].stata << endl;
                    cout << "************************************" << endl;
                    break;
                }
            }
        }
    }

    void lost() //用户挂失
    {
        ifstream userfile;
        int id, cipher;
        char a;
        cout << "是否对账户进行挂失(Y/N): "; cin >> a;
        if (a == 'y' || a == 'Y')
        {
            cout << "请输入账号: "; cin >> id;
            cout << "请输入密码: "; cin >> cipher;
            for (int i = 0; i < m; i++)
            {
                if (id == ul[i].ID && cipher == ul[i].Cipher)
                {
                    if (ul[i].stata == 0)
                    {
                        cout << "您的账户已挂失,无法进行任何操作!\n";
                        break;
                    }
                    else if (ul[i].stata == 2)
                    {
                        cout << "此账户已销户,无法进行任何操作!\n";
                        break;
                    }
                    else
                    {
                        ul[i].stata = 0;
                        cout << "您的账户已经成功挂失!\n";
                        break;
                    }
                }
            }
        }
    }

    void userdelete() //用户销户
    {
        ifstream userfile;
        int id, cipher;
        cout << "请输入账号: "; cin >> id;
        cout << "请输入密码: "; cin >> cipher;
        for (int i = 0; i < m; i++)
        {
            if (id == ul[i].ID && cipher == ul[i].Cipher)
            {
                if (ul[i].stata == 0)
                {
                    cout << "您的账户已挂失,无法进行任何操作!\n";
                    break;
                }
                else if (ul[i].stata == 2)
                {
                    cout << "此账户已销户,无法进行任何操作!\n";
                    break;
                }
                else
                {
                    char a;
                    cout << "是否确认销户(Y/N): "; cin >> a;
                    if (a == 'y' || a == 'Y')
                    {
                        ul[i].stata = 2;
                    }
                    cout << "您已经成功销户!!!" << endl;
                }
            }
        }
    }
    void edit() //修改信息
    {
        ifstream userfile;
        int id, cipher, phone;
        string name;
        cout << "请输入账号: "; cin >> id;
        cout << "请输入密码: "; cin >> cipher;
        for (int i = 0; i < m; i++)
        {
            if (id == ul[i].ID && cipher == ul[i].Cipher)
            {
                if (ul[i].stata == 0)
                {
                    cout << "您的账户已挂失,无法进行任何操作!\n";
                    break;
                }
                else if (ul[i].stata == 2)
                {
                    cout << "此账户已销户,无法进行任何操作!\n";
                    break;
                }
                else
                {
                    cout << "****************" << endl;
                    cout << "*  1.修改户名  *" << endl;
                    cout << "*  2.修改密码  *" << endl;
                    cout << "*  3.修改电话  *" << endl;
                    cout << "****************" << endl;
                    int sz; cin >> sz;
                    switch (sz)
                    {
                    case 1:
                        cout << "请输入新户名: "; cin >> name;
                        ul[i].Name = name;
                        break;
                    case 2:
                        cout << "请输入新密码: "; cin >> cipher;
                        ul[i].Cipher = cipher;
                        break;
                    case 3:
                        cout << "请输入新电话: "; cin >> phone;
                        ul[i].Phone = phone;
                        break;
                    }
                }
            }
        }
    }

    void record() //查询交易记录
    {
        ifstream recordfile;
        recordfile.open("D:C++record.txt", ios::in);
        for (int i = 0; i < m; i++)
        {
            recordfile >> Record[m];
        }
        recordfile.close();
        int id, cipher;
        cout << "请输入账号: "; cin >> id;
        cout << "请输入密码: "; cin >> cipher;
        for (int i = 0; i < m; i++)
        {
            if (id == ul[i].ID && cipher == ul[i].Cipher)
            {
                if (ul[i].stata == 0)
                {
                    cout << "您的账户已挂失,无法进行任何操作!\n";
                    break;
                }
                else if (ul[i].stata == 2)
                {
                    cout << "此账户已销户,无法进行任何操作!\n";
                    break;
                }
                else
                {
                    cout << "***以下是您的交易记录***" << endl;
                    cout << Record[i] << endl;
                    cout << "************************" << endl;
                }
            }
        }
    }

    void Anew() //数据更新
    {
        //用户信息的更新
        fstream userfile;
        userfile.open("D:\C++2526\Bank\BankData.txt", ios::trunc | ios::binary | ios::out);
        for (int i = 0; i < m; i++)
        {
            userfile << ul[i].Name << " " << ul[i].ID << " " << ul[i].Cipher << " " << ul[i].Phone << " " << ul[i].Money << " " << ul[i].stata << endl;
        }
        userfile.close();
        //用户数量的更新
        mfile.open("D:\C++2526m.txt", ios::trunc | ios::out);
        mfile << m;
        mfile.close();
    }
};

void select() //初始界面 
{
    Account a;
    a.text();
b:  cout << "**********************************************" << endl;
    cout << "*                                            *" << endl;
    cout << "*           *请选择需要办理的业务*           *" << endl;
    cout << "*                                            *" << endl;
    cout << "**********************************************" << endl;
    cout << "*                                            *" << endl;
    cout << "*              *1.开户业务办理               *" << endl;
    cout << "*                                            *" << endl;
    cout << "*              *2 办理取款业务               *" << endl;
    cout << "*                                            *" << endl;
    cout << "*              *3 办理存款业务               *" << endl;
    cout << "*                                            *" << endl;
    cout << "*              *4 查询用户信息               *" << endl;
    cout << "*                                            *" << endl;
    cout << "*              *5.账户进行挂失               *" << endl;
    cout << "*                                            *" << endl;
    cout << "*              *6.账户进行销户               *" << endl;
    cout << "*                                            *" << endl;
    cout << "*              *7.修改用户信息               *" << endl;
    cout << "*                                            *" << endl;
    cout << "*              *8.查询交易记录               *" << endl;
    cout << "*                                            *" << endl;
    cout << "*              *9.退出系统保存数据           *" << endl;
    cout << "*                                            *" << endl;
    cout << "*    注:关闭程序时请输入'9'更新保存数据!!!   * "<< endl;
    cout << "*                                            *" << endl;
    cout << "**********************************************" << endl;
    int sz;
    cout << "请选择办理的业务: "; cin >> sz;
    switch (sz)
    {
    case 1:
        a.Open();
        goto b;
        break;
    case 2:
        a.take();
        goto b;
        break;
    case 3:
        a.save();
        goto b;
        break;
    case 4:
        a.message();
        goto b;
        break;
    case 5:
        a.lost();
        goto b;
        break;
    case 6:
        a.userdelete();
        goto b;
        break;
    case 7:
        a.edit();
        goto b;
        break;
    case 8:
        a.record();
        goto b;
        break;
    case 9:
        a.Anew();
        cout << "正在为你保存数据......................";
        cout << "正在为你关闭系统......................";
        break;
    }
}

int main()
{
    select();
    return 0;
}

运行成功截图

0973e6f681c84c38b5185e824cd02dfb.png

外部文件数据存储成功

交易记录

3bc7f0c45df14216b529a0ede62a12cd.png

用户信息

17b6cd5df44f488bbb15b4293bc9aca4.png

 

 

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值