java基础:8.2 异常 编程练习

建立一个Account类: 银行账号
属性: balance 余额
方法: getBalance() 获取余额
方法: deposit() 存钱
方法: withdraw() 取钱
OverdraftException: 透支异常,继承Exception
属性: deficit 透支额

建立一个类: CheckingAccount 支票账户,具备透支额度,继承Account
属性:overdraftProtection 透支额度

package Bank;
public class Account {
	
	protected double balance;
	String name;
	
	//create account
	public Account() {
		this.balance = 0;
		name = "unknow ";
	}
	 
    public Account(String name,double balance) {
    	this.name = name;
        this.balance = balance;
    }
       
    // query balance 
	public double getBalance() {
		return balance;
	}
	
	public void printInformation() {
		System.out.println("User: " + name + "'s account balance is " + balance);
	}
	
	//deposit money
	public void deposit(double askMoney) {
		balance += askMoney
		System.out.println(name + " 存款成功 ! 余额:"+this.getBalance());
	}
	
	// withdraw money
	public void withdraw(double askMoney) throws OverdraftExceptions {
		if( balance < askMoney) {
			throw new OverdraftExceptions("取款失败,余额不足。 ",(balance-askMoney));
		}
		else{
			this.balance = balance - askMoney;
			System.out.println(name +" 取款成功 !余额:"+this.getBalance());
		}
	}
}
package Bank;
public class CheckingAccount extends Account{	
	protected double overdraftProtection;
	
	protected CheckingAccount() {
		super();
	}	  
    protected CheckingAccount(String name,double balance) {
    	super(name,balance);
    }
    
    public CheckingAccount(String name,double balance, double protect) {
        super(name,balance);
        overdraftProtection = protect;
    }
    
    //get Overdraft Protection .
    public double getOverdraftProtection() { //添加的方法获得透支额度
        return overdraftProtection;
    }
       
   @Override
    public void withdraw(double askMoney)  throws OverdraftExceptions {
    	if((balance-askMoney) >= 0) {
    		this.balance = balance - askMoney;
			System.out.println("取款成功 !余额:"+this.getBalance());
    	}
    	else 
    		if((balance-askMoney) >= -overdraftProtection) {
    			this.balance = balance - askMoney;
    			System.out.println("取款成功 !透支 " + this.getBalance() +" 剩余透支额: " + (overdraftProtection+balance));
    					
    		}
    		else
    			throw new OverdraftExceptions("取款失败,额度不足。",(balance-askMoney));
    }  
}
package Bank;
public class OverdraftExceptions extends Exception{	
	 double deficit;	 
	public OverdraftExceptions() {	         
	}		
	public OverdraftExceptions(String message, double deficit) {
		super(message);
		this.deficit = deficit;
	}	
}
package Bank;
public class Test {
	public static void main(String[] args)  {
		// TODO Auto-generated method stub
		Account u1 = new Account("Amy",1000);
		u1.printInformation();
		u1.deposit(200);
		u1.deposit(500);
	
		try {
		u1.withdraw(500);
		u1.withdraw(1000);
		u1.withdraw(500);
		}
		catch(OverdraftExceptions e) {
			e.printStackTrace();
		}
			
		CheckingAccount u2 = new CheckingAccount("bob",1000,2000);
		u2.printInformation();
		u2.deposit(400);
		u2.deposit(600);
		
		try {
		u2.withdraw(1900);
		u2.withdraw(500);
		u2.withdraw(900);
		u2.withdraw(1000);
		}
		catch(OverdraftExceptions e) {
			e.printStackTrace();
		}	
	}
}

运行结果
在这里插入图片描述

转载于:https://www.cnblogs.com/l20902/p/10610888.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值