java异常处理在Bank项目中的应用

//页面一
public class Bank {
   private Customer[] customers;//用于存放客户
   private int numberOfCustomers;//记录Customer的个数
   
   private Bank() {
       customers = new Customer[5];
   }
   private static Bank bank = new Bank();
   public static Bank getBanking() {
       return bank;
   }
   //添加一个Customer到数组里
   public void addCustomer(String f,String l) {
       Customer cust = new  Customer(f,l);
       customers[numberOfCustomers] = cust;
       numberOfCustomers++;
   }
   //获取Customer的个数
   public int getNumOfCustomers() {
       return numberOfCustomers;
   }
   //返回指定索引位置的Customer
   public Customer getCustomer(int index) {
       return customers[index];
   }
}

//页面二
public class Customer {
   private String firstName;
   private String lastName;
   private Account account;
   
   public Customer(String f,String l) {
       firstName = f;
       lastName = l;
   }

public String getFirstName() {
    return firstName;
}

public String getLastName() {
    return lastName;
}

public Account getAccount() {
    return account;
}

public void setAccount(Account account) {
    this.account = account;
}
   
}

//页面三
//账户
public class Account {
    protected double balance;//账户余额

    /**
     * @param balance
     */
    public Account(double balance) {
        super();
        this.balance = balance;
    }

    public double getBalance() {
        return balance;
    }

    //存钱
    public boolean deposit(double amt) {//amt:要存的额度
        balance += amt;
        return true;
    }
    //取钱
    public void withdraw(double amt) throws OverdraftException {//amt:要取的额度
        if(balance >= amt) {
            balance -= amt;
            
        }else {
            throw new OverdraftException("余额不足",amt - balance);
        }
    }

}
//页面四
//存储账户
public class SavingAccount extends Account{
 private double interestRate;//利率
 
 public SavingAccount(double balance,double init_rate) {
     super(balance);
     this.interestRate = init_rate;
 }
}

//页面五
public class CheckingAccount extends Account{
    private Double overdraftProtection;//透支额度
    
    public CheckingAccount(double balance) {
        super(balance);
    }

    /**
     * @param balance
     * @param overdraftProtection
     */
    public CheckingAccount(double balance, double overdraftProtection) {
        super(balance);
        this.overdraftProtection = overdraftProtection;
    }

    public double getOverdraftProtection() {
        return overdraftProtection;
    }

    public void setOverdraftProtection(double overdraftProtection) {
        this.overdraftProtection = overdraftProtection;
    }
    @Override
    public void withdraw(double amt) throws OverdraftException{//amt:要取的额度
        if(balance >= amt) {
            balance -= amt;
        }else {
            if(overdraftProtection == null) {
                throw new OverdraftException("没有透支额度取款失败,还差",amt - balance);
            }else if(overdraftProtection < amt - balance){
                throw new OverdraftException("透支额度不足取款失败,还差",amt - balance-overdraftProtection);
            }else {
                overdraftProtection -= (amt - balance);
                balance = 0;
            }
        }
    }
    
}

//页面六
public class TestBanking {
    public static void main(String[] args) {
        Bank bank = Bank.getBanking();
        Customer customer;
        Account account;
        
        //Create two customers and their account
        bank.addCustomer("Jane","Simms");
        customer = bank.getCustomer(0);
        customer.setAccount(new SavingAccount(500.00,0.05));
        customer.setAccount(new CheckingAccount(200.00,500.00));
        bank.addCustomer("Owen","Bryant");
        customer = bank.getCustomer(1);
        customer.setAccount(new CheckingAccount(200.00));
        
        //Test the checking account of Jane Simms(with overdraft protection)
        customer = bank.getCustomer(0);
        account = customer.getAccount();
        System.out.println("Customer [" + customer.getLastName()
                   + "," + customer.getFirstName() + "] :"
                   + "has a checking balance of "
                   + account.getBalance()
                   +" with a 500.00 overdraftProtection.");
        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();
        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 22.50 ");
        account.deposit(22.50);
        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());
        }
        System.out.println();
    }
}

//页面七
public class OverdraftException extends Exception{
    static final long serialVersionUID = -338751124229948L;
    private double deficit;//表示所取的钱与余额的差额

    public double getDeficit() {
       return deficit;
   }
   
   public OverdraftException(String msg,double deficit) {
       super(msg);
       this.deficit = deficit;
   }
   
   
}

猜你喜欢

转载自blog.csdn.net/weixin_41536380/article/details/88751315