Q:

Nested loop not calculating correct amount due to previous value not being set beforehand

I've come across a problem that has left me stumped. I basically have an array of Merchants, with each Merchant having an array that contains Payments.
Payments is a simple POJO with payment information, the loop is developed to retrieve each Payment from the Merchant, calculate the total amount for that day and store it in a new List. The code below is the code in question
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 );
}

The variable previousDay is meant to store the current day and continue iterating over the current day amounting the totals. When a new day is discovered then the current amount should be added to the List, along with day, and Merchant name and should continue the loop again. Though this is not the case, as during the first iteration the previousDay variable is null and asserting True causing the first element to be inserted into the List, though obviously after the first element each next element is calculated and stored correctly. 
The current output:
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 should be 280105.19 but is only 64.4 as its inserting only the first element for Monday.
Any assistance that could highlight a new interpretation of the current implementation would be greatly appreciated.

A:

Well, you may add this condition to catch the first element just before your second condition (that checks for new days):
if(previousDay == null)
   previousDay = payment.getPaymentDue().getDayOfWeek();

When you add to the list, use the previousDay, not the current one as the total values collected are for the previous days. You need the previous Merchant names as well.
This should work, hopefully. Here is your code after my additions:
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 );
}

Hope this helps!

