diff --git a/src/Main.java b/src/Main.java index fe625bf..6de23ff 100644 --- a/src/Main.java +++ b/src/Main.java @@ -31,6 +31,13 @@ public class Main { ledger.post(operation5); printAccountBalance(card, ledger); printAccountBalance(cash, ledger); + Operation operation6 = Operation.income(LocalDate.now(), 504L, card); + operationLine = new OperationLine(new Category(4L,"Salary"), new BigDecimal(20000)); + operation6.addLine(operationLine); + ledger.post(operation6); + printAccountBalance(card, ledger); + printAccountBalance(cash, ledger); + } public static void printAccountBalance(Account account, Ledger ledger){ diff --git a/src/service/Ledger.java b/src/service/Ledger.java index 6d47c73..816ba37 100644 --- a/src/service/Ledger.java +++ b/src/service/Ledger.java @@ -26,13 +26,13 @@ public class Ledger { postOutflow(operation); break; case INFLOW: - //do something + postInflow(operation); break; case MOVEMENT: postMovement(operation); break; default: - //do something + throw new IllegalArgumentException("Не поддерживается тип операции: " + operationType); } } @@ -52,6 +52,22 @@ public class Ledger { entries.addAll(newEntries); } + private void postInflow(Operation operation) { + + if (operation.getLines().isEmpty()) { + throw new IllegalArgumentException("Документ прихода не содержит строк"); + } + ArrayList newEntries = new ArrayList<>(); + List operationLines = operation.getLines(); + for (OperationLine operationLine : operationLines) { + LedgerEntry ledgerEntry = new LedgerEntry(operation.getDate(), operation.getId(), + operation.getDestinationAccount(), operationLine.getCategory(), operationLine.getAmount()); + newEntries.add(ledgerEntry); + } + entries.removeIf(entry -> entry.getOperationId().equals(operation.getId())); + entries.addAll(newEntries); + } + private void postOpeningBalance(Operation operation) { ArrayList newEntries = new ArrayList<>();