From 9742b55e437b27c7988e833de27b978123d8252f Mon Sep 17 00:00:00 2001 From: Kashey Date: Fri, 16 Oct 2026 19:37:27 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D0=B4=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D1=80=D0=B0=D1=81=D1=85=D0=BE=D0=B4=D0=BE=D0=B2=20=D0=BF=D0=BE?= =?UTF-8?q?=20=D1=81=D1=82=D1=80=D0=BE=D0=BA=D0=B0=D0=BC=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=BA=D1=83=D0=BC=D0=B5=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Main.java | 12 ++++++++++++ src/service/Ledger.java | 22 ++++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/Main.java b/src/Main.java index 16ea53c..fe625bf 100644 --- a/src/Main.java +++ b/src/Main.java @@ -19,6 +19,18 @@ public class Main { ledger.post(operation3); printAccountBalance(card, ledger); printAccountBalance(cash, ledger); + Operation operation4 = Operation.expense(LocalDate.now(), 401L, card); + OperationLine operationLine = new OperationLine(new Category(1L,"Coca cola"), new BigDecimal(200)); + operation4.addLine(operationLine); + operationLine = new OperationLine(new Category(2L,"Pizza"), new BigDecimal(800)); + operation4.addLine(operationLine); + ledger.post(operation4); + Operation operation5 = Operation.expense(LocalDate.now(), 404L, cash); + operationLine = new OperationLine(new Category(3L,"Mineral water"), new BigDecimal(60)); + operation5.addLine(operationLine); + ledger.post(operation5); + 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 d178397..6d47c73 100644 --- a/src/service/Ledger.java +++ b/src/service/Ledger.java @@ -5,6 +5,7 @@ import model.*; import java.math.BigDecimal; import java.util.ArrayList; import java.time.LocalDate; +import java.util.List; public class Ledger { private final ArrayList entries = new ArrayList<>(); @@ -21,15 +22,12 @@ public class Ledger { case OPENING_BALANCE: postOpeningBalance(operation); break; - case OUTFLOW: - //do something + postOutflow(operation); break; - case INFLOW: //do something break; - case MOVEMENT: postMovement(operation); break; @@ -38,6 +36,22 @@ public class Ledger { } } + private void postOutflow(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.getSourceAccount(), operationLine.getCategory(), operationLine.getAmount().negate()); + newEntries.add(ledgerEntry); + } + entries.removeIf(entry -> entry.getOperationId().equals(operation.getId())); + entries.addAll(newEntries); + } + private void postOpeningBalance(Operation operation) { ArrayList newEntries = new ArrayList<>();