我有兩個類,其含義是取可用值(日期、金額和匯率)。接下來,填寫日期之間的空白,以前的值
class ReportRow {
private LocalDate date;
private BigDecimal amount;
private BigDecimal rate;
public ReportRow(LocalDate date, BigDecimal amount, BigDecimal rate) {
this.date = date;
this.amount = amount;
this.rate = rate;
}
public LocalDate getDate() {
return date;
}
public BigDecimal getAmount() {
return amount;
}
public BigDecimal getRate() {
return rate;
}
@Override
public String toString() {
return date " | " amount " | " rate;
}
}
public class Main {
public static void main(String[] args) {
List<ReportRow> originalReport = List.of(
new ReportRow(LocalDate.of(2022, 6, 20), BigDecimal.valueOf(10000), BigDecimal.valueOf(3)),
new ReportRow(LocalDate.of(2023, 1, 15), BigDecimal.valueOf(8000), BigDecimal.valueOf(3)),
new ReportRow(LocalDate.of(2023, 7, 5), BigDecimal.valueOf(6500), BigDecimal.valueOf(3)));
System.out.println("Before:");
originalReport.forEach(System.out::println);
List<ReportRow> updatedReport = new ArrayList<>();
int size = originalReport.size();
updatedReport.add(originalReport.get(0));
for (int i = 1; i < size; i ) {
ReportRow lastRow = originalReport.get(i - 1);
ReportRow currentRow = originalReport.get(i);
BigDecimal rate = lastRow.getRate();
BigDecimal lastAmount = lastRow.getAmount();
LocalDate dateStart = lastRow.getDate();
LocalDate dateEnd = currentRow.getDate();
if (ChronoUnit.MONTHS.between(dateStart.withDayOfMonth(dateEnd.getDayOfMonth()), dateEnd) > 1) {
for (LocalDate date = dateStart.plusMonths(1); date.isBefore(dateEnd); date = date.plusMonths(1))
updatedReport.add(new ReportRow(date, lastAmount.divide(rate, RoundingMode.CEILING), rate));
}
updatedReport.add(currentRow);
}
System.out.println("After:");
updatedReport.forEach(System.out::println);
//for (ReportRow reportRow : originalReport) {
// BigDecimal newAmount = reportRow.getAmount().divide(reportRow.getRate(), //RoundingMode.CEILING);
// System.out.println(newAmount);
}
}
}
結果,我得到了這個結果:
Before:
2022-06-20 | 10000 | 3
2023-01-15 | 8000 | 3
2023-07-05 | 6500 | 3
After:
2022-06-20 | 10000 | 3
2022-07-20 | 3334 | 3
2022-08-20 | 3334 | 3
2022-09-20 | 3334 | 3
2022-10-20 | 3334 | 3
2022-11-20 | 3334 | 3
2022-12-20 | 3334 | 3
2023-01-15 | 8000 | 3
2023-02-15 | 2667 | 3
2023-03-15 | 2667 | 3
2023-04-15 | 2667 | 3
2023-05-15 | 2667 | 3
2023-06-15 | 2667 | 3
2023-07-05 | 6500 | 3
從結果可以看出,我除以匯率的金額只在要填補的缺口中除,主要金額保持不變。
我試圖在輸出中得到以下結果,以便將所有金額除以我的匯率:
Before:
2022-06-20 | 10000 | 3
2023-01-15 | 8000 | 3
2023-07-05 | 6500 | 3
After:
2022-06-20 | 3334 | 3
2022-07-20 | 3334 | 3
2022-08-20 | 3334 | 3
2022-09-20 | 3334 | 3
2022-10-20 | 3334 | 3
2022-11-20 | 3334 | 3
2022-12-20 | 3334 | 3
2023-01-15 | 2667 | 3
2023-02-15 | 2667 | 3
2023-03-15 | 2667 | 3
2023-04-15 | 2667 | 3
2023-05-15 | 2667 | 3
2023-06-15 | 2667 | 3
2023-07-05 | 2167 | 3
uj5u.com熱心網友回復:
要獲得所需的輸出,您的 Main 類應該是:
public class Main {
public static void main(String[] args) {
List<ReportRow> originalReport = List.of(
new ReportRow(LocalDate.of(2022, 6, 20), BigDecimal.valueOf(10000), BigDecimal.valueOf(3)),
new ReportRow(LocalDate.of(2023, 1, 15), BigDecimal.valueOf(8000), BigDecimal.valueOf(3)),
new ReportRow(LocalDate.of(2023, 7, 5), BigDecimal.valueOf(6500), BigDecimal.valueOf(3)));
System.out.println("Before:");
originalReport.forEach(System.out::println);
List<ReportRow> updatedReport = new ArrayList<>();
int size = originalReport.size();
for (int i = 1; i < size; i) {
ReportRow lastRow = originalReport.get(i - 1);
ReportRow currentRow = originalReport.get(i);
BigDecimal rate = lastRow.getRate();
BigDecimal lastAmount = lastRow.getAmount();
LocalDate dateStart = lastRow.getDate();
LocalDate dateEnd = currentRow.getDate();
if (ChronoUnit.MONTHS.between(dateStart.withDayOfMonth(dateEnd.getDayOfMonth()), dateEnd) > 1) {
for (LocalDate date = dateStart; date.isBefore(dateEnd); date = date.plusMonths(1)) {
updatedReport.add(new ReportRow(date, lastAmount.divide(rate, RoundingMode.CEILING), rate));
}
}
if (i == (size - 1)) {
updatedReport.add(new ReportRow(dateEnd, currentRow.getAmount().divide(rate, RoundingMode.CEILING), rate));
}
}
System.out.println("After:");
updatedReport.forEach(System.out::println);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/511715.html
標籤:爪哇春天弹簧靴
