如何避免嵌套for-each循环?

3

我有一个类,应该解析一个XML文件,就像这样:

<customers>
        ...
        <customer>
            <id>123456</id>
            <name>Mike</name>
            <orders>
                ...
                <order>
                    <id>233658</id>
                    <positions>
                        ...
                        <position>
                            <id>12345</id>
                            <price>10.0</price>
                            <count>5</count>
                        </position>
                        ...
                    </positions>
                </order>
                ...
            </orders>
        </customer>
<customers>

我将使用JAXB对其进行反序列化,然后处理结果对象以获取统计信息(如最大订单金额、总订单金额等)。
在这种情况下使用3级foreach循环是一种不好的做法吗?
public void getStatistics() {
    for (Customer customer: this.customers.getCustomer()) {

        BigDecimal customerTotalAmount = new BigDecimal(0);
        for (Order order : customer.getOrders().getOrder()) {

            BigDecimal orderAmount = new BigDecimal(0);
            for (Position position : order.getPositions().getPosition()) {
                orderAmount = orderAmount.add( position.getPrice().multiply(new BigDecimal(position.getCount())) );
            }

            customerTotalAmount = customerTotalAmount.add(orderAmount);
            this.totalOrders++;
        }

        this.totalAmount = this.totalAmount.add(customerTotalAmount);
    }
}

客户(Customer)、订单(Order)和职位(Position)类已从XSD模式自动生成,我认为修改它们不好。

我做错了什么?如何避免这些嵌套循环?

谢谢。


我想应该有一些库可以让你选择DOM中的元素。虽然我从未在Java中做过这个,所以无法指向任何一个。 - Canadian Coder
1个回答

4

我建议提取一些方法:

public void getStatistics() {
    for (Customer customer: this.customers.getCustomer()) {
        BigDecimal customerTotalAmount = processCustomer(customer);
        this.totalAmount = this.totalAmount.add(customerTotalAmount);
    }
}

private void processCustomer(Customer customer){
    BigDecimal customerTotalAmount = new BigDecimal(0);
    for (Order order : customer.getOrders().getOrder()) {
        BigDecimal orderAmount = new BigDecimal(0);
        for (Position position : order.getPositions().getPosition()) {
            orderAmount = orderAmount.add( position.getPrice().multiply(new BigDecimal(position.getCount())) );
        }

        customerTotalAmount = customerTotalAmount.add(orderAmount);
        this.totalOrders++;
    }
    return customerTotalAmount;
}

对于订单和位置循环,做同样的事情,给方法取一个足够描述性的名称,并确保它们返回适当的值,您将得到一个漂亮,干净的代码。这些仍然是嵌套循环,但至少当您看到它们时,您的眼睛不会受伤。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接