类的继承、super

写一个名为Account的类模拟用户账号啊,该类的属性:账号、余额、年利率;方法:访问器方法(getter和setter方法)、withdraw()、deposit()。

package lianxi;         
public class Account {
	private int id;//账号
	private double balance;//余额
	private double annuallnterstRate;//年利率
	public Account() {
		super();
	}
	public Account(int id, double balance, double annuallnterstRate) {
		super();
		this.id = id;
		this.balance = balance;
		this.annuallnterstRate = annuallnterstRate;
	}
	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 getAnnuallnterstRate() {
		return annuallnterstRate;
	}
	public void setAnnuallnterstRate(double annuallnterstRate) {
		this.annuallnterstRate = annuallnterstRate;
	}
	public double getMonthlylnterest() {
	
		return annuallnterstRate/12;
	}
	//取钱
	public void withdraw(double amount) {
		if(balance>=amount) {
			balance-=amount;
			System.out.println("取款成功1");
		}
		System.out.println("余额不足");
	}
	//存钱
	public void deposit(double amount) {
		if(amount>0) {
			balance+=amount;
			System.out.println("存钱成功1" );
		}
		System.out.print("存钱失败");
	}         	
}

在写一个CheckAccount()的子类继承Account父类,该类的属性:额度;方法:访问器方法(getter和setter方法)、withdraw()、deposit()。

package lianxi;

public class CheckAccount extends Account {
	private double overdraft;

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

	public double getOverdraft() {
		return overdraft;
	}

	public void setOverdraft(double overdraft) {
		this.overdraft = overdraft;
	}
	// 取钱
	@Override
	public void withdraw(double amount) {
		if (getBalance() >= amount) {
			super.withdraw(amount);
		}else if(overdraft>=amount-getBalance()){
			
			overdraft-=(amount-getBalance());
			setBalance(0);
			System.out.println("取款成功2");
		}else {
			System.out.println("超过可透支限额!");
		}
		
	}
	@Override
	public void deposit(double amount) {
		if(getBalance() >=0&&getOverdraft()==20000&&amount>0) {
			super.deposit(amount);
		}else if(overdraft >0&&overdraft<20000&&amount>0){
			if(amount>(20000-getOverdraft())) {
				setBalance(amount-(20000-overdraft));
				overdraft=20000;
			    System.out.println("存钱成功2" );
			}else if(amount<(20000-getOverdraft())) {
				overdraft+=amount;
				System.out.println("存钱成功3" );
			}
			
		}
		//System.out.print("存钱失败");
	}         

}

最后是测试类。创建一个账号为33、余额为30000、利率为3.6%,可透支额度为20000`。
1、打印出月利率
2、使用withdraw方法取款60000,打印账户余额和可透支余额。
3、使用withdraw方法取款25000,打印账户余额和可透支余额。
4、使用withdraw方法取款8000,打印账户余额和可透支余额。
5、使用deposit方法存款60,打印账户余额和可透支余额。
6、使用deposit方法存款60000,打印账户余额和可透支余额。
7、使用deposit方法存款6,打印账户余额和可透支余额。

package lianxi;

import java.util.Scanner;

public class AccountTest {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		

		CheckAccount test=new CheckAccount(33,30000,0.045,20000) ;
		System.out.println("您的账户余额为:"+test.getBalance());
		System.out.println("月利率:"+(test.getMonthlylnterest())*100+"%");
		test.withdraw(60000);
		System.out.println("您的账户余额为:"+test.getBalance());
		System.out.println("您的可透支额度:"+test.getOverdraft());
		test.withdraw(25000);
		System.out.println("您的账户余额为:"+test.getBalance());
		System.out.println("您的可透支额度:"+test.getOverdraft());
		test.withdraw(8000);
		System.out.println("您的账户余额为:"+test.getBalance());
		System.out.println("您的可透支额度:"+test.getOverdraft());
		test.deposit(60);
		System.out.println("您的账户余额为:"+test.getBalance());
		System.out.println("您的可透支额度:"+test.getOverdraft());
		test.deposit(60000);
		System.out.println("您的账户余额为:"+test.getBalance());
		System.out.println("您的可透支额度:"+test.getOverdraft());
		test.deposit(6);
		System.out.println("您的账户余额为:"+test.getBalance());
		System.out.println("您的可透支额度:"+test.getOverdraft());
		
	}

}

在这里插入图片描述
`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值