From c734a3fa059d19250c740d19f9464ff43f4f6b44 Mon Sep 17 00:00:00 2001 From: Kashey Date: Sun, 18 Oct 2026 15:46:45 +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=B6=D1=83=D1=80=D0=BD=D0=B0=D0=BB=20=D0=BE=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D0=B9,=20=D1=81=D0=B5=D1=80=D0=B2=D0=B8?= =?UTF-8?q?=D1=81=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D0=B4=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B8=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D1=83=20=D1=81=D0=BE=D1=81=D1=82=D0=BE=D1=8F=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D0=B4=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Main.java | 35 +++++++++++------ src/model/OperationJournal.java | 48 +++++++++++++++++++++++ src/service/Ledger.java | 20 +++++++++- src/service/OperationService.java | 63 +++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 13 deletions(-) create mode 100644 src/model/OperationJournal.java create mode 100644 src/service/OperationService.java diff --git a/src/Main.java b/src/Main.java index 6de23ff..b1ec5bc 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,6 @@ import model.*; import service.Ledger; +import service.OperationService; import java.math.BigDecimal; import java.time.LocalDate; @@ -11,36 +12,46 @@ public class Main { Operation operation1 = Operation.openingBalance(LocalDate.now(), 101L, card, new BigDecimal(15000)); Operation operation2 = Operation.openingBalance(LocalDate.now(), 201L, cash, new BigDecimal(3000)); Ledger ledger = new Ledger(); - ledger.post(operation1); - ledger.post(operation2); + OperationJournal journal = new OperationJournal(); + OperationService operationService = new OperationService(journal, ledger); + operationService.post(operation1); + operationService.post(operation2); printAccountBalance(card, ledger); printAccountBalance(cash, ledger); Operation operation3 = Operation.transfer(LocalDate.now(), 301L, card, cash, new BigDecimal(2000)); - ledger.post(operation3); + operationService.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)); + 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)); + operationLine = new OperationLine(new Category(2L, "Pizza"), new BigDecimal(800)); operation4.addLine(operationLine); - ledger.post(operation4); + operationService.post(operation4); Operation operation5 = Operation.expense(LocalDate.now(), 404L, cash); - operationLine = new OperationLine(new Category(3L,"Mineral water"), new BigDecimal(60)); + operationLine = new OperationLine(new Category(3L, "Mineral water"), new BigDecimal(60)); operation5.addLine(operationLine); - ledger.post(operation5); + operationService.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)); + operationLine = new OperationLine(new Category(4L, "Salary"), new BigDecimal(20000)); operation6.addLine(operationLine); - ledger.post(operation6); + operationService.post(operation6); printAccountBalance(card, ledger); printAccountBalance(cash, ledger); - + printPosted(operation6, operationService); + operationService.unpost(operation6.getId()); + printAccountBalance(card, ledger); + printAccountBalance(cash, ledger); + printPosted(operation6, operationService); } - public static void printAccountBalance(Account account, Ledger ledger){ + public static void printAccountBalance(Account account, Ledger ledger) { System.out.println("Баланс " + account.getName() + " = " + ledger.balance(account, LocalDate.now())); } + + public static void printPosted(Operation operation, OperationService operationService) { + System.out.println("Документ с id = " + operation.getId() + (operationService.isPosted(operation) ? " проведен" : " распроведен")); + } } \ No newline at end of file diff --git a/src/model/OperationJournal.java b/src/model/OperationJournal.java new file mode 100644 index 0000000..31fefb1 --- /dev/null +++ b/src/model/OperationJournal.java @@ -0,0 +1,48 @@ +package model; + +import java.time.LocalDate; +import java.util.*; + +public class OperationJournal { + private final Map operations = new HashMap<>(); + + public void saveOperation(Operation operation) { + + if (operation == null) { + throw new IllegalArgumentException("Не указана операция для сохранения в журнал"); + } + + operations.put(operation.getId(), operation); + } + + public Operation getOperationById(Long id) { + return operations.get(id); + } + + public List getOperationsBetweenDates(LocalDate dateFrom, LocalDate dateTo) { + + if (dateFrom == null || dateTo == null) { + throw new IllegalArgumentException( + "Необходимо указать начало и конец периода" + ); + } + + if (dateFrom.isAfter(dateTo)) { + throw new IllegalArgumentException( + "Начало периода не может быть позже конца" + ); + } + + List result = new ArrayList<>(); + + for (Operation operation : operations.values()) { + if (!operation.getDate().isBefore(dateFrom) && !operation.getDate().isAfter(dateTo)) { + result.add(operation); + } + } + + result.sort(Comparator.comparing(Operation::getDate).thenComparing(Operation::getId)); + + return result; + } +} diff --git a/src/service/Ledger.java b/src/service/Ledger.java index 816ba37..985f81b 100644 --- a/src/service/Ledger.java +++ b/src/service/Ledger.java @@ -8,7 +8,7 @@ import java.time.LocalDate; import java.util.List; public class Ledger { - private final ArrayList entries = new ArrayList<>(); + private final List entries = new ArrayList<>(); public void post(Operation operation) { @@ -36,6 +36,24 @@ public class Ledger { } } + public void unpost(Long operationId) { + + if (operationId == null) { + throw new IllegalArgumentException("Не указан ID операции"); + } + entries.removeIf(entry -> entry.getOperationId().equals(operationId)); + } + + public boolean isPosted(Long operationId) { + + if (operationId == null) { + throw new IllegalArgumentException("Не указан ID операции"); + } + + return entries.stream() + .anyMatch(entry -> entry.getOperationId().equals(operationId)); + } + private void postOutflow(Operation operation) { if (operation.getLines().isEmpty()) { diff --git a/src/service/OperationService.java b/src/service/OperationService.java new file mode 100644 index 0000000..8c4db7f --- /dev/null +++ b/src/service/OperationService.java @@ -0,0 +1,63 @@ +package service; + +import model.Operation; +import model.OperationJournal; + +import java.util.Objects; + +public class OperationService { + + private final OperationJournal journal; + private final Ledger ledger; + + public OperationService(OperationJournal journal, Ledger ledger) { + + this.journal = Objects.requireNonNull( + journal, "Не указан журнал операций" + ); + this.ledger = Objects.requireNonNull( + ledger, "Не указан регистр движений" + ); + } + + public void save(Operation operation) { + journal.saveOperation(operation); + } + + public void post(Long operationId) { + post(requiredOperation(operationId)); + } + + public void post(Operation operation) { + ledger.post(operation); + journal.saveOperation(operation); + } + + public void unpost(Long operationId) { + ledger.unpost(requiredOperation(operationId).getId()); + } + + public boolean isPosted(Operation operation) { + if (operation == null) { + throw new IllegalArgumentException("Не указан документ"); + } + return ledger.isPosted(operation.getId()); + } + + private Operation requiredOperation(Long operationId) { + + if (operationId == null) { + throw new IllegalArgumentException("Не указан ID операции"); + } + + Operation operation = journal.getOperationById(operationId); + + if (operation == null) { + throw new IllegalArgumentException( + "Операция не найдена: " + operationId + ); + } + + return operation; + } +}