Добавил перепроведение начальных остатков, проверки и расчёт итога по строкам

This commit is contained in:
Kashey committed 2026-10-16 13:16:49 +03:00
1 parent 357f494a56
commit a1c4990243
6 files changed
+220 -29

No files matched your search

+71 -5
View File
@@ -1,8 +1,6 @@
package service;
import model.Account;
import model.LedgerEntry;
import model.Operation;
import model.*;
import java.math.BigDecimal;
import java.util.ArrayList;
@@ -13,13 +11,82 @@ public class Ledger {
public void post(Operation operation) {
if (operation == null) {
throw new IllegalArgumentException("Не указан документ");
}
if (operation.getType() == null) {
throw new IllegalArgumentException("Не указан тип операции");
}
OperationType operationType = operation.getType();
switch (operationType) {
case OPENING_BALANCE:
postOpeningBalance(operation);
break;
case OUTFLOW:
//do something
break;
case INFLOW:
//do something
break;
case MOVEMENT:
//do something
break;
default:
//do something
}
}
private void postOpeningBalance(Operation operation) {
validateOpeningBalance(operation);
ArrayList<LedgerEntry> newEntries = new ArrayList<>();
LedgerEntry ledgerEntry = new LedgerEntry(operation.getDate(), operation.getId(),
operation.getDestinationAccount(), null, operation.getTotal());
newEntries.add(ledgerEntry);
entries.removeIf(entry -> entry.getOperationId().equals(operation.getId()));
entries.addAll(newEntries);
}
private void validateOpeningBalance(Operation operation) {
if (operation == null) {
throw new IllegalArgumentException("Не указан документ");
}
if (operation.getId() == null) {
throw new IllegalArgumentException("Не указан ID документа");
}
if (operation.getDate() == null) {
throw new IllegalArgumentException("Не указана дата документа");
}
if (operation.getType() != OperationType.OPENING_BALANCE) {
throw new IllegalArgumentException(
"Ожидается документ ввода начального остатка"
);
}
if (operation.getDestinationAccount() == null) {
throw new IllegalArgumentException("Не указан счёт-приёмник");
}
if (operation.getTotal() == null) {
throw new IllegalArgumentException("Не указана сумма документа");
}
}
public BigDecimal balance(Account account, LocalDate date) {
BigDecimal result = BigDecimal.ZERO;
for (LedgerEntry entry: entries) {
for (LedgerEntry entry : entries) {
boolean sameAccount = entry.getAccount().equals(account);
boolean withinPeriod = !entry.getDate().isAfter(date);
@@ -27,7 +94,6 @@ public class Ledger {
result = result.add(entry.getDeltaAmount());
}
}
return result;
}
}