【案例】C++个人银行账户管理程序

本文详细描述了一个名为SavingsAccount的类的设计,用于管理活期存储账户,涉及余额、年利率、存款、取款、利息结算等功能,特别强调了如何计算日均余额和按日累计值以正确计算利息。
摘要由CSDN通过智能技术生成

要求:

    一个人可以有多个活期存储账户,一个活期存储账户包括账号(id)、余额(balance)、年利率(rate)等信息,还包括显示账户信息(show)、存款(deposit)、取款(withdraw)、结算利息(settle)等操作,为此,设计一个类SavingsAccount,将id、balance、rate均作为其成员数据,将deposit、withdraw、settle均作为其成员函数。

   实现该类的难点在于利息的计算。由于账户的余额是不断变化的,因此不能通过余额与年利率相乘的办法来计算年利,而是需要将一年当中每天的余额累积起来再除以一年的总天数,得到一个日均余额,再乘以年利率。为了计算余额的按日累计值,SavingsAccount引入了私有数据成员lastDate、accumulation和私有成员函数accumulate。lastDate用来存储上一次余额变动的日期,accumulation用来存储上次计算利息后直到最近一次余额变动时余额按日累加的值,accumulate成员函数用来计算截止至指定日期的账户余额按日累计值。这样,当余额变动时,需要做的是将变动前的余额与该余额所持续的天数相乘,累加得到accumulation中,再修改lastDate。

  account.h、account.cpp:

#pragma once
#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
#include"date.h"         //引入date类
#include<string>
class SavingsAccount     //定义银行账户类
{
private:
	std::string id;     //由于开头没有用using namespace,因此这里要用std标准库
	double balance;     //余额
	double rate;        //利率
	Date lastDate;      //上一次变动日期
	double accumulation;     //累计余额
	static double total;     //所有账户累计余额
	void record(const Date& date, double amount, const std::string& desc);   /*记录一笔账,date为日期,amount为金额,desc为说明*/
	void error(const std::string& msg) const;   //报告错误信息
	double accumulate(const Date& date) const   //获得到指定日期为止的存款金额按日累计值
	{
		return accumulation + balance * date.distance(lastDate);  
	}
public:
	SavingsAccount(const Date& date, const std::string& id, double rate);   //构造函数
	const std::string& getId() const { return id; }   //获取ID
	double getBalance() const { return balance; }     //获取余额
	double getRate() const { return rate; }           //获取利率
	static double getTotal() { return total; }        //获取总余额
	void deposit(const Date& date, double amount, const std::string& desc);  //存款函数
	void withdraw(const Date& date, double amount, const std::string& desc);  //取款函数
	void settle(const Date& date);      //结算利率
	void show() const;     //显示账户信息
};
#endif
#include"account.h"    //引入account类
#include<cmath>
#include<iostream>
using namespace std;
double SavingsAccount::total = 0;     //初始化total为0
SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) :
	id(id), balance(0), rate(rate), lastDate(date), accumulation(0)   //构造函数的具体实现
{ 
	date.show();
	cout << "\t#" << id << " created" << endl;     
}
void SavingsAccount::record(const Date& date, double amount, const string& desc)
{    //记账函数的实现
	accumulation = accumulate(date);
	lastDate = date;
	amount = floor(amount * 100 + 0.5) / 100;  //通过floor函数进行保留小数点两位的四舍五入
	balance += amount;
	total += amount;
	date.show();
	cout << "\t#" << id << "\t" << amount << balance << "\t" << desc << endl;
}
void SavingsAccount::error(const string& msg) const    //报告错误函数
{
	cout << "Error(#" << id << "):" << msg << endl;
}
void SavingsAccount::deposit(const Date& date, double amount, const string& desc)
{
	record(date, amount, desc);        //存款函数
}
void SavingsAccount::withdraw(const Date& date, double amount, const string& desc)
{    //取款函数
	if (amount > getBalance())      //先将取款金额与余额进行比较
		error("not enough money!");
	else
		record(date, -amount, desc);    //根据比较输出不同结果
}
void SavingsAccount::settle(const Date& date)     //利息结算
{
	double interest = accumulate(date) * rate / date.distance(Date(date.getYear() - 1, 1, 1));
	if (interest != 0)
		record(date, interest, "interest");
	accumulation = 0;
}
void SavingsAccount::show() const      //输出账户信息
{
	cout << id << "\tBalance:" << balance;
}

 日期的表示:

   日期可以用一个类来表示,内含年、月、日三个数据成员。

   为了计算日期间相差天数,可以先选取一个比较规整的基准日期,在构造日期对象时将该日期到这个基准日期的相对天数计算出来,我们将这个相对天数称为“相对日期”。这样在计算两个日期相差的天数时,只需将两者的相对日期相减即可。我们将公元元年1月1日作为公共日期的基准日期,将y年m月d日相距这一天的天数记为f(y/m/d,1/1/1),可以将其分解为3部分:

