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.