double daysRemainingToReturn = 0;
// if charges is null or empty, return the days estimated
if (charges != null && !charges.isEmpty()) {
// else find time remaining
Charge previousCharge = null;
for (Charge charge : charges) {
if (previousCharge == null ) {
// if it is the first loop
previousCharge = charge;
daysRemainingToReturn = charge.getDaysNeededToFinish();
} else if (charge.getDay().after(previousCharge.getDay())) {
// if the charge is more recent than the previous one
previousCharge = charge;
daysRemainingToReturn = charge.getDaysNeededToFinish();
} else if (charge.getDay().equals(previousCharge.getDay())) {
// if the charge is the same day than the previous one
previousCharge = charge;
daysRemainingToReturn = daysRemainingToReturn + charge.getDaysNeededToFinish();
}
}