当缺少行时使用上一行的值汇总值

时间:2019-01-10 14:15:56

标签: java iterator

我有一堆包含税款的行。

每行包含PaymentDueDate

如果在PaymentDueDate之间缺少行,则必须使用与上一行相同的值来汇总所有总计。

在下面的示例中,Row="2"Row="3"之间的2015 / 09、2015 / 10、2015 / 11、2015 / 12、2016 / 01、2016 / 02月份缺少数据。< / p>

因此,我必须使用Row="2"值来说明缺少的行。

<PaymentChangeMaintenance>
  <PaymentChangeMaintenanceTransaction Row="1">
         <BuydownSubsidyAmount>0.00</BuydownSubsidyAmount>
         <AnnualInterestRate>4.75000</AnnualInterestRate>
         <PIAmount>689.79</PIAmount>
         <PaymentDueDate>2015-07-01</PaymentDueDate>
         <CityTaxAmount>23.22</CityTaxAmount>
         <CountyTaxAmount>32.25</CountyTaxAmount>
  </PaymentChangeMaintenanceTransaction>
  <PaymentChangeMaintenanceTransaction Row="2">
         <BuydownSubsidyAmount>0.00</BuydownSubsidyAmount>
         <AnnualInterestRate>4.75000</AnnualInterestRate>
         <PIAmount>689.79</PIAmount>
         <PaymentDueDate>2015-08-01</PaymentDueDate>
         <CityTaxAmount>125.25</CityTaxAmount>
         <CountyTaxAmount>666.22</CountyTaxAmount>
  </PaymentChangeMaintenanceTransaction>
  <PaymentChangeMaintenanceTransaction Row="3">
         <BuydownSubsidyAmount>0.00</BuydownSubsidyAmount>
         <AnnualInterestRate>4.75000</AnnualInterestRate>
         <PIAmount>689.79</PIAmount>
         <PaymentDueDate>2016-03-01</PaymentDueDate>
         <CityTaxAmount>125.25</CityTaxAmount>
         <CountyTaxAmount>666.22</CountyTaxAmount>
  </PaymentChangeMaintenanceTransaction>
</PaymentChangeMaintenance>

这是某人编写的代码,但是看起来不干净。我想使用for-each:/

private void aggregateEscrowPaymountAmounts(List<PaymentChangeMaintenanceFieldsV214Type> fieldsType,
             PaymentChangeMaintenance paymentChangeMaintenance, final int numberOfTrialPayments) {
     AtomicInteger cnt = new AtomicInteger(1);
     Iterator<PaymentChangeMaintenanceFieldsV214Type> fieldsTypeIterator = fieldsType.iterator();
     PaymentChangeMaintenanceFieldsV214Type fieldType = fieldsTypeIterator.next();
     PaymentChangeMaintenanceFieldsV214Type nextFieldType = null;
     if (fieldsTypeIterator.hasNext()) {
                nextFieldType = fieldsTypeIterator.next();
     }
     LocalDate monthDate = fieldType.getNextPaymentDueDate();

     while (cnt.getAndIncrement() <= numberOfTrialPayments) {
                PaymentChangeMaintenance tempPaymentChangeMaintenance = createPaymentChangeMaintenanceEscrow(fieldType);
                paymentChangeMaintenance.aggregate(tempPaymentChangeMaintenance);
                monthDate = monthDate.plusMonths(1);
                if (nextFieldType != null) {
                       LocalDate nextFieldTypeDate = nextFieldType.getNextPaymentDueDate();

                       if (nextFieldTypeDate.getMonthValue() == monthDate.getMonthValue()) {
                               fieldType = nextFieldType;
                               if (fieldsTypeIterator.hasNext()) {
                                      nextFieldType = fieldsTypeIterator.next();
                               } else {
                                      nextFieldType = null;
                               }
                       }
                }
     }
}

1 个答案:

答案 0 :(得分:1)

对于这种情况,您可以使用以下方法:确定一个步骤-对您来说是一个月。然后在下一步没有该值的情况下初始化默认值。然后使用一些方法,该方法将获取下一个值和默认值,并取决于存在的步骤将返回其中之一 这是一个伪代码:

List<Item> items;
Item nextItem = items.get(0);
Value step = month;
for (int i = 1; i < items.size(); i++) {
  nextItem = getNextItem(items.get(i), nextItem, step);
  ****
}


Item getNextItem(Item nextItem, Item defaultItem, Value step) {
    if (!nextItem.getStepValue().equals(calcNext(step))) {
        return defaultItem;
    } else {
        return nextItem;
    }
}

StepValue calcNext(Value step) {
    /*some calculations. In your case month increment*/
}