MyJavaCode-6

继承与方法重写

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

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

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Account account = new Account(1122, 20000, 0.045);
		account.withdraw(3000);
		account.withdraw(2500);
		account.deposit(3000);
		System.out.println(account.getMonthlyInterest());
		CheckAccount checkAccount = new CheckAccount(1122, 20000, 0.045, 5000);
		checkAccount.withdraw(5000);
		checkAccount.withdraw(18000);
		checkAccount.withdraw(3000);
		
	}

}
//写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属
//性:账号id,余额balance,年利率annualInterestRate;包含的方法:访问器方
//法(getter和setter方法),返回月利率的方法getMonthlyInterest(),取款方
//法withdraw(),存款方法deposit()。
class Account {
	private int id;
	private 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 double getMonthlyInterest(){
		return annualInterestRate/12;
	}
	public void withdraw (double amount){
		if(amount <= 0){
			System.out.println("请重新输入!");
			return;
		}else if(amount > balance){
			System.out.println("余额不足");
			return;
		}
		balance -= amount;
		System.out.println("成功取出" + amount + " 余额为:" + balance);
	}
	public void deposit (double amount){
		if(amount <= 0){
			System.out.println("操作有误,请重试");
		}
		balance += amount;
		System.out.println("存款成功!" + " 余额为:" + balance);
	}

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

public class CheckAccount extends Account{
	double overdraft;
	public CheckAccount(int id, double balance, double annualInterestRate, double overdraft) {
		super(id, balance, annualInterestRate);
		this.overdraft = overdraft;
	}
	@Override
	public void withdraw(double amount) {
		if(amount <= 0){
			System.out.println("操作失败!");
			return;
		}
		if(amount <= this.getBalance()){
			double b = this.getBalance() - amount;
			this.setBalance(b);
			System.out.println("成功取出" + amount + "  余额为:" + this.getBalance() + "   可透支额度为:" + overdraft);
		}else{ 
			double b2 = amount - this.getBalance();
			if(b2 <= overdraft){
				this.setBalance(0);
				overdraft -= b2;
				System.out.println("成功取出" + amount + "  余额为:0   透支了:" + b2 + "   透支额度剩余:" + overdraft);
			}else{
				System.out.println("超过可透支额度,取款失败!");
			}
		}
	}
	
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值