Java 继承性 & super练习

Java 继承性 & super 练习

题目要求

1、写一个名为 Account 的类模拟账户。该类的属性和方法如下图所示。
该类包括的属性:账号 id,余额 balance,年利率 annualInterestRate;
包含的方法:访问器方法(getter 和setter 方法),
			返回月利率的方法 getMonthlyInterest(),
			取款方法 withdraw(),
			存款方法deposit()。

在这里插入图片描述

Account.java


public class Account {

	private int id;// 账号
	private double balance;// 余额
	private double annuallnterestRate;// 年利率

	public Account(int id, double balance, double annuallnterestRate) {
		super();
		this.id = id;
		this.balance = balance;
		this.annuallnterestRate = annuallnterestRate;
	}

	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 getAnnuallnterestRate() {
		return annuallnterestRate;
	}

	public void setAnnuallnterestRate(double annuallnterestRate) {
		this.annuallnterestRate = annuallnterestRate;
	}

	//返回月利率
	public double getMonthlyInterest() {
		return annuallnterestRate/12;
	}

	// 取款
	public void withdraw(double amount) {
		if (amount > balance || amount < 0) {
			System.out.println("余额不足");
			return;
		}
		balance -= amount;
	}

	// 存款
	public void deposit(double amount) {
		if (amount < 0) {
			System.out.println("您输入的存款金额有误");
			return;
		}
		balance += amount;
	}

}

写一个用户程序测试 Account 类。在用户程序中,
创建一个账号为 1122、余额为 20000、年利率 4.5%的 Account 对象。
使用 withdraw 方法提款 30000 元,并打印余额。
再使用 withdraw 方法提款 2500 元,并打印余额
使用 deposit 方法存款 3000 元,然后打印余额和月利率。

提示:在提款方法 withdraw 中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。

运行结果如图所示:

在这里插入图片描述
AccountTest.java

public class AccountTest {

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

		account.deposit(3000);
		System.out.println("您的帐户余额为:" + account.getBalance());
		
		System.out.println("月利率为:" + account.getMonthlyInterest());

	}
}

2.创建 Account 类的一个子类 CheckAccount 代表可透支的账户,
	该账户中定义一个属性overdraft 代表可透支限额。
	在 CheckAccount 类中重写 withdraw 方法,其算法如下:
	
	如果(取款金额<账户余额),
		可直接取款
	如果(取款金额>账户余额),
		计算需要透支的额度
		判断可透支额 overdraft 是否足够支付本次透支需要,如果可以
			将账户余额修改为 0,冲减可透支金额
		如果不可以
			提示用户超过可透支额的限额

CheckAccount.java

public class CheckAccount extends Account {

	private double overdraft;// 可透支限额

	public double getOverdraft() {
		return overdraft;
	}

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

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

	public void withdraw(double amount) {
		if (amount <= getBalance()) {// 余额足够
			super.withdraw(amount);
		} else if (amount - getBalance() <= overdraft) {// 透支额度+余额足够
			overdraft -= (amount - getBalance());
			setBalance(0);
		} else {// 透支限额不够
			System.out.println("超过可透支限额!");
		}

	}

}
写一个用户程序测试 CheckAccount 类。在用户程序中,
 * 创建一个账号为 1122、余额为 20000、年利率 4.5%,可透支限额为 5000 元的 CheckAccount 对象。
 * 使用 withdraw 方法提款 5000 元,并打印账户余额和可透支额。
 * 再使用 withdraw 方法提款 18000 元,并打印账户余额和可透支额。
 * 再使用 withdraw 方法提款 3000 元,并打印账户余额和可透支额。

 运行结果如下图:

在这里插入图片描述

CheckAccountTest.java

public class CheckAccountTest {

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

		
		checkAccount.withdraw(18000);
		System.out.println("您的帐户余额为:" + checkAccount.getBalance());
		System.out.println("您的可透支额度为:" + checkAccount.getOverdraft());

		checkAccount.withdraw(3000);
		System.out.println("您的帐户余额为:" + checkAccount.getBalance());
		System.out.println("您的可透支额度为:" + checkAccount.getOverdraft());

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值