Day08_12練習05

這裏寫圖片描述

 class SavingAccount extends Account{
    private double interestRate; //利率

    public SavingAccount(double balance ,double interestRate){
    super(balance); 
    this.interestRate=interestRate;
    }
 }

 class CheckingAccount extends Account{
    private double overdraftProtection;

    public CheckingAccount (double balance,double overdraftProtection){
    super(balance);
    this.overdraftProtection=overdraftProtection;
    } 

    //重寫withdraw方法 以避免餘額不夠的透支方法
    @Override
  public boolean withdraw(double amt) {
    if(amt<balance){
        balance-= amt;
    }else{
        if(overdraftProtection<(amt-balance)){
            return false;
        }else{
            //透支額度還是不夠
            overdraftProtection=(amt-balance);
            balance = 0;
        }
    }
    return true;
    }
 }

 //測試類
   public class TestBanking {

      public static void main(String[] args) {
        Bank  bank = new Bank();

        //
        // Create bank customers and their accounts
        //

        System.out.println("Creating the customer Jane Smith."); 
        Customer customer = new Customer("Jane","Smith");
        bank.addCustomer("Jane", "Simms"); 

        //code
        System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest.");  
        bank.getCustomer(0).setAccount(new SavingAccount(500,0.03));

        //code
        System.out.println("Creating the customer Owen c."); 
        bank.addCustomer("Owen ", "Bryant"); 

        //code
        customer = bank.getCustomer(1);
        System.out.println("Creating his Checking Account with a 500.00 balance and no overdraft protection.");  
        customer.setAccount(new Account(500));

        //code
        System.out.println("Creating the customer Tim Soley.");
        bank.addCustomer("Tim", "Soley");
        customer = bank.getCustomer(2);
        System.out.println("Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection."); 
        customer.setAccount(new CheckingAccount(500,500));

        //code
        System.out.println("Creating the customer Maria Soley."); 
        bank.AddCustomer("Maria", "Soley"); 

        //code
        customer = bank.getCustomer(3);
        System.out.println("Maria shares her Checking Account with her husband Tim."); 
        //將第二個名字叫Tim的客戶的賬戶賦值給 Maria這個賬戶
        customer.setAccount(bank.getCustomer(2).getAccount());
        System.out.println();

        //
        // Demonstrate behavior of various account types
        //

        // Test a standard Savings Account
        System.out.println("Retrieving the customer Jane Smith with her savings account.");
        customer = bank.getCustomer(0);
        account = customer.getAccount();
        // Perform some account transactions
        System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
        System.out.println("Deposit 22.50: " + account.deposit(22.50));
        System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
        System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
        // Print out the final account balance
        System.out.println("Customer [" + customer.getLastName()
                   + ", " + customer.getFirstName()
                   + "] has a balance of " + account.getBalance());

        System.out.println();

        // Test a Checking Account w/o overdraft protection
        System.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection."); 

        customer = bank.getCustomer(1);
        account = customer.getAccount();
        // Perform some account transactions
        System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
        System.out.println("Deposit 22.50: " + account.deposit(22.50));
        System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
        System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
        // Print out the final account balance
        System.out.println("Customer [" + customer.getLastName()
                   + ", " + customer.getFirstName()
                   + "] has a balance of " + account.getBalance());

        System.out.println();

        // Test a Checking Account with overdraft protection
        System.out.println("Retrieving the customer Tim Soley with his checking account that has overdraft protection.");
        customer = bank.getCustomer(2);
        account = customer.getAccount();
        // Perform some account transactions
        System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
        System.out.println("Deposit 22.50: " + account.deposit(22.50));
        System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
        System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
        // Print out the final account balance
        System.out.println("Customer [" + customer.getLastName()
                   + ", " + customer.getFirstName()
                   + "] has a balance of " + account.getBalance());

        System.out.println();

        // Test a Checking Account with overdraft protection
        System.out.println("Retrieving the customer Maria Soley with her joint checking account with husband Tim.");
        customer = bank.getCustomer(3);
        account = customer.getAccount();
        // Perform some account transactions
        System.out.println("Deposit 150.00: " + account.deposit(150.00));
        System.out.println("Withdraw 750.00: " + account.withdraw(750.00));
        // Print out the final account balance
        System.out.println("Customer [" + customer.getLastName()
                   + ", " + customer.getFirstName()
                   + "] has a balance of " + account.getBalance());
      }
    } 

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章