C++设计一个Bank类,实现银行的创建账户存钱取钱查询交易明细

#include<iostream>
#include<iomanip>
using namespace std;
#define NUM 30
struct Deal//定义结构体记录交易
{
    string time;//交易日期
    double amount;//交易金钱数量
};
class Bank
{
private:
    int _count;//账户
    double _restMoney;//账户余额
    string _date;//日期
public:
    //定义结构体数组,记录近NUM天的交易记录
    static struct Deal deals[NUM];
    static int index;//记录索引
public:
    Bank(int cout,string date,double restMoney=0);
    void backin(double money,string date);//存钱
    void bankout(double money,string date);//取钱
    double getMoney();//查询余额
    void showDeals();//查询交易明细
};
struct Deal Bank::deals[30];//初始化结构体数组
int Bank::index=0;
//构造函数,创建账户:账号、开户日期、余额
Bank::Bank(int count,string  date,double restMoney)
{
    _count=count;
    _date=date;
    deals[index].time=date;
    deals[index].amount=restMoney;
    index++;
    _restMoney=restMoney;
}
void Bank::backin(double money,string date)
{
    _restMoney+=money;//余额增加
    _date=date;//记录日期
    deals[index].time=date;//记录交易时间
    deals[index].amount=money;//记录交易金钱数量
    index++;//索引自增
}
void Bank::bankout(double money,string date)
{
    _restMoney-=money;//余额减少
    _date=date;//记录日期
    deals[index].time=date;//记录交易时间
    deals[index].amount=-money;//记录交易金钱数量
    index++;//索引自增
}
double Bank::getMoney()//查询余额
{
    return _restMoney;
}
void Bank::showDeals()//查询交易明细
{
    for(int i=0;i<Bank::index;i++)
    {
        cout<<Bank::deals[i].time<<":"<<Bank::deals[i].amount<<endl;
    }
}
int main()
{
    //创建银行账户
    Bank b(123456,"2020-01-14",100);
    //存钱
    b.backin(10000,"2020-03-02");
    //查询余额
    cout<<"账户余额:"<<b.getMoney()<<endl;
    //取钱
    b.bankout(5000,"2020-05-26");
    //查询余额
    cout<<"账户余额:"<<b.getMoney()<<endl;
    //查询交易明细
    b.showDeals();
    return 0;
}


 

  • 15
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

哪里都是JY

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

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

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

打赏作者

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

抵扣说明:

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

余额充值