有没有更好的方法来访问ArrayList对象元素?

时间:2017-08-22 00:50:50

标签: java oop object arraylist

我想了解一下,但我只是想知道是否有更清洁的方法来做到这一点

这是我主要的要点

public class Main {

    private static Bank Chase = new Bank();

    //This is the function in main to add a transaction to a specified customer of a branch

    public static void addTransaction() {

        System.out.println("Enter the name of the branch");
        String branch = scanner.nextLine();
        System.out.println("Enter the name of the person");
        String name = scanner.nextLine();
        System.out.println("Enter the amount you would like to add");
        double amount = scanner.nextDouble();
        scanner.nextLine();

        Chase.getBranchList().get(Chase.branchIndex(branch)).getCustomerList().get(Chase.getBranchList().get(Chase.branchIndex(branch)).customerIndex(name)).addTransaction(amount);
    }
}

这最后一行真的很长,让其他人感到困惑

//gets the branchlist -> gets the specified branch -> gets the customerlist -> finds the specified customer -> adds transaction

这些是函数引用的类的其他相关部分

public class Bank {

    private ArrayList<Branch> branchList = new ArrayList<Branch>();

    public ArrayList<Branch> getBranchList() {
        return branchList;
    }
    public int branchIndex(String name){
        for(Branch branch: branchList){
        if(branch.getName().equals(name)){
            return branchList.indexOf(branch);
            }
        }
        return -1;
    }
}

public class Branch {

    private String branchName;
    private ArrayList<Customer> customerList;

    public ArrayList<Customer> getCustomerList() {
        return customerList;
    }
    public int customerIndex(String name){
        for(Customer customer: customerList){
            if(customer.getName().equals(name)){
                return customerList.indexOf(customer);
            }
        }
        return -1;
    }

public class Customer {

    private String customerName;
    private ArrayList<Double> transactions = new ArrayList<Double>();


 public Customer(String customerName, double amount) {

        this.customerName = customerName;
        this.transactions = new ArrayList<Double>();
        transactions.add(amount);
    }
    public String getName() {

        return customerName;
    }

    public void addTransaction(double transaction){

        transactions.add(transaction);
    }
}

那么有没有更可读的方法来访问对象ArrayLists中的这些元素?我认为addTransaction()的最后一行看起来有点多余。

3 个答案:

答案 0 :(得分:0)

目前您正在假设唯一的客户/分支机构名称,然后在您的阵列列表中循环以按名称查找客户。如果这是一个有效的假设,这个假设很好,但可能意味着有更多的最优解。我建议使用代码的重构来利用java哈希映射: https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

基本上,这意味着您可以直接通过名称访问客户/银行,并将大大简化您的代码!它还具有性能优势。

对于您的场景,此重构看起来与此类似:

public class Branch
{
    private HashMap<String, Customer> _customers;
    private String _branchName;

    public Branch(String branchName)
    {
        _branchName = branchName;
        _customers = new HashMap<String, Customer>();
    }

    public Customer getCustomer(String customerName)
    {
        return _customers.get(customerName);
    }
}

如果您对银行采用相同的措施,您应该能够访问客户并按如下方式添加交易:

Chase.getBranch(branch).getCustomer(name).addTransaction(transaction);

如果您需要帮助转换银行,请告诉我们:)

答案 1 :(得分:0)

而不是一条长线

a)将代码分成多行

Chase.getBranchList().get(Chase.branchIndex(branch))
                     .getCustomerList()
                     .get(Chase.getBranchList()
                         .get(Chase.branchIndex(branch))
                         .customerIndex(name))
                     .addTransaction(amount);

b)将每个get的返回值存储到局部变量中,尤其是重新调用相同方法的代码,例如: Chase.branchIndex(branch)Chase.getBranchList()

答案 2 :(得分:0)

你走在正确的轨道上,但是你有一些小的设计缺陷。

第1步:getBranchByName(String branchName)方法添加到您的Bank类中,该类返回Branch个对象,并删除branchIndex()方法:

public Branch getBranchByName(String branchName) {
        return branchList.stream()
                .filter(branch -> branch.getBranchName().equals(branchName))
                .findAny()
                .get();
    }

第2步:向getCustomerByName(String name)类添加一个名为Customer的方法,该方法返回Customer个对象,并删除customerIndex()方法:

public Customer getCustomerByName(String name) {
        return customerList.stream()
                .filter(customer -> customer.getCustomerName().equals(name))
                .findAny()
                .get();
    }

第3步:现在,main()方法中的方法调用变得更加紧凑,简单易读:

Chase.getBranchByName(branchName).getCustomerByName(customerName).addTransaction(amount);

注意:我已经使用了 Java 8流。如果您不被允许使用Java 8流,您可以通过编写for()循环来坚持编程的经典命令式,就像您之前所做的那样。作为一个简单的例子,如果你想用老式的 Java 7 风格写getBranchByName(String branchName),你的循环看起来像这样:

for(Branch branch : branchList) {
        if(branch.getBranchName().equals(branchName)){
                return branch;
        }   
}
相关问题