From a1c4990243042980ce65d3a40ee8212b46c4d887 Mon Sep 17 00:00:00 2001 From: Kashey Date: Fri, 16 Oct 2026 13:16:49 +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=D0=B5=D1=80=D0=B5=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D0=B4?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D1=8B=D1=85=20=D0=BE=D1=81=D1=82=D0=B0=D1=82=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2,=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=B8=20=D1=80=D0=B0=D1=81=D1=87=D1=91=D1=82=20=D0=B8=D1=82?= =?UTF-8?q?=D0=BE=D0=B3=D0=B0=20=D0=BF=D0=BE=20=D1=81=D1=82=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=D0=B0=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Main.java | 28 +++++++---- src/model/Category.java | 5 ++ src/model/LedgerEntry.java | 21 ++++++-- src/model/Operation.java | 93 +++++++++++++++++++++++++++++++++--- src/model/OperationLine.java | 26 +++++++++- src/service/Ledger.java | 76 +++++++++++++++++++++++++++-- 6 files changed, 220 insertions(+), 29 deletions(-) diff --git a/src/Main.java b/src/Main.java index 930198c..a155382 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,15 +1,23 @@ -//TIP To Run code, press or -// click the icon in the gutter. +import model.*; +import service.Ledger; + +import java.math.BigDecimal; +import java.time.LocalDate; + public class Main { public static void main(String[] args) { - //TIP Press with your caret at the highlighted text - // to see how IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); + Account card = new Account(1L, "Сбер карта"); + Account cash = new Account(2L, "Наличные"); + 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); + printAccountBalance(card, ledger); + printAccountBalance(cash, ledger); + } - for (int i = 1; i <= 5; i++) { - //TIP Press to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . - System.out.println("i = " + i); - } + public static void printAccountBalance(Account account, Ledger ledger){ + System.out.println("Баланс " + account.getName() + " = " + ledger.balance(account, LocalDate.now())); } } \ No newline at end of file diff --git a/src/model/Category.java b/src/model/Category.java index 935c537..66e897f 100644 --- a/src/model/Category.java +++ b/src/model/Category.java @@ -19,4 +19,9 @@ public class Category { public void setName(String name) { this.name = name; } + + public Category(Long id, String name) { + this.id = id; + this.name = name; + } } diff --git a/src/model/LedgerEntry.java b/src/model/LedgerEntry.java index 48ced5d..476b465 100644 --- a/src/model/LedgerEntry.java +++ b/src/model/LedgerEntry.java @@ -4,17 +4,24 @@ import java.math.BigDecimal; import java.time.LocalDate; public class LedgerEntry { - private LocalDate date; - private Operation operation; - private Account account; + private final LocalDate date; + private final Long operationId; + private final Account account; private Category category; + private final BigDecimal deltaAmount; + + public LedgerEntry(LocalDate date, Long operationId, Account account, Category category, BigDecimal deltaAmount) { + this.date = date; + this.operationId = operationId; + this.account = account; + this.category = category; + this.deltaAmount = deltaAmount; + } public BigDecimal getDeltaAmount() { return deltaAmount; } - private BigDecimal deltaAmount; - public LocalDate getDate() { return date; } @@ -22,4 +29,8 @@ public class LedgerEntry { public Account getAccount() { return account; } + + public Long getOperationId() { + return operationId; + } } diff --git a/src/model/Operation.java b/src/model/Operation.java index c89a001..6ad0b8e 100644 --- a/src/model/Operation.java +++ b/src/model/Operation.java @@ -3,13 +3,92 @@ package model; import java.math.BigDecimal; import java.util.ArrayList; import java.time.LocalDate; +import java.util.Collections; +import java.util.List; public class Operation { - private LocalDate date; - private Long id; - private OperationType type; - private Account sourceAccount; - private Account destinationAccount; - private BigDecimal total; - private final ArrayList lines = new ArrayList<>(); + private final LocalDate date; + private final Long id; + private final OperationType type; + private final Account sourceAccount; + private final Account destinationAccount; + private final BigDecimal total; + private final List lines = new ArrayList<>(); + + private Operation(LocalDate date, Long id, OperationType type, Account sourceAccount, Account destinationAccount, BigDecimal total) { + this.date = date; + this.id = id; + this.type = type; + this.sourceAccount = sourceAccount; + this.destinationAccount = destinationAccount; + this.total = total; + } + + public LocalDate getDate() { + return date; + } + + public Long getId() { + return id; + } + + public OperationType getType() { + return type; + } + + public Account getSourceAccount() { + return sourceAccount; + } + + public Account getDestinationAccount() { + return destinationAccount; + } + + public BigDecimal getTotal() { + + if (type == OperationType.INFLOW || type == OperationType.OUTFLOW) { + BigDecimal result = BigDecimal.ZERO; + + for (OperationLine line : lines) { + result = result.add(line.getAmount()); + } + return result; + } + return total; + } + + public List getLines() { + return Collections.unmodifiableList(lines); + } + + public void addLine(OperationLine line) { + + if (line == null) { + throw new IllegalArgumentException("Не указана строка документа"); + } + + if (type != OperationType.INFLOW + && type != OperationType.OUTFLOW) { + throw new IllegalArgumentException("Строки разрешены только для доходов и расходов"); + } + + lines.add(line); + } + + public static Operation openingBalance(LocalDate date, Long id, Account destinationAccount, BigDecimal total) { + return new Operation(date, id, OperationType.OPENING_BALANCE, null, destinationAccount, total); + } + + public static Operation income(LocalDate date, Long id, Account destinationAccount) { + return new Operation(date, id, OperationType.INFLOW, null, destinationAccount, null); + } + + public static Operation expense(LocalDate date, Long id, Account sourceAccount) { + return new Operation(date, id, OperationType.OUTFLOW, sourceAccount, null, null); + } + + public static Operation transfer(LocalDate date, Long id, Account sourceAccount, Account destinationAccount, + BigDecimal total) { + return new Operation(date, id, OperationType.MOVEMENT, sourceAccount, destinationAccount, total); + } } diff --git a/src/model/OperationLine.java b/src/model/OperationLine.java index 4bfc212..d92d35f 100644 --- a/src/model/OperationLine.java +++ b/src/model/OperationLine.java @@ -3,6 +3,28 @@ package model; import java.math.BigDecimal; public class OperationLine { - private Category category; - private BigDecimal amount; + private final Category category; + private final BigDecimal amount; + + public OperationLine(Category category, BigDecimal amount) { + + if (category == null) { + throw new IllegalArgumentException("В строке не указана категория"); + } + + if (amount == null) { + throw new IllegalArgumentException("В строке не указана сумма"); + } + + this.category = category; + this.amount = amount; + } + + public Category getCategory() { + return category; + } + + public BigDecimal getAmount() { + return amount; + } } diff --git a/src/service/Ledger.java b/src/service/Ledger.java index 184ab33..c17d801 100644 --- a/src/service/Ledger.java +++ b/src/service/Ledger.java @@ -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 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; } }