Добавил перепроведение начальных остатков, проверки и расчёт итога по строкам

This commit is contained in:
Kashey committed 2026-10-16 13:16:49 +03:00
1 parent 357f494a56
commit a1c4990243
6 files changed
+220 -29

No files matched your search

+18 -10
View File
@@ -1,15 +1,23 @@
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or import model.*;
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter. import service.Ledger;
import java.math.BigDecimal;
import java.time.LocalDate;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text Account card = new Account(1L, "Сбер карта");
// to see how IntelliJ IDEA suggests fixing it. Account cash = new Account(2L, "Наличные");
System.out.printf("Hello and welcome!"); 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++) { public static void printAccountBalance(Account account, Ledger ledger){
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint System.out.println("Баланс " + account.getName() + " = " + ledger.balance(account, LocalDate.now()));
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
System.out.println("i = " + i);
}
} }
} }
+5
View File
@@ -19,4 +19,9 @@ public class Category {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public Category(Long id, String name) {
this.id = id;
this.name = name;
}
} }
+16 -5
View File
@@ -4,17 +4,24 @@ import java.math.BigDecimal;
import java.time.LocalDate; import java.time.LocalDate;
public class LedgerEntry { public class LedgerEntry {
private LocalDate date; private final LocalDate date;
private Operation operation; private final Long operationId;
private Account account; private final Account account;
private Category category; 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() { public BigDecimal getDeltaAmount() {
return deltaAmount; return deltaAmount;
} }
private BigDecimal deltaAmount;
public LocalDate getDate() { public LocalDate getDate() {
return date; return date;
} }
@@ -22,4 +29,8 @@ public class LedgerEntry {
public Account getAccount() { public Account getAccount() {
return account; return account;
} }
public Long getOperationId() {
return operationId;
}
} }
+86 -7
View File
@@ -3,13 +3,92 @@ package model;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
public class Operation { public class Operation {
private LocalDate date; private final LocalDate date;
private Long id; private final Long id;
private OperationType type; private final OperationType type;
private Account sourceAccount; private final Account sourceAccount;
private Account destinationAccount; private final Account destinationAccount;
private BigDecimal total; private final BigDecimal total;
private final ArrayList<OperationLine> lines = new ArrayList<>(); private final List<OperationLine> 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<OperationLine> 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);
}
} }
+24 -2
View File
@@ -3,6 +3,28 @@ package model;
import java.math.BigDecimal; import java.math.BigDecimal;
public class OperationLine { public class OperationLine {
private Category category; private final Category category;
private BigDecimal amount; 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;
}
} }
+71 -5
View File
@@ -1,8 +1,6 @@
package service; package service;
import model.Account; import model.*;
import model.LedgerEntry;
import model.Operation;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
@@ -13,13 +11,82 @@ public class Ledger {
public void post(Operation operation) { 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<LedgerEntry> 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) { public BigDecimal balance(Account account, LocalDate date) {
BigDecimal result = BigDecimal.ZERO; BigDecimal result = BigDecimal.ZERO;
for (LedgerEntry entry: entries) { for (LedgerEntry entry : entries) {
boolean sameAccount = entry.getAccount().equals(account); boolean sameAccount = entry.getAccount().equals(account);
boolean withinPeriod = !entry.getDate().isAfter(date); boolean withinPeriod = !entry.getDate().isAfter(date);
@@ -27,7 +94,6 @@ public class Ledger {
result = result.add(entry.getDeltaAmount()); result = result.add(entry.getDeltaAmount());
} }
} }
return result; return result;
} }
} }