Day08_13练习07

这里写图片描述

//编写异常类

class OverdraftException{ 

private double Deficit;  //表示所取的钱跟余额的差钱 

//共有访问方法
public OverdraftException(){
  super();
 }  

//message为继承Exception类中继承过来   
public OverdraftException(String message,double Deficit){
  super(message);
  this.Deficit = Deficit;
}  

public double getDeficit(){
    return Deficit; 
} 

}

//修改Account类

class Account {
 protected double balance ; //余额

public Account(double balance) {
    super();
    this.balance = balance;
}

public double getBalance() {
    return balance;
}

public boolean deposit(double amt){  //存钱方法
    balance+=amt;
    return true;

}   

//重写withdraw方法 声明并抛出异常
public void withdraw(double amt)throws OverdraftException{  
if(amt<=balance){ 
  balance - = amt;
}else{  
  //抛出"资金不足",及其不足额数
  throw new OverdraftException("资金不足",amt-balance);
        }
    }
}  

//修改CheakingAccount类

class  CheakingAccount {  

private double overdraftProtection;

public CheckingAccount(double balance) {
    super(balance);
}

public CheckingAccount(double balance,double overdraftProtection) {
        super(balance);
        this.overdraftProtection = overdraftProtection;
}
@Override 
public void withdraw(double amt)throws OverdraftException{ 
    if(balance>=amt){
        balance - = amt;
    }else{
      if(overdraftProtection==0){ 
      throw new OverdraftException("no overdraft protection",amt-balance);
      }else if(overdraftProtection<=amt-balance){ 
      throw new OverdraftException("Insufficient fund for overdraft protection",amt-balance);
      }else{ 
      overdraftProtection - =(amt - balance); 
      balacne = 0;
            }
        }
    }
}  

//测试类

public class TestBanking07 {

public static void main(String[] args) {
    Bank bank = Bank.getBank();
    banking.Customer customer;
    Account account;

    // Create two customers and their accounts
    bank.AddCustomer("Jane", "Simms");
    customer = bank.getCustomer(0);
    customer.addAccount(new SavingAccount(500.00, 0.05));
    customer.addAccount(new CheckingAccount(200.00, 500.00));
    bank.AddCustomer("Owen", "Bryant");
    customer = bank.getCustomer(1);
    customer.addAccount(new CheckingAccount(200.00));

    // Test the checking account of Jane Simms (with overdraft protection)
    customer = bank.getCustomer(0);
    account = customer.getAccount(1);
    System.out
            .println("Customer [" + customer.getLastName() + ", "
                    + customer.getFirstName() + "]"
                    + " has a checking balance of " + account.getBalance()
                    + " with a 500.00 overdraft protection.");
    try {
        System.out.println("Checking Acct [Jane Simms] : withdraw 150.00");
        account.withdraw(150.00);
        System.out.println("Checking Acct [Jane Simms] : deposit 22.50");
        account.deposit(22.50);
        System.out.println("Checking Acct [Jane Simms] : withdraw 147.62");
        account.withdraw(147.62);
        System.out.println("Checking Acct [Jane Simms] : withdraw 470.00");
        account.withdraw(470.00);
    } catch (OverdraftException e1) {
        System.out.println("Exception: " + e1.getMessage() + "   Deficit: "
                + e1.getDeficit());
    } finally {
        System.out.println("Customer [" + customer.getLastName() + ", "
                + customer.getFirstName() + "]"
                + " has a checking balance of " + account.getBalance());
    }
    System.out.println();

    // Test the checking account of Owen Bryant (without overdraft
    // protection)
    customer = bank.getCustomer(1);
    account = customer.getAccount(0);
    System.out.println("Customer [" + customer.getLastName() + ", "
            + customer.getFirstName() + "]" + " has a checking balance of "
            + account.getBalance());
    try {
        System.out.println("Checking Acct [Owen Bryant] : withdraw 100.00");
        account.withdraw(100.00);
        System.out.println("Checking Acct [Owen Bryant] : deposit 25.00");
        account.deposit(25.00);
        System.out.println("Checking Acct [Owen Bryant] : withdraw 175.00");
        account.withdraw(175.00);
    } catch (OverdraftException e1) {
        System.out.println("Exception: " + e1.getMessage() + "   Deficit: "
                + e1.getDeficit());
    } finally {
        System.out.println("Customer [" + customer.getLastName() + ", "
                + customer.getFirstName() + "]"
                + " has a checking balance of " + account.getBalance());
        }
    }
} 

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值