//編寫異常類
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());
}
}
}