Java番外之集合(bank8项目)

背景

  • bank7项目后续
  • 要求在此基础上修改:将数组转换为集合ArrayList,同时调整相应方法,最后使用迭代器进行遍历

Bank类

package banking8.domain;

import java.util.ArrayList;
import java.util.List;

public class Bank {
    private List<Customer> customers;

    private Bank() {
        customers = new ArrayList<Customer>();
    }

    private static Bank bank = new Bank();

    public static Bank getBanking() {
        return bank;
    }

    public void addCustomer(String f, String l) {
        Customer cust = new Customer(f, l);
        customers.add(cust);
    }

    public int getNumOfCustomers() {
        return customers.size();
    }

    public Customer getCustomer(int index) {
        return customers.get(index);
    }
}

Customer类

package banking8.domain;

import java.util.ArrayList;
import java.util.List;

public class Customer {
    private String firstName;
    private String lastName;
    private List<Account> accounts;//用于存放account

    public Customer(String f, String l) {
        firstName = f;
        lastName = l;
        accounts = new ArrayList<Account>();
    }
//向集合中添加一个account
    public void addAccount(Account account) {
        accounts.add(account);

    }
//获取account的个数
    public int getNumOfAccounts() {// public易忘
        return accounts.size();
    }
//返回指定索引处的account
    public Account getAccount(int index) {
        return accounts.get(index);
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

附加题

要求:

修改 CustomerReport 类,使用 Iterator 实现对客户的迭代
1. 在 Bank 类中,添加一个名为 getCustomers 的方法,该方法返回一个客户列 表上的 iterator
2. 在 Customer 类中,添加一个名为个体 Accounts 的方法,该方法返回一个帐 户列表上的 iterator
3. 修改 CustomerReport 类,使用一对嵌套的 while 循环(而不是使用嵌套的 for 循环),在客户的 iterator 与帐户的 iterator 上进行迭代
4. 重新编译运行 TestBanking 程序,应看到与上面一样的输出结果

这里写图片描述

  • list.iterator()返回的是Iterator的一个实现类的对象(存储的都是customer,所以使用泛型)或称返回迭代器对象

Iterable和Iterator接口定义的方法

  • public interface Iterator
    这里写图片描述

  • 所有已知实现类:
    BeanContextSupport.BCSIterator, EventReaderDelegate, Scanner

  • 接口 Iterable
    这里写图片描述

总结:ArrayList实现了Iterable接口,重写iterator()方法,返回了迭代器对象或是实现了Iterator接口的类的对象

添加迭代方法

//Bank类中
public Iterator<Customer> getCustomers() {
        return customers.iterator();
    }
//Customer类中
public Iterator<Account> getAccounts() {
        return accounts.iterator();
    }

打印类

package banking8.reports;

import java.text.NumberFormat;
import java.util.Iterator;

import banking8.domain.Account;
import banking8.domain.Bank;
import banking8.domain.CheckAccount;
import banking8.domain.Customer;
import banking8.domain.SavingAccount;

public class CustomerReport {
    private Customer customer;
    private Account account;
    private Bank bank = Bank.getBanking();

    public void generateReport() {
        NumberFormat currency_format = NumberFormat.getCurrencyInstance();
        // Generate a report
        System.out.println("\t\t\tCUSTOMERS REPORT");
        System.out.println("\t\t\t================");
        // for (int cust_idx = 0; cust_idx < bank.getNumOfCustomers();
        // cust_idx++) {
        // customer = bank.getCustomer(cust_idx);
        Iterator<Customer> customers = bank.getCustomers();//返回迭代器对象
        while (customers.hasNext()) {
            customer = customers.next();   //   没有},通过迭代器指针获取集合元素

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

            // for (int acct_idx = 0; acct_idx < customer.getNumOfAccounts();
            // acct_idx++) {
            // Account account = customer.getAccount(acct_idx);
            Iterator<Account> accounts = customer.getAccounts();//返回迭代器对象,并接受
            while (accounts.hasNext()) {
                account = accounts.next();

                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 = "SavingAccount";
                }
                if (account instanceof CheckAccount) {
                    account_type = "CheckAccount";
                }
                // 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 "
                        + currency_format.format(account.getBalance()));

            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/lwz45698752/article/details/81429769
今日推荐