简单银行账号管理:练习 C++ OOP 规范高效书写代码及 Singleton 设计模式


项目文件包括:

  • Sources
    • Account.cpp (Account class 的实现)
    • Bank.cpp (Bank class 的实现)
    • main.cpp(系统接口)
  • Headers
    • Account.h(Account class 的定义接口)
    • Bank.h(Bank class 的定义接口)

Headers

Account.h

//防卫式声明
#ifndef _ACCOUNT_
#define _ACCOUNT_

#include <string>
#include <iostream>
using namespace std;

class Account{
    //数据成员尽量放在private
private:
    string name;
    string id;
    string code;
    double balance;
    bool isLost;
public:
    //交易记录可被公共访问,因此放在public
    string record="";   //交易记录

    //构造函数尽量用初始化列表,这样只调用复制构造函数,不会调用默认构造函数,效率更高
    //参数尽量 by reference 可以的话尽量加 const
    Account(const string& _id,const string& _name,const double& _balance,const string& _code,const bool _islost=false):
        id(_id),name(_name),code(_code),balance(_balance),isLost(_islost){};

    //没有指针成员,big three 可默认

    //不会修改成员的一定要加const
    void showAllInfo() const {
        cout << endl << "账户信息如下 :" << endl;
        cout << "id = " << id << endl;
        cout << "name = " << name << endl;
        cout << "code = " << code << endl;
        cout << "balance = " << balance << endl;
        cout << "isLost = " << isLost << endl;
        cout << "record=" << record << endl;
        cout << endl;
    }

    //返回值也尽量 by reference,不会修改成员的一定要加const
    const string& get_name() const {return name;}
    const string& get_id() const {return id;}
    const string& get_code() const {return code;}
    const double& get_balance() const {return balance;}
    const bool get_isLost() const {return isLost;}


    //为了练习,下面的函数在 Account.cpp 中实现
    void setisLost(bool state);             //设置挂失状态
    inline void addBalance(const double& add);     //存款
    bool subBalance(const double& sub);     //取款


};

//所有用到的函数都在头文件中声明
ostream& operator<< (ostream& os,const Account& a);


// inline 函数必须在这里定义
void Account::addBalance(const double& add){
    balance+=add;
    record+="add balance " + to_string(add) + "\n";
}

#endif // _ACCOUNT_

Bank.h

#ifndef _BANK_
#define _BANK_

#include <vector>
#include "Account.h"
using namespace std;

class Bank{
private:
    vector<Account*> accounts;
    //将构造函数设为 private 外界不允许通过构造函数创建对象
    Bank(){
        //cout << "CALL" << endl;
    };
    Bank(const Bank& b){};

public:
    //静态成员函数,没有隐藏的this指针,可以不通过对象调用,只能访问静态成员
    static Bank* getSingletonBank(){
        //这个函数是创建对象的唯一接口,必须设为static,因为对象无法在其他地方产生而调用它
        //创建的是静态变量,永远只有一份
        static Bank bank;
        return &bank;
    }

    void showAllInfo() const{
        if (accounts.size()==0){
            cout << "无任何账户" << endl;
            return;
        }
        cout << endl << "账户信息如下 :" << endl;
        for (Account* x:accounts){
            cout << *x << endl;
        }
    }

    bool createAccount();
    bool deleteAccount();
    bool addBalance();
    bool subBalance();
    bool check();
    bool setLost();
};



#endif // _BANK_

Sources

Account.cpp

#include "Account.h"
#include <iostream>
#include <string>
using namespace std;


void Account::setisLost(bool state){
    isLost=state;
}


bool Account::subBalance(const double& sub){
    if (balance<sub) return false;
    balance-=sub;
    record+="sub balance " + to_string(sub) + "\n";
    return true;
}


//重载 output stream << 运算符 只能设为非成员函数,输出会改变状态,因此不能加const
ostream& operator<< (ostream& os,const Account& a){
    //return os << "(" << a.name << "," << a.balance << ")" << endl; //不能这样写,因为数据私有
    return os << "name and balance : (" << a.get_name() << "," << a.get_balance() << ")" << endl;
}

Bank.cpp

#include "Bank.h"
#include <iostream>
using namespace std;

bool Bank::createAccount(){
    //创建账户
    string name,id,code;
    double balance;

    cout << "id : ";cin >> id;
    //查询账户是否存在
    for (Account* x:accounts){
        if (x->get_id()==id){
            cout << "账户已存在" << endl;
            return false;
        }
    }

    cout << "name : ";cin >> name;
    cout << "code : ";cin >> code;
    cout << "balance : ";cin >> balance;

    Account* a=new Account(id,name,balance,code);
    accounts.push_back(a);

    return true;
}


