个人银行管理系统2(C改Java)

  • C语言版本
#include "account.h"
#include <iostream>
using namespace std;

int main() {
	//建立几个账户
	SavingsAccount sa0(1, 21325302, 0.015);
	SavingsAccount sa1(1, 58320212, 0.015);

	//几笔账目
	sa0.deposit(5, 5000);
	sa1.deposit(25, 10000);
	sa0.deposit(45, 5500);
	sa1.withdraw(60, 4000);

	//开户后第90天到了银行的计息日,结算所有账户的年息
	sa0.settle(90);
	sa1.settle(90);

	//输出各个账户信息
	sa0.show();	cout << endl;
	sa1.show();	cout << endl;
	cout << "Total: " << SavingsAccount::getTotal() << endl;
	return 0;
}


//account.cpp
#include "account.h"
#include <cmath>
#include <iostream>
using namespace std;

double SavingsAccount::total = 0;

//SavingsAccount类相关成员函数的实现
SavingsAccount::SavingsAccount(int date, int id, double rate)
	: id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
	cout << date << "\t#" << id << " is created" << endl;
}

void SavingsAccount::record(int date, double amount) {
	accumulation = accumulate(date);
	lastDate = date;
	amount = floor(amount * 100 + 0.5) / 100;	//保留小数点后两位
	balance += amount;
	total += amount;
	cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
}

void SavingsAccount::deposit(int date, double amount) {
	record(date, amount);
}

void SavingsAccount::withdraw(int date, double amount) {
	if (amount > getBalance())
		cout << "Error: not enough money" << endl;
	else
		record(date, -amount);
}

void SavingsAccount::settle(int date) {
	double interest = accumulate(date) * rate / 365;	//计算年息
	if (interest != 0)
		record(date, interest);
	accumulation = 0;
}

void SavingsAccount::show() const {
	cout << "#" << id << "\tBalance: " << balance;
}

  • Java版本
    在这里插入图片描述
package Bank5_11;

public class SavingsAccount {
	private int id;//账号
	private double balance;//余额
	private double rate;//存款的年利率
	private int lastDate;//上次变更余款的时期
	private double accumulation;//余额按日累计之和
	//记录一笔账,date为日期,amount为金额
	private void record(int date,double amount) {
		accumulation = this.accumulate(date);
		lastDate = date;
		balance += amount;
		System.out.println(date + "  " + id + "  " + amount + "  " + balance);
	}
	//获得到指定日期为止的存款金额按日累计值
	private double accumulate(int date) {
		return accumulation + balance *(date - lastDate);
	}
	//构造函数
	public SavingsAccount(int date,int id,double rate) {
		this.lastDate = date;
		this.id = id;
		this.rate = rate;
		balance = 0;
		accumulation = 0;
		System.out.println(date + "  " + id + "  is created" );
	}
	//存入现金
	public void deposit(int date,double amount) {
		this.record(date,amount);
	}
	//取出现金
	public void withdraw(int date,double amount) {
		if(amount > balance) {
			System.out.println("Error: not enough money");
		}
		else {
			this.record(date, -amount);
		}
	}
	//结算利息,每年1月1日调用一次该函数
	public void settle(int date){
		double interest = this.accumulate(date)*rate/365;//计算年息
		if(interest != 0) {
			this.record(date, interest);
		}
		accumulation = 0;
	}
	//显示账户信息
	public void show() {
		System.out.println("id:" + id + " Balance:" + balance);
	}
	
}


package Bank5_11;

public class Run {
	public static void main(String[] args) {
		SavingsAccount sa0 = new SavingsAccount(1,21325302,0.015);
		SavingsAccount sa1 = new SavingsAccount(1,58320212,0.015);
		sa0.deposit(5, 5000);
		sa1.deposit(25, 6000);
		sa0.deposit(45, 5500);
		sa1.withdraw(60, 4000);
		sa0.settle(90);
		sa1.settle(90);
		sa0.show();
		sa1.show();
	}
}
  • 总结反思

在C语言中,可以把一个程序分成类定义头文件(.h)和类实现文件(.cpp),还有主函数文件。这种思路在JAVA里的体现是在同一个包里建立两个类。

本题中正是利用了这种思路,在同一个Bank5_11包中建立了两个类,Run和SavingsAccount,这两个类之间可以通过相互调用来实现对应的功能。

在同一个包中按照功能的不同建立不同的类,使得程序可读性大大增加,程序看起来简洁明了,需要进行修改时也能够较快的确定位置。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值