0

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.

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

10 Comments

Its not that I want to exclude it, but I want to include every element not just the first.
So, you need it to enter the if condition when there is a new day, right?
Yes, but during the first iteration previousDay is null causing the if to assert True. therefore, inserting the first iteration and then continues to set previousDay to Monday and proceeds to not add any other values for Monday.
I see. I got your point. I revised my answer. I hope you get it working now. Thanks.
I like your thought process there but unfortunately it doesn't work, as the following if still asserts false
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.