1021. 多重继承

我的支付宝和余额宝

支付宝AliPay和余额宝AliFund是一对好兄弟,他们来自同一个父类Account。

已知类Account是支付宝AliPay和余额宝AliFund的虚基类,包括两个protected成员数据:
long ID;//账号
string name;//用户名

支付宝AliPay是类Account的保护派生类,包括两个新增protected成员数据:
double amount;//支付宝账户中的金额
int out-limit;//支付宝单次转账上限,单次资金转出不得超出账户中金额也不能超出上限
//当转账要求超出上述限制时,自动转出最大允许金额

余额宝AliFund是类Account的保护派生类,包括三个新增protected成员数据:
double amount;//余额宝账户中的金额
double rate;//余额宝账户中资金年利率
int in-limit;//余额宝单次转账下限,单次资金转入余额宝时不得少于该下限,上不封顶
//当转入资金不足下限金额时,自动忽略该资金转入操作,资金退回

为了实现支付宝账户和余额宝账户间的资金收集和转账等功能,现以AliPay和AliFund为基类,创建一个淘宝账户类My_Ali,包括以下新增数据成员:
bool auto_collect_flag;//资金自动收集标志
int threshold;//资金自动收集阈值
/若资金自动收集标志为true,当用户支付宝账户金额超过资金自动收集阈值threshold时,超过且符合支付宝账户资金转出限制和余额宝资金转入下限的部分资金将自动转入余额宝中/

增加合适的成员函数,可以实现支付宝账户和余额宝账户之间的资金转账和支付宝账户向余额宝账户的资金自动收集功能。

生成以上各类并编写主函数,根据输入的初始账户信息,建立用户对象,根据用户的账户操作信息,输出用户最后的资金信息。

测试输入包含一个测试用例。
第一行为初始账户信息,第一个数据是账号,第二个字符串是用户名,第三个数据是支付宝初始金额,第四个数据是支付宝转账上限,第五个数据是余额宝初始金额,第六个数据是余额宝资金年利率,第七个数据是余额宝转账下限,第八个数据是资金自动收集标志,字符Y代表设置资金自动收集,字符N代表不设置资金自动收集。若设置资金自动收集,第九个数据是资金自动收集阈值,否则,第九个数据不存在。
随后每一行代表用户资金账户的一次操作,第一个数字代表当前操作的账户类型,1代表支付宝账户,2代表余额宝账户,第二个数字代表具体操作,1代表账户资金增加,2代表账户金额减少,3代表从当前操作账户转出资金并同时转入余额宝(当前操作账户为1)或者支付宝(当前操作账户为2),第三个数字代表操作金额。
所有资金转出时均不能超过当前账户现有资金。
最后输入0 0 0时结束。
注意:当设置资金自动收集为真时,支付宝账户资金每次增加时会自动触发资金自动收集。

输入样例:
100001 Zhangsan 1000 30000 50000 0.047 5000 Y 3000
1 1 1500
1 2 500
1 3 500
1 1 7000
2 3 500
0 0 0

输出样例:
Account for Zhangsan
Total: 59000
AliPay: 3500
AliFund: 55500 with rate 4.7%



时间限制
1000 ms
内存限制
65536 kB
代码长度限制
8192 B
判题程序
Standard
作者
YU
来源
final test多重继承


#include<iostream>
#include<iomanip>
using namespace std;
class Account
{
  protected:
    long ID;
    string name;
    Account(){cin>>ID>>name;
    }
};
class AliPay:virtual protected Account
{
  protected:
    double amount;//支付宝账户中的金额
    int out_limit;//支付宝单次转账上限,单次资金转出不得超出账户中金额也不能超出上限
          //当转账要求超出上述限制时,自动转出最大允许金额
    AliPay(){cin>>amount>>out_limit;
    }
    void add(double& pay);
    void sub(double& pay);  
};
void AliPay::add(double& pay)
{
  amount+=pay;
}
void AliPay::sub(double& pay)
{
  if(pay>amount)
    pay=amount;
  if(pay>out_limit)
    pay=out_limit;
  amount-=pay;
}
class AliFund:virtual protected Account
{
  protected:
    double amount;//余额宝账户中的金额
    double rate;//余额宝账户中资金年利率
    int in_limit;//余额宝单次转账下限,单次资金转入余额宝时不得少于该下限,上不封顶
          //当转入资金不足下限金额时,自动忽略该资金转入操作,资金退回
  public:
    AliFund();
    void add(double& pay);
    void sub(double& pay);      
};
void AliFund::add(double& pay)
{
  if(pay>=in_limit)
    amount+=pay;
} 
void AliFund::sub(double& pay)
{
  if(pay>amount)
    pay=amount;
  amount-=pay;
}
AliFund::AliFund()
{
  cin>>amount>>rate>>in_limit;
}
class My_Ali:public AliPay,public AliFund
{
  bool auto_collect_flag;//资金自动收集标志
  int threshold; //资金自动收集阈值//若资金自动收集标志为true,当用户支付宝账户金额超过资金自动收集阈值threshold时,
          //超过且符合支付宝账户资金转出限制和余额宝资金转入下限的部分资金将自动转入余额宝中
  public:
    My_Ali();
    void payadd(double& pay){  AliPay::add(pay);autodo(); 
    }
    void fundadd(double& pay){  AliFund::add(pay);
    }
    void paysub(double& pay){  AliPay::sub(pay);
    }
    void fundsub(double& pay){  AliFund::sub(pay);
    }
    void paytofund(double& pay);
    void fundtopay(double& pay);
    void autodo();
    void out();
};
void My_Ali::out()
{
  cout<<"Account for "<<name<<endl<<"Total: "<<AliFund::amount+AliPay::amount<<endl;
  cout<<"AliPay: "<<AliPay::amount<<endl<<"AliFund: "<<AliFund::amount<<" with rate "<<setiosflags(ios::fixed)<<setprecision(1)<<rate*100<<"%"<<dec<<endl; 
}
void My_Ali::paytofund(double& pay)
{
  AliPay::sub(pay);
  if(pay>=AliFund::in_limit)
    AliFund::add(pay);
    else
      AliPay::add(pay);
}
void My_Ali::fundtopay(double& pay)
{
  AliFund::sub(pay);
  AliPay::add(pay);
  autodo();
}
void My_Ali::autodo()
{
  double t=AliPay::amount-threshold;
  if(auto_collect_flag)
  {
      if(AliPay::amount>threshold)
        paytofund(t);
  }
}
My_Ali::My_Ali()
{
  char t;
  cin>>t;
  if(t=='Y')
  {
    cin>>threshold;
    auto_collect_flag=1;
  }else auto_collect_flag=0;
    
}
int main()
{
  int act,op;
  double money;
  My_Ali t;//t.out();
  cin>>op>>act>>money;
  while(act!=0&&op!=0&&money!=0)
  {
    if(op==1)
    {
      if(act==1) t.payadd(money);
      if(act==2) t.paysub(money);
      if(act==3) t.paytofund(money);
    }
    if(op==2)
    {
      if(act==1) t.fundadd(money);
      if(act==2) t.fundsub(money);
      if(act==3) t.fundtopay(money);
    }//t.out();
    cin>>op>>act>>money;
  }
  t.out();
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值