f(y/m/d)=f(y/1/1,1/1/1)+f(y/m/1,y/1/1)+f(y/m/d,y/m/1)

  上面等式右边的第一项表示当年的1月1日与公元元年1月1日相距的天数,即公元元年到公元(y-1)年的总天数。平年每年有365天,闰年多一天,因此该值为365(y-1)加上公元元年到(y-1)年之间的闰年数,由于4年一闰,100的倍数免闰,400的倍数再闰,故有:

f(y/1/1,1/1/1)=365(y-1)+\left \lfloor (y-1)/4 \right \rfloor-\left \lfloor (y-1)/100 \right \rfloor+\left \lfloor (y-1)/400 \right \rfloor

   其中\left \lfloor x \right \rfloor表示对x向下取整。f(y/m/1,y/1/1)表示y年的m月1日与1月1日相距的天数。由于每月的天数不同,因此这难以表示为一个统一的公式。好在各平年中指定月份的1日与1月1日的相差天数可以由月份m唯一确定,因此可以把每月1日到1月1日的天数放在一个数组中,计算时只要查询该数组,便可得到f(y/m/1,y/1/1)的值。而对于闰年,仍可通过数组查询,只需在m>2时将查得的值加1.该值只依赖于x和y,将它记为g(x,y)。此外:

f(y/m/d,y/m/1)=d-1

   如果把公元元年1月1日的相对日期定为1,那么公元y年m月d日的相对日期就是:

f(y/m/d,1/1/1)+1=365(y-1)+\left \lfloor (y-1)/4 \right \rfloor-\left \lfloor (y-1)/100 \right \rfloor+\left \lfloor (y-1)/400 \right \rfloor+g(m,y)+d

   相对日期得出后,计算两日期相差天数的难题就迎刃而解了。

Date.h:

#ifndef __DATE_H__
#define __DATE_H__
class Date      //日期类
{
private:
	int year;    //年
	int month;   //月
	int 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; }
	int getMaxDay() const;       //获得当月有多少天
	bool isLeapYear() const      //判断当年是否为闰年
	{
		return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
	}
	void show() const;      //输出当前日期
	int distance(const Date& date) const   //计算两个日期之间是多少天
	{
		return totalDays - date.totalDays;
	}
};

#endif //__DATE_H__

Date.cpp:

#include"date.h"
#include<iostream>
#include<cstdlib>
using namespace std;
namespace    //namespace使下面的定义只在当前文件中有效
{   //存储平年中的某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项
	const int DAYS_BEFORE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
}
Date::Date(int year, int month, int day) :year(year), month(month), day(day)
{
	if (day <= 0 || day > getMaxDay())
	{
		cout << "Invalid date:";
		show();
		cout << endl;
		exit(1);
	}
	int years = year - 1;
	totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day;
	if (isLeapYear() && month > 2) totalDays++;
}
int Date::getMaxDay() const
{
		if (isLeapYear() && month == 2)
			return 29;
		else
			return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
}
void Date::show()const
{
		cout << getYear() << "-" << getMonth() << "-" << getDay();
}

