Java面向对象实验三

1、写一个名为 Account 的类模拟账户。

该类的属性和方法如下图所示。

该类包括的属性: 账号 id,余额 balance,年利率 annualInterestRate;

包含的方法:访问器方法(getter 和 setter 方法),返回月利率的方法 getMonthlyInterest(),取款方法 withdraw(),存款方法 deposit()。

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

再使用 withdraw 方法提款 2500 元,使用 deposit 方法存款 3000 元,然后打印余额和月利率。

public class AccountTest {
	public static void main(String[] args) {
		Account account=new Account(1122,20000,4.5);
		account.withdraw(30000);
		System.out.println("您的账户余额为:"+account.getBalance());
		account.withdraw(2500);
		account.deposit(3000);
		System.out.println("您的账户余额为:"+account.getBalance()+"  月利率为:"+account.getMonthlyInterest()+"%");
	}
}

class Account {
	private int id;//账号
	private double balance;//余额
	private double annulInterestRate;//年利率
	public Account(int id,double banlance,double annualInterestRate) {
		super();
		this.id=id;
		this.balance=banlance;
		this.annulInterestRate=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 getAnnulInterestRate() {
		return annulInterestRate;
	}
	public void setAnnulInterestRate(double annulInterestRate) {
		this.annulInterestRate = annulInterestRate;
	}
	/**
	 * 返回月利率
	 */
	public double getMonthlyInterest() {
		return annulInterestRate/12;
	}
	/**
	 * 取钱
	 * @param amount
	 */
	public void withdraw(double amount) {
		if(balance>amount) {
			balance-=amount;
		}else {
			System.out.println("余额不足!回去努力赚钱吧");
		}
	}
	/**
	 * 存钱
	 * @param amount
	 */
	public void deposit(double amount) {
		balance+=amount;
	}
	
	
	
}

运行结果如下

2、创建 Account 类的一个子类 CheckAccount 代表可透支的账户,该账户中定义一个属性 overdraft 代表可透支限额。在 CheckAccount 类中重写 withdraw 方法,其算法如下:

要求:写一个用户程序测试 CheckAccount 类。在用户程序中,创建一个账号为 1122、余 额为 20000、年利率 4.5%,可透支限额为 5000 元的 CheckAccount 对象。

使用 withdraw 方法提款 5000 元,并打印账户余额和可透支额。

再使用 withdraw 方法提款 18000 元,并打印账户余额和可透支额。

再使用 withdraw 方法提款 3000 元,并打印账户余额和可透支额。

提示: (1) 子类 CheckAccount 的构造方法需要将从父类继承的 3 个属性和子类自己的属性全 部初始化。

(2) 父类Account的属性balance被设置为private,但在子类CheckAccount的withdraw 方法中需要修改它的值,因此应修改父类的 balance 属性,定义其为 protected。

public class CheckAccountTest {
	public static void main(String[] args) {
		CheckAccount check=new CheckAccount(1122, 20000, 4.5, 5000);
		check.withdraw(5000);
		System.out.println("您的余额为:"+check.getBalance()+",您的透支额度还剩:"+check.getOverDraft());
		check.withdraw(18000);
		System.out.println("您的余额为:"+check.getBalance()+",您的透支额度还剩:"+check.getOverDraft());
		check.withdraw(3000);
		System.out.println("您的余额为:"+check.getBalance()+",您的透支额度还剩:"+check.getOverDraft());
	}
}


class CheckAccount extends Account{
	private int overDraft;
	public CheckAccount(int id,double balance,double annualInterestRate,int overDraft) {
		super(id,balance,annualInterestRate);
		this.overDraft=overDraft;
	}
	
	public int getOverDraft() {
		return overDraft;
	}

	public void setOverDraft(int 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);
			 
		 }else {
			 System.out.println("超过可透支限额");
		 }
	}
}

运行结果如下

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值