我遇到了一个让我感到困惑的问题。我基本上有一个商户数组,每个商户都有一个包含付款的数组。

Payments是带有付款信息的简单POJO,开发了一个循环以从商家检索每个Payment,计算当天的总金额并将其存储在新的List中。下面的代码是有问题的代码

private void calculateMerchantTotals()
{
    double dayAmount = 0;
    DayOfWeek previousDay = null;

    for( Merchant merchant : merchantList )
    {
        for( Payment payment : merchant.getPayments() )
        {
            if( payment.getPaymentDue().get(ChronoField.CLOCK_HOUR_OF_DAY) >= 16 && payment.getPaymentDue().get(ChronoField.CLOCK_HOUR_OF_DAY) != 24 )
            {
                payment.setPaymentDue( payment.getPaymentDue().plusDays(1) );
            }

            dayAmount += payment.getAmount();

            if( !payment.getPaymentDue().getDayOfWeek().equals( previousDay ) )
            {
                dayAmount = Math.round(dayAmount * 100.0) / 100.0;
                tableDataList.add( new TableData( payment.getPaymentDue().getDayOfWeek(), merchant.getMerchantName(), dayAmount ) );
                dayAmount = 0;
            }

            previousDay = payment.getPaymentDue().getDayOfWeek();
        }
    }

    System.out.println( tableDataList );
}


变量previousDay用于存储当日并在当日继续迭代总计。当发现新的一天时,应将当前金额以及日期和商户名称添加到列表中,并应再次继续循环。尽管不是这种情况,因为在第一次迭代中,previousDay变量是null并断言True导致第一个元素插入到列表中,尽管显然在第一个元素之后,每个下一个元素都被计算并存储了正确地。

当前输出:

TableData:
Day: MONDAY
Name: Sky
Amount: 64.4,
TableData:
Day: TUESDAY
Name: Sky
Amount: 185031.75,
TableData:
Day: WEDNESDAY
Name: Sky
Amount: 273188.4 ... (continued)


TableData: Monday应该是280105.19,但只能是64.4,因为它仅在星期一插入第一个元素。

能够突出强调对当前实施方式的新解释的任何协助将不胜感激。

最佳答案

好吧,您可以添加此条件以在第二个条件(检查新日期)之前捕获第一个元素:

if(previousDay == null)
   previousDay = payment.getPaymentDue().getDayOfWeek();


当您添加到列表中时,请使用previousDay,而不是当前值,因为所收集的总值是前几天的值。您还需要以前的商家名称。
希望这可以工作。这是我添加后的代码:

private void calculateMerchantTotals()
{
    double dayAmount = 0;
    DayOfWeek previousDay = null;
    String previousName = "";

    for( Merchant merchant : merchantList )
    {
        for( Payment payment : merchant.getPayments() )
        {
            if( payment.getPaymentDue().get(ChronoField.CLOCK_HOUR_OF_DAY) >= 16 && payment.getPaymentDue().get(ChronoField.CLOCK_HOUR_OF_DAY) != 24 )
            {
                payment.setPaymentDue( payment.getPaymentDue().plusDays(1) );
            }

            dayAmount += payment.getAmount();

            // The newly injected condition here
            if(previousDay == null)
               previousDay = payment.getPaymentDue().getDayOfWeek();

            if( !payment.getPaymentDue().getDayOfWeek().equals( previousDay ) )
            {
                dayAmount = Math.round(dayAmount * 100.0) / 100.0;
                tableDataList.add( new TableData( previousDay, previousName, dayAmount ) );
                dayAmount = 0;
            }

            previousDay = payment.getPaymentDue().getDayOfWeek();
            previousName = merchant.getMerchantName();
        }
    }
    // For adding the last day into the list
    tableDataList.add( new TableData( previousDay, previousName, dayAmount ) );

    System.out.println( tableDataList );
}


希望这可以帮助!

07-28 00:45