bool Bank::deleteAccount(){
    string id,code;
    cout << "id : ";cin >> id;
    cout << "code : ";cin >> code;
    vector<Account*>::iterator it;
    for (it=accounts.begin();it!=accounts.end();it++){
        if ((*it)->get_id()==id && (*it)->get_code()==code){
            accounts.erase(it);
            cout << "删除成功" << endl;
            return true;
        }
    }
    cout << "用户id不存在或code不正确" << endl;
    return false;
}
bool Bank::addBalance(){
    string id,code;
    double add;
    cout << "id : ";cin >> id;
    cout << "code : ";cin >> code;
    vector<Account*>::iterator it;
    for (it=accounts.begin();it!=accounts.end();it++){
        if ((*it)->get_id()==id && (*it)->get_code()==code){
            cout << "add : ";cin >> add;
            (*it)->addBalance(add);
            cout << "存款成功" << endl;
            return true;
        }
    }
    cout << "用户id不存在或code不正确" << endl;
    return false;
}
bool Bank::subBalance(){
    string id,code;
    double sub;
    cout << "id : ";cin >> id;
    cout << "code : ";cin >> code;
    vector<Account*>::iterator it;
    for (it=accounts.begin();it!=accounts.end();it++){
        if ((*it)->get_id()==id && (*it)->get_code()==code){
            if ((*it)->get_isLost()){
                cout << "挂失无法取款" << endl;
                return false;
            }
            cout << "sub : ";cin >> sub;
            bool flag=(*it)->subBalance(sub);
            if (flag) cout << "取款成功" << endl;
            else cout << "余额不足" << endl;
            return true;
        }
    }
    cout << "用户id不存在或code不正确" << endl;
    return false;
}
bool Bank::check(){
    string id,code;
    cout << "id : ";cin >> id;
    cout << "code : ";cin >> code;
    vector<Account*>::iterator it;
    for (it=accounts.begin();it!=accounts.end();it++){
        if ((*it)->get_id()==id && (*it)->get_code()==code){
            (*it)->showAllInfo();
            return true;
        }
    }
    cout << "用户id不存在或code不正确" << endl;
    return false;
}
bool Bank::setLost(){
    string id,code;
    char state;
    cout << "id : ";cin >> id;
    cout << "code : ";cin >> code;
    vector<Account*>::iterator it;
    for (it=accounts.begin();it!=accounts.end();it++){
        if ((*it)->get_id()==id && (*it)->get_code()==code){
            cout << "LOST?(Y/N)";cin >> state;
            if (state=='Y') (*it)->setisLost(true);
            else (*it)->setisLost(false);
            return true;
        }
    }
    cout << "用户id不存在或code不正确" << endl;
    return false;
}

main.cpp

#include <iostream>
#include "Account.h"
#include "Bank.h"

using namespace std;

int main()
{
    int mode;
    Bank* bank=Bank::getSingletonBank();
    //Bank* bank2=Bank::getSingletonBank();
    //if (bank==bank2) cout << "succeed" << endl;
    while (1){
        system("cls");
		cout<<"----------欢迎来到此银行-----------"<<endl;
		cout<<"----------请选择您的业务-------------"<<endl;
		cout<<"-----------1、开户-------------------"<<endl;
		cout<<"-----------2、销户-------------------"<<endl;
		cout<<"-----------3、存款-------------------"<<endl;
		cout<<"-----------4、取钱-------------------"<<endl;
		cout<<"-----------5、查询-------------------"<<endl;
		cout<<"-----------6、挂失-------------------"<<endl;
		cout<<"-----------7、浏览-------------------"<<endl;
		cin>>mode;

        switch(mode) {
			case 1:
				bank->createAccount();
				system("pause");
				break;
			case 2:
				bank->deleteAccount();
				system("pause");
				break;
			case 3:
				bank->addBalance();
				system("pause");
				break;
			case 4:
				bank->subBalance();
				system("pause");
				break;
			case 5:
				bank->check();
				system("pause");
				break;
			case 6:
				bank->setLost();
				system("pause");
				break;
			case 7:
			    bank->showAllInfo();
			    system("pause");
				break;
			default:
				cout << "输入有误,请重新输入" << endl;
				break;
		}
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_森罗万象

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值