C++银行账户管理程序2

    xiaoxiao2022-07-12  168

    c++银行账户管理程序2

    在 C++个人银行账户管理1的基础上进行以下改进: (1)在类SavingsAccount中增加一个静态数据成员total,用来记录各个账户的总金额,并为其增加相应的静态成员函数getTotal用来对其进行访问。 (2)将类SavingsAccount中不需要改变对象状态的成员函数声明为常成员函数,比如getBalance等。 (3)增加日期类Date 整个程序分为5个文件:date.h account.h是类定义头文件,date.cpp account.cpp是类实现文件,main.cpp是主函数文件。

    main.cpp

    #include<iostream> #include"date.h" #include"account.h" using namespace std; double SavingsAccount::total = 0; int main() { SavingsAccount s0(1001,0.015,2008,11,1); SavingsAccount s1(1002,0.015,2008,11,1); Date d01(2008,11,5),d02(2008,12,5),d11(2008,11,25),d12(2008,12,20),d2(2009,1,1); s0.deposit(d01,5000); s1.deposit(d11,10000); s0.deposit(d02,5500); s1.withdraw(d12,4000); s0.settle(d2); s1.settle(d2); cout<<"-----------------------------------------------------------------------"<<endl; s0.show(); s1.show(); cout<<"各账户总额:"<<s0.getTotal()<<endl; }

    account.h

    #ifndef ACCOUNT_H_ #define ACCOUNT_H_ #include"date.h" class SavingsAccount { int Id; //帐号 double balance; //余额 double Rate; //年利率 Date lastDate; //上次变更余额的日期 double accumulation=0; //余额按日累加之和 double static total; double accumulate(Date date); //获得到指定日期为止的存款金额按日累积值 public: SavingsAccount(int id, double rate,int year,int month,int day); //构造函数 void deposit(Date date, double amount); //存入现金,date为日期,amount为金额 void withdraw(Date date, double amount); //取出现金 void settle(Date date); //结算利息,每年1月1日调用一次该函数 void show(); //输出账户信息 int getId() {return Id;} double getBalance () { return balance;} double getRate() {return Rate;} double static getTotal(); }; #endif // ACCOUNT_H_INCLUDED #endif // ACCOUNT_H_INCLUDED

    account.cpp

    #include<iostream> #include "account.h" using namespace std; double SavingsAccount::accumulate(Date date) { return balance*lastDate.distance(date); } SavingsAccount::SavingsAccount(int id,double rate,int year,int month,int day) :lastDate(year,month,day) { Id=id; Rate=rate; balance=0; cout<<year<<"/"<<month<<"/"<<day<<"\t"; cout<<"账户"<<Id<<"创建完成!"<<endl; } void SavingsAccount::deposit(Date date, double amount) 存入现金,date为日期,amount为金额 { accumulation += accumulate(date); balance+=amount; lastDate = date; total += amount; cout<<Id<<"存入:"<<amount<<"!\t"<<Id<<" 余额:"<<balance<<endl; } void SavingsAccount::withdraw(Date date, double amount) 取出现金 { accumulation += accumulate(date); if(balance>=amount) balance-=amount; else cout<<"余额不足"<<endl; lastDate=date; total -= amount; cout<<Id<<"取出:"<<amount<<"!\t"<<Id<<" 余额:"<<balance<<endl; } void SavingsAccount::settle(Date date) //结算利息,每年1月1日调用一次该函数 { accumulation += accumulate(date); double settle = (accumulation/356)*Rate; cout<<Id<<"结算利息:"<<settle<<endl; deposit(date,settle); } void SavingsAccount::show() //输出账户信息 { cout<<"帐号:"<<Id<<endl<<"余额:"<<balance<<endl; } double SavingsAccount::getTotal() { return total; }

    date.h

    #ifndef DATE_H_INCLUDED #define DATE_H_INCLUDED class Date { int year, month, day; int totalDays; //该日期是从公元元年1月1日开始的第几天 public: Date(int year, int month, int day); int getYear() const { return year; } int getMonth() const { return month; } int getDay() const { return day; } void show() const; //输出当前日期 bool isLeapYear(int year) const; //判断当年是否为闰年 int distance(const Date& date) const;//计算当前日期与指定日期之间相差天数 }; #endif // DATE_H_INCLUDED

    date.cpp

    #include<iostream> #include "date.h" using namespace std; Date::Date(int Year, int Month, int Day) { year=Year; month=Month; day=Day; int Days=0; for(int i=0;i<year;i++) { if(isLeapYear(i)==true) Days=Days+366; else Days=Days+365; } switch(month){ case 1:Days+=0;break; case 2:Days+=31;break; case 3:Days+=59;break; case 4:Days+=90;break; case 5:Days+=120;break; case 6:Days+=151;break; case 7:Days+=181;break; case 8:Days+=212;break; case 9:Days+=243;break; case 10:Days+=273;break; case 11:Days+=304;break; case 12:Days+=334;break; default:cout<<"month error!"<<endl; } totalDays=Days+day; } void Date::show() const { cout<<getYear()<<"/"<<getMonth()<<"/"<<getDay()<<endl; } //输出当前日期 bool Date::isLeapYear(int year) const { if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return true; else return false; } //判断当年是否为闰年 int Date::distance(const Date& date) const { cout<<date.year<<"/"<<date.month<<"/"<<date.day<<"\t"; return date.totalDays-totalDays; } //计算当前日期与指定日期之间相差天数

    代码运行截图

    更多见:

    C++银行账户管理1 C++银行账户管理3 C++银行账户管理4

    最新回复(0)