From 0775fef034b430acebc851ac92070eef2b40964a Mon Sep 17 00:00:00 2001 From: Kashey Date: Fri, 16 Oct 2026 16:41:44 +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?=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5=D1=89=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B9=20=D0=B8=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BD=D0=B5=D1=81=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20=D0=BE=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D0=B9=20=D0=B2=20=D0=BC=D0=BE?= =?UTF-8?q?=D0=B4=D0=B5=D0=BB=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Main.java | 4 +++ src/model/Category.java | 11 +++++-- src/model/Operation.java | 57 ++++++++++++++++++++++++++++++++++++ src/model/OperationLine.java | 4 +++ src/service/Ledger.java | 42 +++++++------------------- 5 files changed, 85 insertions(+), 33 deletions(-) diff --git a/src/Main.java b/src/Main.java index a155382..16ea53c 100644 --- a/src/Main.java +++ b/src/Main.java @@ -15,6 +15,10 @@ public class Main { ledger.post(operation2); printAccountBalance(card, ledger); printAccountBalance(cash, ledger); + Operation operation3 = Operation.transfer(LocalDate.now(), 301L, card, cash, new BigDecimal(2000)); + ledger.post(operation3); + printAccountBalance(card, ledger); + printAccountBalance(cash, ledger); } public static void printAccountBalance(Account account, Ledger ledger){ diff --git a/src/model/Category.java b/src/model/Category.java index 66e897f..df99669 100644 --- a/src/model/Category.java +++ b/src/model/Category.java @@ -1,5 +1,7 @@ package model; +import java.util.Objects; + public class Category { private String name; private Long id; @@ -17,11 +19,16 @@ public class Category { } public void setName(String name) { + if (name == null || name.isBlank()) { + throw new IllegalArgumentException("Не указано имя категории"); + } + this.name = name; } public Category(Long id, String name) { - this.id = id; - this.name = name; + this.id = Objects.requireNonNull(id, "Не указан идентификатор"); + setName(name); + } } diff --git a/src/model/Operation.java b/src/model/Operation.java index 6ad0b8e..f2f99ef 100644 --- a/src/model/Operation.java +++ b/src/model/Operation.java @@ -16,6 +16,18 @@ public class Operation { private final List lines = new ArrayList<>(); private Operation(LocalDate date, Long id, OperationType type, Account sourceAccount, Account destinationAccount, BigDecimal total) { + + if (id == null) { + throw new IllegalArgumentException("Не указан ID документа"); + } + + if (date == null) { + throw new IllegalArgumentException("Не указана дата документа"); + } + + if (type == null) { + throw new IllegalArgumentException("Не указан вид документа"); + } this.date = date; this.id = id; this.type = type; @@ -76,19 +88,64 @@ public class Operation { } public static Operation openingBalance(LocalDate date, Long id, Account destinationAccount, BigDecimal total) { + + if (destinationAccount == null) { + throw new IllegalArgumentException("Не указан счёт-приёмник"); + } + + if (total == null) { + throw new IllegalArgumentException("Не указана сумма документа"); + } + return new Operation(date, id, OperationType.OPENING_BALANCE, null, destinationAccount, total); } public static Operation income(LocalDate date, Long id, Account destinationAccount) { + + if (destinationAccount == null) { + throw new IllegalArgumentException("Не указан счёт-приёмник"); + } + return new Operation(date, id, OperationType.INFLOW, null, destinationAccount, null); } public static Operation expense(LocalDate date, Long id, Account sourceAccount) { + + if (sourceAccount == null) { + throw new IllegalArgumentException("Не указан счёт-источник"); + } + return new Operation(date, id, OperationType.OUTFLOW, sourceAccount, null, null); } public static Operation transfer(LocalDate date, Long id, Account sourceAccount, Account destinationAccount, BigDecimal total) { + + if (sourceAccount == null) { + throw new IllegalArgumentException("Не указан счёт-источник"); + } + + if (destinationAccount == null) { + throw new IllegalArgumentException("Не указан счёт-приёмник"); + } + + if (sourceAccount + .equals(destinationAccount)) { + throw new IllegalArgumentException( + "Счёт-источник и счёт-приёмник должны различаться" + ); + } + + if (total == null) { + throw new IllegalArgumentException("Не указана сумма документа"); + } + + if (total.signum() <= 0) { + throw new IllegalArgumentException( + "Сумма перемещения должна быть больше нуля" + ); + } + return new Operation(date, id, OperationType.MOVEMENT, sourceAccount, destinationAccount, total); } } diff --git a/src/model/OperationLine.java b/src/model/OperationLine.java index d92d35f..8d68df5 100644 --- a/src/model/OperationLine.java +++ b/src/model/OperationLine.java @@ -16,6 +16,10 @@ public class OperationLine { throw new IllegalArgumentException("В строке не указана сумма"); } + if (amount.signum() <= 0) { + throw new IllegalArgumentException("Сумма строки должна быть больше нуля"); + } + this.category = category; this.amount = amount; } diff --git a/src/service/Ledger.java b/src/service/Ledger.java index c17d801..d178397 100644 --- a/src/service/Ledger.java +++ b/src/service/Ledger.java @@ -15,10 +15,6 @@ public class Ledger { throw new IllegalArgumentException("Не указан документ"); } - if (operation.getType() == null) { - throw new IllegalArgumentException("Не указан тип операции"); - } - OperationType operationType = operation.getType(); switch (operationType) { @@ -35,7 +31,7 @@ public class Ledger { break; case MOVEMENT: - //do something + postMovement(operation); break; default: //do something @@ -44,7 +40,6 @@ public class Ledger { private void postOpeningBalance(Operation operation) { - validateOpeningBalance(operation); ArrayList newEntries = new ArrayList<>(); LedgerEntry ledgerEntry = new LedgerEntry(operation.getDate(), operation.getId(), operation.getDestinationAccount(), null, operation.getTotal()); @@ -53,33 +48,18 @@ public class Ledger { entries.addAll(newEntries); } - private void validateOpeningBalance(Operation operation) { + private void postMovement(Operation operation) { - if (operation == null) { - throw new IllegalArgumentException("Не указан документ"); - } + ArrayList newEntries = new ArrayList<>(); + LedgerEntry ledgerEntryExpense = new LedgerEntry(operation.getDate(), operation.getId(), + operation.getSourceAccount(), null, operation.getTotal().negate()); + newEntries.add(ledgerEntryExpense); + LedgerEntry ledgerEntryIncome = new LedgerEntry(operation.getDate(), operation.getId(), + operation.getDestinationAccount(), null, operation.getTotal()); + newEntries.add(ledgerEntryIncome); - 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("Не указана сумма документа"); - } + entries.removeIf(entry -> entry.getOperationId().equals(operation.getId())); + entries.addAll(newEntries); } public BigDecimal balance(Account account, LocalDate date) {