java继承与super练习

Account

package 继承与super练习;

public class Account {
	private int id;//账号
	protected double balance;//余额
	private double annualInterestRate;//年利率
	
	public Account() {
		
	}
	
	public Account (int id, double balance, double annualInterestRate ) {
		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 void withdraw (double amount) {
		if(amount > 0 && amount <= balance) 
			balance -= amount;
		else if(amount > balance)
			System.out.println("余额不足!");
		System.out.println("您的帐户余额为:" + balance);
	}
	
	public double getMonthlyInterest() {
		return annualInterestRate/12;
	}
	
	public void deposit (double amount) {
		if(amount > 0) {
			balance += amount;
			System.out.println("您的余额为:" + balance);
			System.out.println("月利率为:" + getMonthlyInterest());
		}
	}
}

CheckAccount

package 继承与super练习;


public class CheckAccount extends Account{
	
	private double overdeaft;
	
	public CheckAccount() {
		super();
	}

	public CheckAccount(int id, double balance, double annualInterestRate,double overdeaft) {
		super(id, balance, annualInterestRate); 
		this.overdeaft = overdeaft;
	}

	public double getOverdeaft() {
		return overdeaft;
	}

	public void setOverdeaft(double overdeaft) {
		this.overdeaft = overdeaft;
	}


	public void withdraw(double amount) {
		if(amount > 0 && amount < balance)//如果 取款金额<账户余额
			balance -= amount;
		
		if(amount > balance) {//如果 取款金额>账户余额
			 double overdeaft1 = amount - balance;//计算需要透支的额度
			 if(overdeaft > overdeaft1) {//判断可透支额 overdraft 是否足够支付本次透支需要
				 balance = 0;//如果可以将账户余额修改为 0,冲减可透支金额
				 overdeaft -= overdeaft1;
			 }
			 else if(overdeaft < overdeaft1)//如果不可以提示用户超过可透支额的限额
				 System.out.println("超过可透支限额!\n");
			 }
		
		}
		

	public static void main(String[] args) {
			CheckAccount user = new CheckAccount(1122, 20000, 0.045,5000);
			user.withdraw(5000);
			System.out.println("您的帐户余额为:" + user.getBalance());
			System.out.println("您的可透支额:" + user.getOverdeaft() + "\n");
			user.withdraw(18000);
			System.out.println("您的帐户余额为:" + user.getBalance());
			System.out.println("您的可透支额:" + user.getOverdeaft() + "\n");
			user.withdraw(3000);
			System.out.println("您的帐户余额为:" + user.getBalance());
			System.out.println("您的可透支额:" + user.getOverdeaft() + "\n");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值