ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java继承与super练习

2021-02-15 19:58:42  阅读:110  来源: 互联网

标签:annualInterestRate java double 练习 System balance super public out


Account

package 继承与super练习;

public class Account {
	private int id;//账号
	protected 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 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;
	}
	
	public void withdraw (double amount) {
		if(amount > 0 && amount <= balance) 
			balance -= amount;
		else if(amount > balance)
			System.out.println("余额不足!");
		System.out.println("您的帐户余额为:" + balance);
	}
	
	public double getMonthlyInterest() {
		return annualInterestRate/12;
	}
	
	public void deposit (double amount) {
		if(amount > 0) {
			balance += amount;
			System.out.println("您的余额为:" + balance);
			System.out.println("月利率为:" + getMonthlyInterest());
		}
	}
}

CheckAccount

package 继承与super练习;


public class CheckAccount extends Account{
	
	private double overdeaft;
	
	public CheckAccount() {
		super();
	}

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

	public double getOverdeaft() {
		return overdeaft;
	}

	public void setOverdeaft(double overdeaft) {
		this.overdeaft = overdeaft;
	}


	public void withdraw(double amount) {
		if(amount > 0 && amount < balance)//如果 取款金额<账户余额
			balance -= amount;
		
		if(amount > balance) {//如果 取款金额>账户余额
			 double overdeaft1 = amount - balance;//计算需要透支的额度
			 if(overdeaft > overdeaft1) {//判断可透支额 overdraft 是否足够支付本次透支需要
				 balance = 0;//如果可以将账户余额修改为 0,冲减可透支金额
				 overdeaft -= overdeaft1;
			 }
			 else if(overdeaft < overdeaft1)//如果不可以提示用户超过可透支额的限额
				 System.out.println("超过可透支限额!\n");
			 }
		
		}
		

	public static void main(String[] args) {
			CheckAccount user = new CheckAccount(1122, 20000, 0.045,5000);
			user.withdraw(5000);
			System.out.println("您的帐户余额为:" + user.getBalance());
			System.out.println("您的可透支额:" + user.getOverdeaft() + "\n");
			user.withdraw(18000);
			System.out.println("您的帐户余额为:" + user.getBalance());
			System.out.println("您的可透支额:" + user.getOverdeaft() + "\n");
			user.withdraw(3000);
			System.out.println("您的帐户余额为:" + user.getBalance());
			System.out.println("您的可透支额:" + user.getOverdeaft() + "\n");
	}
}

标签:annualInterestRate,java,double,练习,System,balance,super,public,out
来源: https://blog.csdn.net/qq_43758585/article/details/113818593

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有