个人银行账户管理系统(4~6)

个人银行账户管理系统(4~6)

代码4.9——类:
Java与C++的不同:

  1. Java里不允许普通函数(除抽象类,接口等)的声明与函数体分开,要写一起。
  2. c++ 里面void test (int _x) const{}时const指该函数不能修改类的成员,而Java 里面final void
  3. JAVA里的floor函数因为没有了头文件cmath,要用已经定义好的类Math去调用,即Math.floor()。
  4. JAVA里的floor函数因为没有了头文件cmath,要用已经定义好的类Math去调用,即Math.floor()。
package PersonalBank1;

public class SavingsAccount {
	private int id;				//账号
	private double balance;		//余额
	private double rate;		//存款的年利率
	private int lastDate;		//上次变更余额的时期
	private double accumulation;
	private void record(int date, double amount) {
		accumulation = accumulate(date);
		lastDate = date;
		amount = Math.floor(amount * 100 + 0.5) / 100;	//保留小数点后两位
		balance += amount;
		System.out.println(date+"\t#"+id+"\t"+amount+"\t"+balance);
	}
	private double accumulate(int date) {
		return accumulation + balance * (date - lastDate);
	}
	public SavingsAccount(int date, int id, double rate) {
		this.id = id;
		this.balance = 0;
		this.rate = rate;
		this.lastDate = date;
		this.accumulation = 0;
		System.out.println(date+"\t#"+id+" is created");
	}
	public int getId() {
		return id;
	}
	public double getBalance() {
		return balance;
	}
	public double getRate() {
		return rate;
	}
	public void deposit(int date , double amount) {
		record(date, amount);
	}
	public void withdraw(int date, double amount) {
		if (amount > getBalance ())
			System.out.println("Error: not enough money");
		else
			record(date, -amount);
	}
	public void settle(int date) {
		double interest = accumulate(date) * rate / 365;
		if (interest != 0)
			record(date, interest);
		accumulation = 0;
	}
	public void show() {
		System.out.println("#"+id+"\tBalance: "+balance);
	}

	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, 10000);
		sa0.deposit(45, 5500);
		sa1.withdraw(60, 4000);
		sa0.settle(90);
		sa1.settle(90);
		sa0.show();	
		System.out.println();
		sa1.show();	
		System.out.println();
	}

}

代码5_11——增添静态属性与方法:

  1. Java里面没有const修饰符,有类似的final,但两者功能有所差异。
  2. 由于C++里面可以把一个程序分成类定义头文件(.h)和类实现文件(.cpp),还有主函数文件。在JAVA里做的改动是在同一个包里建立两个类(这里是SavingsAccount5和SavingsAccount51),在SavingsAccount51中去调用SavingsAccount5。
  3. JAVA中的final与C++中const并不等价,final用在方法里还表示最终方法,不能被继承。
package PersonalBank2;

//import PersonalBank1.SavingsAccount;

public class SavingsAccount {

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

	private void record(int date, double amount) {
		accumulation = accumulate(date);
		lastDate = date;
		amount = Math.floor(amount * 100 + 0.5) / 100;	//保留小数点后两位
		balance += amount;
		total += amount;
		System.out.println(date+"\t#"+id+"\t"+amount+"\t"+balance);
	}
	
	private final double accumulate(int date) {
		return accumulation + balance * (date - lastDate);
	}

	public SavingsAccount(int date, int id, double rate) {
    	this.id = id;
		this.balance = 0;
		this.rate = rate;
		this.lastDate = date;
		this.accumulation = 0;
		System.out.println(date+"\t#"+id+" is created");
    }

	public final int getId() {
		return id;
	}

	public final double getBalance() {
		return balance;
	}

	public final double getRate() {
		return rate;
	}

	public static double getTotal() {
		return total;
	}

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

	public void withdraw(int date, double amount) {
		if (amount > getBalance())
			System.out.println("Error: not enough money");
		else
			record(date, -amount);
	}

	public void settle(int date) {
		double interest = accumulate(date) * rate / 365;
		if (interest != 0)
			record(date, interest);
		accumulation = 0;
	}

	public final void show() /* const */ {
		System.out.println("#" + id + "\tBalance: " + balance);
	};
	
	/*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, 10000);
		sa0.deposit(45, 5500);
		sa1.withdraw(60, 4000);
		sa0.settle(90);
		sa1.settle(90);
		sa0.show();	
		System.out.println();
		sa1.show();	
		System.out.println();
		System.out.println("Total: "+ getTotal());//static修饰的total该怎么办?
	}*/

}
package PersonalBank2;

public class Bank {

	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, 10000);
		sa0.deposit(45, 5500);
		sa1.withdraw(60, 4000);
		sa0.settle(90);
		sa1.settle(90);
		sa0.show();	
		System.out.println();
		sa1.show();	
		System.out.println();
		System.out.println("Total: "+ SavingsAccount.getTotal());//static修饰的total该怎么办?加个sa0 或 sa1,为什么?
		

	}

}


