Java实战——银行管理系统_4.9

银行管理系统

C++源码:

#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__

class SavingsAccount { //储蓄账户类
private:
	int id;				//账号
	double balance;		//余额
	double rate;		//存款的年利率
	int lastDate;		//上次变更余额的时期
	double accumulation;	//余额按日累加之和
	static double total;	//所有账户的总金额

	//记录一笔帐,date为日期,amount为金额,desc为说明
	void record(int date, double amount);
	//获得到指定日期为止的存款金额按日累积值
	double accumulate(int date) const {
		return accumulation + balance * (date - lastDate);
	}
public:
	//构造函数
	SavingsAccount(int date, int id, double rate);
	int getId() const { return id; }
	double getBalance() const { return balance; }
	double getRate() const { return rate; }
	static double getTotal() { return total; }

	//存入现金
	void deposit(int date, double amount);
	//取出现金
	void withdraw(int date, double amount);
	//结算利息,每年1月1日调用一次该函数
	void settle(int date);
	//显示账户信息
	void show() const;
};

#endif //__ACCOUNT_H__

Java:


public class SavingsAccount {
	
	private int id;//账号
	private int lastDate;//上次变更余额的时期
	private double balance;//余额
	private double rate;//存款的年利率
	private double accumulation;//余额按日累加之和
	//记录一笔帐,date为日期,amount为金额,desc为说明
	
	public double accumulate(int date) {
		return accumulation + balance * (date - lastDate);
	}
	
	public int getId(int id) {
		return id;
	}
	
	public double getBalance(double balance) {
		return balance;
	}
	
	public double getRate(double rate) {
		return rate;
	}//存入现金
	
	public int floor(double d) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public void record(int date,double amount) {
		accumulation = accumulate(date);
		lastDate = date;
		amount = Math.floor(amount * 100+0.5)/100;
		balance += amount;
		System.out.print(date+"\n"+id+"\n"+amount+"\n"+balance);
	}//获得到指定日期为止的存款金额按日累积值
	
	public void deposit(int date,double amount) {
		record(date,amount);
	}//存入现金

	public void withdraw(int date,double amount) {
		if (amount > balance)
			System.out.print( "Error: not enough money"+"\n");
		else
			record(date, -amount);
	}//取出现金
	
	public void settle(int date) {
		double interest = accumulate(date) * rate / 365;	//计算年息
		if (interest != 0)
			record(date, interest);
		accumulation = 0;
	}//结算利息,每年1月1日调用一次该函数
	
	public void show() {
		System.out.print("#"+id+"\tbalance: "+balance);
	}//显示账户信息
	
	public SavingsAccount(int date, int id, double rate) {
	    this.id = id;
	    this.balance = 0;
	    this.rate = rate;
	    this.lastDate = date;
	    this.accumulation = 0;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//建立几个账户
		SavingsAccount sa0= new SavingsAccount(1, 21325302, 0.015);
		SavingsAccount sa1= new SavingsAccount(1, 58320212, 0.015);
		
		//几笔账目
		sa0.deposit(5, 5000);
		sa1.deposit(25, 10000);
		sa0.deposit(45, 5500);
		sa1.withdraw(60, 4000);
		
		sa0.settle(90);//开户后第90天到了银行的计息日,结算所有账户的年息
		sa1.settle(90);//开户后第90天到了银行的计息日,结算所有账户的年息
		
		sa0.show();//输出各个账户信息
		sa1.show();//输出各个账户信息

		System.out.println();
	}

}

输出案例:
Java实战——银行管理系统_4.9_第1张图片

你可能感兴趣的:(Java实战——银行管理系统_4.9)