银行系统

来源:互联网 发布:车辆检测数据集 编辑:程序博客网 时间:2024/04/27 13:51

昨天我们老师布置了我们的一个简单的银行系统。刚一看到我一点想法都没有。我回去后仔细想了想用昨天的继承方法可以做的。

首先我定义了第一个类Account作为父类、然后就是SavingAccount作为子类。再定义一个子类CheckingAccount.由于我做的时间不长而且最近比较忙可能没时间在改这个系统了。所以就将就把这个系统先放上来。我的程序的缺点是在运行每一个类的同时我多无法让银行账户的余额做到更新。下面是我的源代码:

#include<iostream>
using namespace std;
class Account
{
friend class CheckingAccount;
protected:
double balance;  //账户余额
public:
Account(double Balance=100);
void credit();//向当前余额加钱
int debit();//从账中取钱
int getBalance();//返回balance值
};
Account::Account(double Balance)
{
  balance=Balance;
}
void Account::credit()
{
int save;
 cout<<"您的银行可用余额为:"<<balance<<endl;
 cout<<"请输入您要存入的金额:"<<endl;
 cin>>save;
 balance=balance+save;
 cout<<"存入后的余额为:"<<endl;
 cout<<balance<<endl;
}
int Account::debit()
{
 int demand;int flag=1;
 cout<<"请输入您要取出的金额:"<<endl;
 cin>>demand;
 if(demand>balance)
 {balance=balance;
 cout<<"对不起!您的余额不足,请充值:"<<endl;
 }
 else
 { balance=balance-demand;
   cout<<"您已成功取出"<<demand<<"元现金"<<endl;
   cout<<"您的余额为"<<getBalance()<<endl;;
   //cout<<"您的余额为"<<balance<<endl;
   flag=0;//表示钱已被取走
 }
  return flag;
}
int Account::getBalance()
{
 return balance;
}
class SavingAccount:public Account
{
friend class CheckingAccount;
private:
//double balance;
double interestrate;//账户的比例
public:
SavingAccount(double Balance=100,double Interestrate=0.2);
int caclculateInterest();
};
SavingAccount::SavingAccount(double Balance,double Interestrate):Account(Balance)
{
getBalance();
balance=balance;
 interestrate=Interestrate;
 //credit();//存
 //debit();//取
}
int SavingAccount::caclculateInterest()
{
double money;
money=balance*interestrate;
return money;//利息
}
class CheckingAccount:public SavingAccount
{
private:
double fare;//表示每笔的费用
public:
CheckingAccount(double Balance=100,double Interestrate=0.2,double Fare=30);
void rescredit();
int resdebit();
};
CheckingAccount::CheckingAccount(double Balance,double Interestrate,double Fare):SavingAccount(Interestrate)
{
balance=Balance;
interestrate=Interestrate;
  fare=Fare;
}
/*void CheckingAccount::rescredit()
{
 credit();
 //caclculateInterest();
 int save;
 cout<<"请输入您要存入的金额:"<<endl;
 cin>>save;
 balance=balance+save;
}*/
int CheckingAccount::resdebit()
{bool flag;
 //credit();
 //debit();
 if(debit()==0)
 {
cout<<"您已成功提出钱!:"<<endl;
balance=balance-fare;
 }
 return balance;
}
void main()
{
 cout<<"************欢迎您使用张新华银行系统************"<<endl;
 cout<<"***********************************"<<endl;
 Account A1;
 A1.credit();A1.debit();A1.getBalance();
 cout<<"***********************************"<<endl;
 SavingAccount S1;
 S1.credit();
 S1.debit();
 S1.getBalance();
 cout<<"账户的利息:"<<S1.caclculateInterest()<<endl;
  cout<<"***********************************"<<endl;
 CheckingAccount C1;
 C1.credit();
 cout<<"收取费用后的余额:"<<C1.resdebit();
 cout<<"取钱收取费用!"<<endl;
 cout<<"收取的费用后余额产生的利息:"<<C1.caclculateInterest()<<endl;;
}

3 0
原创粉丝点击