how2j上一道异常类的综合小练习

异常类的小练习

  • 异常类综合练习

how2j异常练习综合

异常类综合练习

练习一:没有透支额度
Account类: 银行账号
属性: balance 余额
方法: getBalance() 获取余额
方法: deposit() 存钱
方法: withdraw() 取钱
OverdraftException: 透支异常,继承Exception
属性: deficit 透支额
how2j上一道异常类的综合小练习_第1张图片
练习二:有透支额度
类: CheckingAccount 支票账户,具备透支额度,继承Account
属性:overdraftProtection 透支额度
how2j上一道异常类的综合小练习_第2张图片
理解类图和整个流程,写下代码

OverdraftException.java

package YIChangTest1;

//透支异常
public class OverdraftException extends Exception{
 
	public static double deficit;//透支额度
	
	//无参构造函数
	public OverdraftException() {
		
	}
	
	public OverdraftException (String msg) {
		super(msg);
	}
	
	//构造有参函数
	public OverdraftException(String msg, double deficit) {
		
	}
	
	//获取到透支额
	public static double getDeficit() {
		return deficit;
	}
}

Account.java

package YIChangTest1;

//银行账号
public class Account {

	public static double balance;//余额
	double deficit;//透支额
	
	//构造函数,无参的
	public Account() {
		
	}
	
	//构造函数,传入一个参数的,初始化余额
	public Account (double balance) {
		this.balance = balance;
		if (balance < 0) {
			System.out.println("存入不能为负值,请重新输入");
			return;
		}
		System.out.println("账户初始存入" + balance + "元");
		
	}
	
	//获取余额
	public void getBalance() {
		System.out.println("查看余额为:" + balance);
	}
	
	//存钱
	public void deposit(double amt) {
		balance += amt;
		System.out.print("存入" + amt + "元!");
		System.out.println("账户余额:" + balance + "元");
	}
	
	//取钱,可能有异常
	public void withdraw(double amt) throws OverdraftException {
		balance -= amt;
		if (balance < 0) {
			throw new OverdraftException("余额不足!透支额度为 " + (amt - balance));
		}else {
			System.out.println("取钱成功:" + amt + "元!剩余:" + balance + "元");
		}
		
	}
	
	public static void main(String[] args) throws Exception {
		
		Account a = new Account(4000);
		a.deposit(1000);
		try {
			a.withdraw(2000);
			a.getBalance();
		} catch(OverdraftException e) {
			e.printStackTrace();
		}
	}
	
}

CheckingAccount.java

package YIChangTest1;

public class CheckingAccount extends Account {

	//相比Account多一个透支额度,可以在不超过透支额度的情况下透支
	private static double overdraftProtection;//透支额度
	
	//构造函数
	public CheckingAccount() {
		super();
	}
	
	public CheckingAccount(double balance) {
		super(balance);
	}
	
	//存钱,可以调用父类的方法
	public void deposit(double amt) {
		super.deposit(amt);
	}
	
	//获得账户余额,调用父类的方法
	public void getBalance() {
		
	}
	
	//重写父类方法
	//取钱方法和父类不一样,目前增加了透支额度
	public void withdraw(double amt) throws OverdraftException {
		
		//第一种,取出的不超过余额
		if (amt <= balance) {
			balance -= amt;
			System.out.println("取钱成功!取出" + amt + "元!账户余额为:" + balance + "元,透支额度为:" + overdraftProtection);
			return;//这里必须要返回,也可以用else if方法进行并列条件判断
		}
			
		//第二种,取出的超过余额,但不超过透支额
		if (amt > balance && amt <= (balance + overdraftProtection)) {
			overdraftProtection -= (amt - balance);
			System.out.println("取钱成功!取出" + amt + "元!账户余额为: 0元, 透支额度为:" + overdraftProtection);
			return;
		}
		
		
		//第三种,取出的超过余额,同时超过透支额
		if (amt > (balance + overdraftProtection)) {
			throw new OverdraftException("透支超额,超出:" + (amt - (balance + overdraftProtection)) + "元");
		}
		
	}
	
	public static void main(String[] args) {
		
		CheckingAccount ca = new CheckingAccount(5000);
		ca.overdraftProtection = 3000;//透支额度
		
		//存入
		ca.deposit(5000);
		
		//取出,抛出异常
		try {
			ca.withdraw(4000);
			ca.withdraw(4000);
			ca.withdraw(4000);
			ca.withdraw(4000);
		} catch(OverdraftException e) {
			e.printStackTrace();
		}
	}
	
}

你可能感兴趣的:(Java基础)