Bank项目7
背景:Bank项目6续
添加自定义异常类
package banking7.domain;
public class OverdraftException extends Exception {
static final long serialVersionUID = -112223311L;//key
private double deficit;//表示所取的钱与余额的差额
public double getDeficit() {
return deficit;
}
public OverdraftException(String msg,double deficit) {
super(msg);//key
this.deficit = deficit;
}
}
- 注意key处
修改Account类
withdraw方法
- 手动抛异常
Account类代码
package banking7.domain;
public class Account {
protected double balance;
public Account(double init_balance){
balance = init_balance;
}
public double getBalance(){
return balance;
}
public boolean deposit(double amt){
balance += amt;
return true;
}
public void withdraw(double amt) throws OverdraftException{//amt为要取的额度
if(balance >= amt){
balance -= amt;
}else{
throw new OverdraftException("资金不足", amt - balance);
}
}
}
修改CheckingAccount类
withdraw方法
成员变量类型(包装类)
- double–>Double
- 取值null:空,未赋值,表示无透支额度
- 取值0:有透支额度,但为0
CheckingAccount类代码
package banking7.domain;
public class CheckAccount extends Account {
private Double overdraftProtection;
public CheckAccount(double balance) {
super(balance);
}
public CheckAccount(double balance, double protect) {
super(balance);
this.overdraftProtection = protect;
}
public double getOverdraftProtection() {
return overdraftProtection;
}
public void setOverdraftProtection(double overdraftProtection) {
this.overdraftProtection = overdraftProtection;
}
public void withdraw(double amt) throws OverdraftException {
if (balance >= amt) {
balance -= amt;
// return true;
} else {
if (overdraftProtection == null) {
throw new OverdraftException("no overdraft protection", amt
- balance);
} else if (overdraftProtection <= amt - balance) {// insufficient
throw new OverdraftException(
"Insufficient funs for overdraft protection", amt
- balance);
} else {
overdraftProtection -= (amt - balance);
balance = 0;
}
}
}
}
测试类
package TestBanking;
/*
* This class creates the program to test the banking classes.
* It creates a set of customers, with a few accounts each,
* and generates a report of current account balances.
*/
import banking7.domain.Account;
import banking7.domain.Bank;
import banking7.domain.CheckAccount;
import banking7.domain.Customer;
import banking7.domain.OverdraftException;
import banking7.domain.SavingAccount;
public class TestBanking7 {
public static void main(String[] args) {
Bank bank = Bank.getBanking();
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 CheckAccount(200.00, 500.00));
bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
customer.addAccount(new CheckAccount(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());
}
}
}
输出结果
Customer [Simms, Jane] has a checking balance of 200.0 with a 500.00 overdraft protection.
Checking Acct [Jane Simms] : withdraw 150.00
Checking Acct [Jane Simms] : deposit 22.50
Checking Acct [Jane Simms] : withdraw 147.62
Checking Acct [Jane Simms] : withdraw 470.00
Exception: Insufficient funs for overdraft protection Deficit: 470.0
Customer [Simms, Jane] has a checking balance of 0.0
Customer [Bryant, Owen] has a checking balance of 200.0
Checking Acct [Owen Bryant] : withdraw 100.00
Checking Acct [Owen Bryant] : deposit 25.00
Checking Acct [Owen Bryant] : withdraw 175.00
Exception: no overdraft protection Deficit: 50.0
Customer [Bryant, Owen] has a checking balance of 125.0