Day08_13練習06

這裏寫圖片描述

//修改bank類
//Bank6 單例模式
//1.私有化構造函數
//2.在本類創建對象
//3.提供一個公共的方法給外界調用獲取該實例對象

public class Bank {  
    private Customer[] customers;  //客戶數組
    private int numberofCustomers; //當前客戶數量  

    //創建一個靜態的對象 
    private static Bank bank = new Bank();

    //私有化構造函數
    private Bank(){ 
    customers = new Customer[10];
    }   

    //提供一個靜態的方法供外界調用當調用該方法的同時實例化對象 
    public static Bank getInstance(){ 
        return bank;
    } 

    // 添加客戶
public void AddCustomer(String firstName, String lastName) {
    // 創建客戶對象
    Customer customer = new Customer(firstName, lastName);
    customers[numberofCustomers] = customer;
    numberofCustomers++;
    }

// 返回對應下標客戶
public Customer getCustomer(int index) {
    return customers[index];
    }

public int getNumberofCustomers() {
    return numberofCustomers;
    }

public static Bank getBank() {
    return bank;
    }

public Customer[] getCustomers() {
    return customers;
    }
}

//創建CustomerReport
//同一個銀行,因爲是單例模式,所以TestBanking6中的給bank中所初始化的所有客戶數據都是共享的.

public class CustomerReport {   
    public void getrateReport() {
    Bank bank = Bank.getInstance();
    banking.Customer customer; 

    for (int cust_idx = 0; cust_idx < bank.getNumberofCustomers(); cust_idx++) {
        customer = bank.getCustomer(cust_idx);

        System.out.println();
        System.out.println("Customer: " + customer.getLastName() + ", "
                + customer.getFirstName());

        for (int acct_idx = 0; acct_idx < customer.getNumberOfAccounts(); acct_idx++) {
            Account account = customer.getAccount(acct_idx);
            String account_type = "";// 賬戶類型

            // Determine the account type
            /***
             * Step 1: Use the instanceof operator to test what type of
             * account we have and set account_type to an appropriate value,
             * such as "Savings Account" or "Checking Account".
             ***/   

             //明確賬戶類型 
             if(account instanceof SavingAccount){ 
                 account_type = "Saving Account";
             }else if(account instanceof CheckingAccount){ 
                 account_type = "Checking Account";
             }   

             // Print the current balance of the account
            /***
             * Step 2: Print out the type of account and the balance. Feel
             * free to use the currency_format formatter to generate a
             * "currency string" for the balance.
             ***/  

            //打印對應賬戶的餘額
            System.out.println(account_type +":current balance is $"+account.getBalance());
                        } 
                    }
                }
        }   

//測試類

public class TestBanking6 {

    public static void main(String[] args) { 

    Bank bank = Bank.getInstance();
    banking.Customer customer;
    CustomerReport report = new CustomerReport();

    // Create several 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, 400.00));

    bank.AddCustomer("Owen", "Bryant");
    customer = bank.getCustomer(1);
    customer.addAccount(new CheckingAccount(200.00));

    bank.AddCustomer("Tim", "Soley");
    customer = bank.getCustomer(2);
    customer.addAccount(new SavingAccount(1500.00, 0.05));
    customer.addAccount(new CheckingAccount(200.00));

    bank.AddCustomer("Maria", "Soley");
    customer = bank.getCustomer(3);
    // Maria and Tim have a shared checking account
    customer.addAccount(bank.getCustomer(2).getAccount(1));
    customer.addAccount(new SavingAccount(150.00, 0.05));

    // Generate a report
    report.getrateReport();
      }
}  

這裏寫圖片描述

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