通过银行账户的例子了解继承!

题述1
题述2
Account.java

package exercise;

public class Account {
	protected int id;//账号
	protected double balance;//余额
	protected double annualInterestRate;//年利率
	public Account(int id, double balance, double annualInterestRate) {
		super();
		this.id = id;
		this.balance = balance;
		this.annualInterestRate = annualInterestRate;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	public double getAnnualInterestRate() {
		return annualInterestRate;
	}
	public void setAnnualInterestRate(double annualInterestRate) {
		this.annualInterestRate = annualInterestRate;
	}
	//返回月利率
	public double getMonthlyInterestRate() {
		return annualInterestRate/12;
	} 
	//取款
	public void withdraw(double amount) {
		if(balance>amount) {
			balance -= amount;
		}
		else {
			System.out.println("余额不足!");
		}
	}
	//存钱
	public void depisit(double amount) {
		balance += amount;
	}
}

TestAccount.java

package exercise;

public class TestAccount {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//创建一个账户 ,id=1122,balance=20000,年利率=0.045
		Account acc = new Account(1122,20000,0.045);
		acc.withdraw(30000);
		System.out.println(acc.getBalance());
		acc.withdraw(2500);//取2500元
		acc.depisit(3000);//存3000元
		//打印余额和月利率
		System.out.println("当前余额为:"+acc.getBalance()
		+"月利率为:"+acc.getMonthlyInterestRate());
		
 	}

}

CheckAccount.java

package exercise;

public class CheckAccount extends Account{
	private double overdraft;
	public CheckAccount(int id, double balance, double annualInterestRate, double overdraft) {
		super(id, balance, annualInterestRate);
		this.overdraft = overdraft;
	}
	public double getOverdraft() {
		return overdraft;
	}
	public void setOverdraft(double overdraft) {
		this.overdraft = overdraft;
	}
	public void withdraw(double amount) {
			if(amount<=balance) {
				balance -=amount;
			}
			else if(overdraft >= amount - balance) {
				overdraft -= (amount - balance);//这一步能不能与下一步调换?不能,因为换之后等式右边amount减去的就是0了
				balance = 0;
			}
			else{
				System.out.println("超过可透支的限额!");
			}
		}
	}


TestCheckAccount.java

package exercise;

public class TestCheckAccount {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CheckAccount acc1 = new CheckAccount(1122,20000,0.045,5000);
		acc1.withdraw(5000);
		System.out.println("账户余额为:"+acc1.getBalance()+"\t"+"可透支额度为:"
		+acc1.getOverdraft());
		acc1.withdraw(18000);
		System.out.println("账户余额为:"+acc1.getBalance()+"\t"+"可透支额度为:"
				+acc1.getOverdraft());
		acc1.withdraw(3000);
		System.out.println("账户余额为:"+acc1.getBalance()+"\t"+"可透支额度为:"
				+acc1.getOverdraft());
	}
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值