目录
【任务介绍】
【任务目标】
【任务分析】见具体任务介绍
【任务实现】
【实验结果(包括输入数据和输出结果)】
【任务介绍】
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对各操作进行测试,并对产生多种异常进行分别处理,输出各自的异常信息。
【任务目标】
- 进步掌握具体子类重写(覆盖、实现)抽象父类中的方法。
- 独立完成“银行抽象类的设计和自定义异常类的”的源代码编写、编译及运行。
- 掌握自定义异常类的应用。
【任务分析】见具体任务介绍
【任务实现】
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());
}
}
}