【Java基礎-實驗7】Banking_7 -添加銀行透支扣款系統的 thorw異常機制
阿新 • • 發佈:2018-10-14
private double without ane 信息 exceptio 構造器 分享 extends
實驗基本要求:
實驗題目 7:(在6基礎上修改) 將建立一個 OverdraftException 異常,它由 Account 類的withdraw()方法 拋出。 實驗目的: 自定義異常 實驗說明: 創建 OverdraftException 類 1. 在 banking.domain 包中建立一個共有類 OverdraftException. 這個類 擴展 Exception 類。 2. 添加一個 double 類型的私有屬性 deficit.增加一個共有訪問方法 getDeficit 3. 添加一個有兩個參數的共有構造器。deficit 參數初始化 deficit 屬性 修改 Account 類 4. 重寫 withdraw 方法使它不返回值(即 void).聲明方法拋出 overdraftException異常 5. 修改代碼拋出新異常,指明“資金不足”以及不足數額(當前余額扣除請求的數額) 修改 CheckingAccount 類 6. 重寫 withdraw 方法使它不返回值(即 void).聲明方法拋出 overdraftException 異常 7. 修改代碼使其在需要時拋出異常。兩種情況要處理:第一是存在沒有透支保 護的赤字,對這個異常使用“no overdraft protection”信息。第二是 overdraftProtection 數 額 不 足 以 彌 補 赤 字 : 對 這 個 異 常 可 使 用 ”Insufficient funds for overdraft protection” 信息View Code
工程組織:(未展示的文件和實驗6的基本完全一直)
CheckingAccount.java (錢款+額度不足, 不再返回int)
package Banking_7; public class CheckingAccount extends Account { private double overdraft;//超額限額保護 public CheckingAccount(double balance,double overd){ super(balance); this.overdraft=overd; }View Codepublic CheckingAccount(double balance){ super(balance); this.overdraft=0; } public void showinfo(){ System.out.println("您的余額:"+this.getBalance()+"\t"+ "您的可透支余額:"+this.overdraft); } public void withdraw(double amt) throws OverdraftException{if(amt<=super.getBalance()) super.setBalance(super.getBalance()-amt ); else{ if(this.overdraft==0){ throw new OverdraftException("no overdraft protection(沒有透支保護):",(amt-this.balance)); } double val=amt-super.getBalance(); if(val<=this.overdraft) { super.setBalance(0); this.overdraft-=val; } else{ throw new OverdraftException("Insufficient funds for overdraft protection: 數額不足以彌補赤字," + "缺少資金或額度:",val-this.overdraft); } } } }
Account.java
package Banking_7; public class Account { protected double balance=0;//余額 ,uml前該變量是 ‘-‘ public Account(double init_balance){ balance=init_balance; } public double getBalance() { return balance; } public void setBalance(double b){this.balance=b;} //存錢 public boolean deposit(double amt){ this.balance+=amt;return true; } //取錢 public void withdraw(double amt) throws OverdraftException{ if(amt>this.balance){ throw new OverdraftException("資金不足,缺少金額:",(amt-this.balance)); } else{ this.balance-=amt; } } }View Code
OverdraftException.java (透支額度的 異常保護機制 )
package Banking_7; public class OverdraftException extends Exception{ private double deficit; public double getDeficit() { return deficit; } public void setDeficit(double deficit) { this.deficit = deficit; } public OverdraftException(double deficit) { this.deficit = deficit; } public OverdraftException(String message,double deficit) { super(message); this.deficit=deficit; System.out.println(message+deficit); } }View Code
TestBanking_7.java (測試類)
package TestBanks;/* * 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 Banking_7.*; public class TestBanking_7 { public static void main(String[] args) { Bank bank = Bank.getBank(); 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),1); customer.addAccount(new CheckingAccount(200.00, 500.00),2); bank.addCustomer("Owen", "Bryant"); customer = bank.getCustomer(1); customer.addAccount(new CheckingAccount(200.00),2); // Test the checking account of Jane Simms (with overdraft protection) customer = bank.getCustomer(0); account = customer.getCheckingAccount(); 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.getCheckingAccount(); 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()); } } }View Code
運行結果:
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 Insufficient funds for overdraft protection: 數額不足以彌補赤字,缺少資金或額度:45.120000000000005 Exception: Insufficient funds for overdraft protection: 數額不足以彌補赤字,缺少資金或額度: Deficit: 45.120000000000005 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 no overdraft protection(沒有透支保護):50.0 Exception: no overdraft protection(沒有透支保護): Deficit: 50.0 Customer [Bryant, Owen] has a checking balance of 125.0View Code
【Java基礎-實驗7】Banking_7 -添加銀行透支扣款系統的 thorw異常機制