h2j异常处理综合练习答案

本文介绍了如何在Java中实现银行账户类,包括基本的账户操作如存款和取款,以及处理透支情况的OverDraftException异常。通过CheckingAccount子类演示了透支保护功能。

package exception;
   
public class Account {
   
    protected double balance;
   
    public Account(double balance) {
        this.balance = balance;
    }
   
    public double getBalance() {
        return balance;
    }
       
    public void deposit(double amt){
        this.balance+=amt;
    }
    public void withdraw(double amt) throws OverDraftException{
        if(this.balance<amt)
            throw new OverDraftException("余额不足", amt-this.balance);
           
        this.balance-=amt;
    }
       
    public static void main(String[] args) {
        //开户存了1000
        Account a = new Account(1000);
        //存钱1000
        a.deposit(1000);
        //查看余额
        System.out.println(a.getBalance());
           
        try {
            //取2001
            a.withdraw(2001);
        } catch (OverDraftException e) {
            System.err.println("透支金额:"+e.getDeficit());
            e.printStackTrace();
        }
           
    }
       
}

package exception;
 
public class OverDraftException extends Exception{
 
    private double deficit;
 
    public double getDeficit() {
        return deficit;
    }
 
    public OverDraftException(String msg, double deficit) {
        super(msg);
        this.deficit = deficit;
    }
     
}

package exception;
 
public class CheckingAccount extends Account {
 
    private double overdraftProtection;
 
    public CheckingAccount(double balance) {
        super(balance);
    }
 
    public CheckingAccount(double balance, double overdraftProtection) {
        super(balance);
 
        this.overdraftProtection = overdraftProtection;
    }
 
    public void withdraw(double amt) throws OverDraftException {
        if (amt > this.balance + overdraftProtection) {
            double deficit = amt - (this.balance + overdraftProtection);
            throw new OverDraftException("透支额度超标", deficit);
        }
        this.balance -= amt;
    }
 
    public static void main(String[] args) {
        //开户存了1000块,拥有500的透支额度
        CheckingAccount a = new CheckingAccount(1000, 500);
        //存了1000
        a.deposit(1000);
        //查询余额
        System.out.println(a.getBalance());
        try {
            a.withdraw(600);
            System.out.println(a.getBalance());
            a.withdraw(600);
            System.out.println(a.getBalance());
            a.withdraw(600);
            System.out.println(a.getBalance());
            a.withdraw(600);
            System.out.println(a.getBalance());
            a.withdraw(600);
            System.out.println(a.getBalance());
        } catch (OverDraftException e) {
            System.err.println("透支超额:"+e.getDeficit());
            e.printStackTrace();
        }
 
    }
 
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值