实验九:修改实验八完成的项目,使其具有异常处理的功能
一、实验目的
练习在项目中使用Java的异常处理机制。
二、实验内容
将建立一个 OverdraftException 异常类,它由 Account 类的withdraw()方法声明抛出。 UML类图如下图所示:
【实验步骤】
1. 在 banking.domain 包中建立一个公共类 OverdraftException, 这个类继承Exception 类。 2. 添加一个 double 类型的私有属性 deficit,增加一个共有访问方法getDeficit()。 3. 添加一个有两个参数的共有构造器。deficit 参数初始化 deficit 属性。 4. 修改 Account 类,重写 withdraw 方法使它不返回值(即 void),声明方法抛出。 OverdraftException异常,修改withdraw 方法声明抛出OverdraftException异常,指明“资金不足”以及不足数额(当前余额扣除请求的数额) 5. 修改 CheckingAccount 类,重写 withdraw 方法使它不返回值(即 void),声明方法抛出OverdraftException异常,修改withdraw 方法使其在需要时抛出异常。 分两种情况要处理: 第一是存在没有透支保护的赤字,对这个异常使用 “no overdraft protection”信息。 第二是透支保护数额不足以弥补赤字 : 对 这 个 异 常 可 使用 ”Insufficient funds for overdraft protection” 信息。
运行 TestBanking 类输出以下内容: Customer [simms,Jane]has a checking balance of 200.0 with a 500.0 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 funds for overdraft protection Deifcit:470.0
Customer [Simms,Jane]has a checking balance of 0.0 Customer [Bryant,Owen]has a checking balance of 200.0 Checking Acct[Bryant,Owen]: withdraw 100.00 Checking Acct[Bryant,Owen]: deposit25.00 Checking Acct[Bryant,Owen]: withdraw 175.00 Exception: no overdraft protection Deficit:50.0 Customer [Bryant,Owen]has a checking balance of 125.0
四、程序代码
程序结构目录如下图所示:
4.1 Account.java
package banking.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=balance+amt;
return true;
}
public void withdraw (double amt)throws OverdraftException
{
if(balance>=amt){
balance=balance-amt;
}
else{
throw new OverdraftException("金额不足",amt-balance);
}
}
public String getTypeOfAccounts(Account account){
if(account instanceof SavingsAccount){
return "Savings Account: ";
}
else {
return "Checking Account: ";
}
}
}
4.2 Bank.java
package banking.domain;
public class Bank {
private Customer[] customers;
private int numberOfCustomer;
private static Bank bank = new Bank();
public Bank(){
customers=new Customer[10];
numberOfCustomer=0;
}
public void addCustomer(String f,String l){
customers[numberOfCustomer]=new Customer(f,l);
numberOfCustomer++;
}
public int getNumOfCustomers(){
return numberOfCustomer;
}
public Customer getCustomer(int index){
return customers[index];
}
public static Bank getBank(){
return bank;
}
}
4.3 CheckingAccount.java
package banking.domain;
public class CheckingAccount extends Account {
private double protectedBy;
public CheckingAccount(double balance) {
super(balance);
}
public CheckingAccount(double balance, double protect) {
super(balance);
this.protectedBy=protect;
}
public void withdraw(double amt) throws OverdraftException{
if(protectedBy==0)
{
if(balance>=amt){
balance=balance-amt;
}
else{
throw new OverdraftException("no overdraft protection Deficit:",amt-balance);
}
}
else{
if(amt <= balance + protectedBy){
balance = balance - amt;
if(balance<0){
protectedBy+=balance;
balance=0;
}
}
else {
throw new OverdraftException("Insufficient funds for overdraft protection Deifcit:",amt);
}
}
}
}
4.4 Customer.java
package banking.domain;
public class Customer {
private String firstName;
private String lastName;
private Account[] account=new Account[10];
private SavingsAccount savingsAccount;
private CheckingAccount checkingAccount;
private int numberOfAccount;
public Customer(String f,String l){
this.firstName=f;
this.lastName=l;
}
public String getFirstName(){
return this.firstName;
}
public String getLastName(){
return this.lastName;
}
public Account getAccount(int number)
{
return this.account[number];
}
public int getNumOfAccounts(){
return numberOfAccount;
}
public void addAccount(Account newAccount){
account[numberOfAccount++]=newAccount;
}
public SavingsAccount getSavings(){
return savingsAccount;
}
public CheckingAccount getChecking(){
return checkingAccount;
}
public void setSavings(SavingsAccount saving){
savingsAccount=saving;
}
public void setChecking(CheckingAccount checking){
checkingAccount=checking;
}
}
4.5 OverdraftException.java
package banking.domain;
public class OverdraftException extends Exception{
private double deficit;
OverdraftException(String message,double deficit){
super(message);
this.deficit=deficit;
}
public double getDeficit(){
return this.deficit;
}
}
4.6 SavingsAccount.java
package banking.domain;
import banking.domain.Account;
public class SavingsAccount extends Account {
double insterstRate;
public SavingsAccount(double balance, double insterst_rate) {
super(balance);
insterstRate = insterst_rate;
}
}
4.7 CustomerReport.java
package banking.reports;
import banking.domain.*;
public class CustomerReport {
Bank bank = Bank.getBank();
Customer customer;
Account account;
public void generateReport(){
System.out.println("\t\t\tCUSTOMER REPORT ");
System.out.println("\t\t\t=================");
for(int index=0;index< bank.getNumOfCustomers();index++){
customer= bank.getCustomer(index);
System.out.println("Customer:"+customer.getLastName()+","+customer.getFirstName());
for(int i=0;i< customer.getNumOfAccounts();i++) {
account = customer.getAccount(i);
System.out.println(account.getTypeOfAccounts(account)+"current balance is $"+account.getBalance());
}
System.out.println();
}
}
}
4.8 TestBanking.java
package test;
import banking.domain.*;
public class TestBanking {
public static void main(String[] args) {
Bank bank = Bank.getBank();
Customer customer;
Account account;
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
customer.addAccount(new SavingsAccount(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));
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();
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());
}
}
}
|