main.cpp:

#include<iostream>
#include"account.h"
using namespace std;
 
int main()
{
	Date date(2008, 11, 1);       //起始日期
	SavingsAccount accounts[] =
	{
		SavingsAccount(date,"03755217",0.015),
	SavingsAccount(date, "02342342", 0.015)
	};
	const int n = sizeof(accounts) / sizeof(SavingsAccount);    //账户总数
    //11月份的几笔账目
	accounts[0].deposit(Date(2008, 11, 5), 5000, "salary");
	accounts[1].deposit(Date(2008, 11, 25), 10000, "sell stock 0323");
    //12月份的几笔账目
	accounts[0].deposit(Date(2008, 12, 5), 5500, "salary");
	accounts[1].withdraw(Date(2008, 12, 20), 4000, "buy a laptop");
    //结算所有账户并输出各个账户信息
	cout << endl;
	for (int i = 0; i < n; i++)
	{
		accounts[i].settle(Date(2009, 1, 1));
		accounts[i].show();
		cout << endl;
	}
	cout << "Total:" << SavingsAccount::getTotal() << endl;
	return 0;
}

运行结果:

  • 42
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是一个简单的银行账户管理程序的 C++ 代码,包括添加账户、存款、取款和查询余额等功能: ```c++ #include <iostream> #include <string> #include <vector> using namespace std; // 定义账户类 class Account { public: string name; // 账户名 int balance; // 账户余额 // 构造函数 Account(string n, int b) { name = n; balance = b; } // 存款 void deposit(int amount) { balance += amount; } // 取款 void withdraw(int amount) { if (balance >= amount) { balance -= amount; } else { cout << "余额不足,无法取款!" << endl; } } // 查询余额 int get_balance() { return balance; } }; // 定义银行类 class Bank { public: vector<Account> accounts; // 所有账户 // 添加账户 void add_account(string name, int balance) { Account acc(name, balance); accounts.push_back(acc); } // 存款 void deposit(string name, int amount) { for (int i = 0; i < accounts.size(); i++) { if (accounts[i].name == name) { accounts[i].deposit(amount); cout << "存款成功,当前余额为:" << accounts[i].get_balance() << endl; return; } } cout << "账户不存在!" << endl; } // 取款 void withdraw(string name, int amount) { for (int i = 0; i < accounts.size(); i++) { if (accounts[i].name == name) { accounts[i].withdraw(amount); cout << "取款成功,当前余额为:" << accounts[i].get_balance() << endl; return; } } cout << "账户不存在!" << endl; } // 查询余额 void get_balance(string name) { for (int i = 0; i < accounts.size(); i++) { if (accounts[i].name == name) { cout << "当前余额为:" << accounts[i].get_balance() << endl; return; } } cout << "账户不存在!" << endl; } }; int main() { Bank bank; // 创建银行对象 // 添加账户 bank.add_account("张三", 1000); bank.add_account("李四", 2000); bank.add_account("王五", 3000); // 存款 bank.deposit("张三", 500); bank.deposit("李四", 1000); bank.deposit("王五", 1500); // 取款 bank.withdraw("张三", 200); bank.withdraw("李四", 500); bank.withdraw("王五", 1000); // 查询余额 bank.get_balance("张三"); bank.get_balance("李四"); bank.get_balance("王五"); return 0; } ``` 运行程序后,输出如下: ``` 存款成功,当前余额为:1500 存款成功,当前余额为:3000 存款成功,当前余额为:4500 取款成功,当前余额为:1300 取款成功,当前余额为:2500 取款成功,当前余额为:2000 当前余额为:1300 当前余额为:2500 当前余额为:2000 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

风雪心

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

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

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

打赏作者

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

抵扣说明:

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

余额充值