一个过于简易的银行储蓄系统的c++风格实现

头文件deposit_card.h

#ifndef DEPOSIT_CARD_H
#define DEPOSIT_CARD_H
#include <string>
using namespace std;

class deposit_card
{
    public:
        deposit_card();
        ~deposit_card();
         void check_record(void)const;
        void remain_amt(void)const;
        void deposit(float);
        void withdrawal(float);
    private:
         int oprts=0;
        float money=0;
        string records[32];
        float opmoney[32];

        static int t;
};

#endif // DEPOSIT_CARD_H

deposit_card.cpp

#include "deposit_card.h"
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int deposit_card::t = 0;
deposit_card::deposit_card()
{
    t++;
}

deposit_card::~deposit_card()
{
    t--;
}

void deposit_card::remain_amt()const
{
    cout<<money<<endl;
}
void deposit_card::check_record()const
{
    int ts = oprts;
    for(int i = 0;i < ts;i++){
        cout<<records[i];
        cout<<" "<<opmoney[i]<<endl;
    }
}
void deposit_card::deposit(float g_number)
{
    if(g_number >= 0){
    money += g_number;
    opmoney[oprts] = g_number;
    time_t current;
    current = time(&current);
    string timestring = ctime(&current);
    timestring = timestring.substr(0,timestring.length()-1);
    records[oprts] = timestring;
    oprts ++;
}
    else cout<<"the value can not be a negative number"<<endl;
}
void deposit_card::withdrawal(float g_number)
{
    if(money > g_number){
    money -= g_number;
    opmoney[oprts] = -g_number;
    time_t current;
    current = time(&current);
    string timestring = ctime(&current);
    timestring = timestring.substr(0,timestring.length()-1);
    records[oprts] = timestring;
    oprts ++;
    }
    else cout<<"the value can not be larger than your savings!"<<endl;
}

主程序

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

int main()
{
        cout<<R"(press 1 to create a new account(but now 1 is out of use)
press 2 to deposit & withdrawal
press 3 to check the records
press 4 to check money remain)"<<endl;
    int flag1,flag2,f_account,f_number,f;
    deposit_card d1,d2,d3;
    while(cin >> flag1){
    if ( flag1 == 1 ) cout<<"nothing happens and see the code u will know why QAQ"<<endl;
    if ( flag1 == 2 )
        {
        cout<<"key in your account"<<endl;
        cin>>f_account;
        cout<<"1 for deposit and 2 for withdrawal"<<endl;
        cin>>flag2;
            if ( flag2 == 1 )
            {
                cout<<"key in the number you deposit";
                cin>>f_number;
                if(f_account == 1) d1.deposit(f_number);
                if(f_account == 2) d2.deposit(f_number);
                if(f_account == 3) d3.deposit(f_number);
            }
            if( flag2 == 2 )
            {
                cout<<"key in the number you withdrawal";
                cin>>f_number;
                if(f_account == 1) d1.withdrawal(f_number);
                if(f_account == 2) d2.withdrawal(f_number);
                if(f_account == 3) d3.withdrawal(f_number);
            }
        }
    if ( flag1 == 3 )
        {
        cout<<"key in your account"<<endl;
        cin>>f_account;
        if(f_account == 1) d1.check_record();
        if(f_account == 2) d2.check_record();
        if(f_account == 3) d3.check_record();
        }
    if ( flag1 == 4 )
    {
        cout<<"key in your account"<<endl;
        cin>>f_account;
        if(f_account == 1) d1.remain_amt();
        if(f_account == 2) d2.remain_amt();
        if(f_account == 3) d3.remain_amt();
    }
    cout<<R"(press 1 to create a new account
press 2 to deposit & withdrawal
press 3 to check the records
press 4 to check money remain)"<<endl;
}
}

由于我太菜了的原因,居然只会用 if(f_account == 1) d1…这种方法来做索引…其实这次的代码和我之前写的用struct 实现理论上没什么区别(只在于电脑提供的指针或我这样人工插眼).
UPD:其实可以直接创建个数组 和struct建立数组的方法是一样的
但是代码的可读性、简洁度是不是上升了很多?(应该是吧)
(前篇:https://blog.csdn.net/weixin_45817880/article/details/104909981
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值