【实验内容-1】自定义异常类的应用

目录

【任务介绍】

【任务目标】

【任务分析】见具体任务介绍

【任务实现】

【实验结果(包括输入数据和输出结果)】


【任务介绍】

1.写一个抽象账户类(Account) 

包含属性:  id:账户号码  name:账户姓名  balance:账户余额

deposit: 存款方法,参数是double型的金额,要求不允许子类修改

withdraw取款方法,参数是double型的金额抽象方法,具体取款操作在两个子类中分别实现

包含的构造方法:无参数构造方法:用于子类调用

有参构造方法:用于设置必要的属性

2.写一个子类储蓄账户(SavingAccount)继承抽象类Account

实现抽象方法withdraw,储蓄账户不允许透支,在取款发生余额不足的情况下要产生BalanceNotEnoughException

3.写一个子类信用账户(CreditAccount)继承抽象类Account

增加属性 ceiling 透支额度可以透支,并允许设置透支额度.实现抽象方法withdraw,在取款发生超过透支额度的情况下要产生OverDraftNotEnoughException

4.写出异常类:        BalanceNotEnoughException :用于取钱的时候余额不足的异常情况(继承父类Exception,新增一个成员变量String message,写一个有参构造方法用于构造异常对象,构建异常信息message,然后再新增一个warnMessage方法,用于返回message.

OverDraftNotEnoughException:用于取钱时超过透支额度的异常情况(继承父类Exception新增一个成员变量String message,写一个有参构造方法用于构造异常对象,构建异常信息message,然后再新增一个warnMessage方法,用于返回message.

5.写一个测试类TestAccount对各操作进行测试,并对产生多种异常进行分别处理输出各自的异常信息

任务目标】

  1. 进步掌握具体子类重写(覆盖、实现)抽象父类中的方法
  2. 独立完成银行抽象类的设计和自定义异常类的”的源代码编写、编译及运行。
  3. 掌握自定义异常类的应用

任务分析】见具体任务介绍

【任务实现】

abstract class Account{
    private String id;
    private String name;
    private double balance;

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public final void deposit(double money){
        this.balance+=money;
    }

    public abstract void withdraw(double money)throws Exception;

    public Account(){

    }

    public Account(String id, String name, double balance) {
        this.id = id;
        this.name = name;
        this.balance = balance;
    }
}

class SavingAccount extends Account{
    public SavingAccount(String id, String name, double balance) {
        super(id, name, balance);
    }

    public void withdraw(double money) throws BalanceNotEnoughException{
        if(money>super.getBalance()){
            throw new BalanceNotEnoughException(money,super.getBalance());
        }
        super.setBalance(super.getBalance()-money);
    }

}

class CreditAccount extends Account{
    private double ceiling;

    public CreditAccount(String id, String name, double balance, double ceiling) {
        super(id, name, balance);
        this.ceiling = ceiling;
    }

    @Override
    public void withdraw(double money)throws OverDraftNotEnoughException {
        if(money>ceiling+super.getBalance()){
            throw new OverDraftNotEnoughException(money,ceiling,super.getBalance());
        }
        super.setBalance(super.getBalance()-money);
    }
}

class BalanceNotEnoughException extends Exception{
    private String message;

    public BalanceNotEnoughException(double money,double balance) {
        this.message="当前余额 "+balance+"元,"+"无法支取"+money+"元";
    }
    public String warnMessage(){
        return this.message;
    }
}

class OverDraftNotEnoughException extends Exception{
    private String message;

    public OverDraftNotEnoughException(double money,double ceiling,double balance) {
        this.message="当前余额"+balance+"元"+"透支额度"+ceiling+ "无法支取"+money+"元";
    }
    public String warnMessage(){
        return this.message;
    }
}




public class TestAccount{
    public static void main(String args[]) {
        SavingAccount savingAccount=new SavingAccount("0011988938","小王",1000);
        CreditAccount creditAccount=new CreditAccount("1248998989","小王",2000,1000);
        try{
            savingAccount.withdraw(500);
            savingAccount.withdraw(600);
        }
        catch(BalanceNotEnoughException e){
            System.out.println("取款过程中发生余额不足异常:");
            System.out.println(e.warnMessage());

        }
        try{
            creditAccount.withdraw(1000);
            creditAccount.withdraw(1500);
            creditAccount.deposit(200);
            creditAccount.withdraw(600);
            creditAccount.withdraw(200);
        }
        catch(OverDraftNotEnoughException e){
            System.out.println("取款过程中发现透支额度不足异常:");
            System.out.println(e.warnMessage());
        }
    }
}

【实验结果(包括输入数据和输出结果)】

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值