代码6_25——增添字符串、对象数组:

  1. 在使用Date对象引用作参数时,由于java没有引用类型的符号&,所以在作函数形参时直接使用Date对象。
  2. C++中的sizeof()函数用来计算对象所占内存空间大小,在java里没有。所以将n=sizeof(accounts)/sizeof(SavingsAccount)改成了accounts.length()。其中length()方法返回数组长度,实现的功能相同。
  3. 在使用Date对象引用作参数时,由于java没有引用类型的符号&,所以在作函数形参时直接使用Date对象。
package PersonalBank3;

public class Bank3 {

	public static void main(String[] args) {
		Date date1 = new Date(2008, 11 ,1);	//起始日期
		//建立几个账户
		
		SavingsAccount[] accounts =new SavingsAccount[]{
			new SavingsAccount(date1, "S3755217", 0.015),
			new SavingsAccount(date1, "02342342", 0.015)
		};
		final int n = accounts.length; //账户总数
		//11月份的几笔账目
		Date date2 = new Date(2008,11,5);
		Date date3 = new Date(2008,11,25);
		accounts[0].deposit(date2, 5000, "salary");
		accounts[1].deposit(date3, 10000, "sell stock 0323");
		//12月份的几笔账目
		Date date4 = new Date(2008,12,5);
		Date date5 = new Date(2008,12,20);
		accounts[0].deposit(date4, 5500, "salary");
		accounts[1].withdraw(date5, 4000, "buy a laptop");

		//结算所有账户并输出各个账户信息
		System.out.println();
		for (int i = 0; i < n; i++) {
			Date date6 = new Date(2009,1,1);
			accounts[i].settle(date6);
			accounts[i].show();
			System.out.println();
		}
		System.out.println("Total: "+SavingsAccount.getTotal());

	}

}
package PersonalBank3;

public class Date {
	final int[] DAYS_BEFORE_MONTH = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
	private int year;		//年
	private int month;		//月
	private int day;		//日
	private int totalDays;
	
	public Date(int year, int month, int day) {
		this.year = year;
		this.month = month;
		this.day = day;
		if (day <= 0 || day > getMaxDay()) {
			System.out.println("Invalid date: "); 
			show();
			System.out.println();
			System.exit(1);//exit()函数在Java中写作System.exit()
		}
		int years = year - 1;
		totalDays = years * 365 + years / 4 - years / 100 + years / 400
			+ DAYS_BEFORE_MONTH[month - 1] + day;
		if (isLeapYear() && month > 2) totalDays++;
	}
	public final int getYear() { return year; }
	public final int getMonth()  { return month; }
	public final int getDay() { return day; }
	public final int getMaxDay() {
		if (isLeapYear() && month == 2)
			return 29;
		else
			return DAYS_BEFORE_MONTH[month]- DAYS_BEFORE_MONTH[month - 1];
	}
	public final boolean isLeapYear() {	//bool在Java中写作boolean
		return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
	}
	public final void show() {
		System.out.print(getYear()+"-"+getMonth()+"-"+getDay());		
	}
	public final int distance(final Date date) {
		return totalDays - date.totalDays;
	}

	
}
package PersonalBank3;

public class SavingsAccount {
	
	private String id;
	private double balance;
	private double rate;		//存款的年利率
	private Date lastDate;		//上次变更余额的时期
	private double accumulation;	//余额按日累加之和
	private static double total = 0;
	
	private void record(final Date date, double amount, final String desc) {
		accumulation = accumulate(date);
		lastDate = date;
		amount = Math.floor(amount * 100 + 0.5) / 100;	//保留小数点后两位
		balance += amount;
		total += amount;
		date.show();
		System.out.println("\t#"+id+"\t"+amount+"\t"+balance+"\t"+desc);		
	}
	
	private final void error(final String msg) {
		System.out.println("Error(#"+id+"):"+msg);
	}
	
	private final double accumulate(final Date date)  {
		return accumulation + balance * date.distance(lastDate);
	}
	public SavingsAccount(final Date date, final String id, double rate) {		
			this.id = id;
			this.balance = 0;
			this.rate = rate;
			this.lastDate = date;
			this.accumulation = 0;
			date.show();
			System.out.println(date+"\t#"+id+" is created");
	}
	public final String getId()  { return id; }
	public final double getBalance()  { return balance; }
	public final double getRate()  { return rate; }
	public static double getTotal() { return total; }

	//存入现金
	public void deposit(final Date date, double amount, final String desc) {
		record(date, amount, desc);
	}
	//取出现金
	public void withdraw(final Date date, double amount, final String desc) {
		if (amount > getBalance())
		error("not enough money");
	else
		record(date, -amount, desc);//结算利息,每年1月1日调用一次该函数
	}
	
	public void settle(final Date date) {
		Date date0 = new Date(date.getYear() - 1, 1, 1);
		double interest = accumulate(date) * rate	//计算年息
			/ date.distance(date0);
		if (interest != 0)
			record(date, interest, "interest");
		accumulation = 0;//显示账户信息
	}	
	public final void show() {
		System.out.println(id+"\tBalance: "+balance);
	}	

}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值