继承和super练习题

这篇博客介绍了如何使用Java编程实现一个银行账户类`Account`,包括设置和获取账户ID、余额和年利率的方法。此外,还创建了一个`CheckAccount`子类,增加了可透支功能。在`CheckAccount`类中,重写了`withdraw`方法以处理透支情况,并提供了获取和设置透支限额的方法。在测试类中展示了账户透支和存款、取款的示例。
摘要由CSDN通过智能技术生成

 

package exer2;

public class Account {
	private int id;//账号
	private double balance;//余额
	private double annualInterestRate;//构造器

	public Account(int id,double balance,double annualInterstRate) {
		this.id = id;
		this.balance = balance;
		this.annualInterestRate = annualInterstRate;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getId() {
		return 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 getMonthlyInsert() {
		return annualInterestRate/12;
	}
	public void withdraw(double amount) {  //取款
		if(balance >= amount) {
			balance -= amount;
			return;
		}
		System.out.println("余额不足");
	}
	public void deposit(double amount) {   //存款
		if(amount > 0) {
			balance += amount;
		}
	}

}
package exer2;

public class AccountTest {
	public static void main(String[] args) {
		Account a = new Account(1122,20000,0.045);
		a.withdraw(30000);
		System.out.println("您的账户余额为:"+a.getBalance());

		a.withdraw(2500);
		System.out.println("您的账户余额为:"+a.getBalance());
		a.deposit(3000);
		System.out.println("您的账户余额为:"+a.getBalance());

		System.out.println("月利率为:"+(a.getMonthlyInsert()*100)+"%");

	}
}

package exer2;

public class CheckAccount extends Account {
	private double overdraft; // 可透支限额

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

	@Override
	public void withdraw(double amount) {
		if(getBalance() >= amount) {  //余额足够消费
			//方法一
			//			setBalance(getBalance() - amount);
			//方法二
			super.withdraw(amount);
		}else if(overdraft >= (amount - getBalance())){   //透支额度 + 余额足够消费
			overdraft -= (amount - getBalance());
			//			 setBalance(0);
			//或
			super.withdraw(getBalance());
		}
		else {
			System.out.println("超过可透支限额!");
		}
	}

	public double getOverdraft() {
		return overdraft;
	}

	public void setOverdraft(double overdraft) {
		this.overdraft = overdraft;
	}

}

 

package exer2;

public class CheckAccountTest {
	public static void main(String[] args) {
		CheckAccount acct = new CheckAccount(1122, 20000, 0.045, 5000);
		acct.withdraw(5000);
		System.out.println("您的账户余额为:"+acct.getBalance());
		System.out.println("您的可透支额度为:"+acct.getOverdraft());
		acct.withdraw(18000);
		System.out.println("您的账户余额为:"+acct.getBalance());
		System.out.println("您的可透支额度为:"+acct.getOverdraft());
		acct.withdraw(3000);
		System.out.println("您的账户余额为:"+acct.getBalance());
		System.out.println("您的可透支额度为:"+acct.getOverdraft());
	} 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

elk-zhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值