Добавил тесты проведения, повторного проведения и изменения сумм операций

This commit is contained in:
Kashey committed 2026-10-19 14:05:42 +03:00
1 parent d264ed6583
commit 8af851a49a
18 files changed
+303 -35

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
package model;
import java.util.Objects;
public class Account {
private String name;
private final Long id;
public Account(Long id, String name) {
this.id = Objects.requireNonNull(id, "Не указан идентификатор");
setName(name);
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Не указано имя счёта");
}
this.name = name;
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Account account = (Account) o;
return Objects.equals(id, account.id);
}
@Override
public int hashCode() {
return id.hashCode();
}
}
+41
View File
@@ -0,0 +1,41 @@
package model;
import java.util.Objects;
public class Category {
private String name;
private final Long id;
public Category(Long id, String name) {
this.id = Objects.requireNonNull(id, "Не указан идентификатор");
setName(name);
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Не указано имя категории");
}
this.name = name;
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Category category = (Category) o;
return Objects.equals(id, category.id);
}
@Override
public int hashCode() {
return id.hashCode();
}
}
+52
View File
@@ -0,0 +1,52 @@
package model;
import java.math.BigDecimal;
import java.time.LocalDate;
public class LedgerEntry {
private final LocalDate date;
private final Long operationId;
private final Account account;
private final Category category;
private final BigDecimal deltaAmount;
public LedgerEntry(LocalDate date, Long operationId, Account account, Category category, BigDecimal deltaAmount) {
if (date == null) {
throw new IllegalArgumentException("Не указана дата движения");
}
if (operationId == null) {
throw new IllegalArgumentException("Не указан ID операции");
}
if (account == null) {
throw new IllegalArgumentException("Не указан счёт движения");
}
if (deltaAmount == null) {
throw new IllegalArgumentException("Не указана сумма движения");
}
this.date = date;
this.operationId = operationId;
this.account = account;
this.category = category;
this.deltaAmount = deltaAmount;
}
public BigDecimal getDeltaAmount() {
return deltaAmount;
}
public LocalDate getDate() {
return date;
}
public Account getAccount() {
return account;
}
public Long getOperationId() {
return operationId;
}
}
+156
View File
@@ -0,0 +1,156 @@
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 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<OperationLine> 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;
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 void replaceLine(int index, OperationLine line) {
if (line == null) {
throw new IllegalArgumentException("Не указана строка документа");
}
lines.set(index, line);
}
public void removeLine(int index) {
lines.remove(index);
}
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);
}
}
+48
View File
@@ -0,0 +1,48 @@
package model;
import java.time.LocalDate;
import java.util.*;
public class OperationJournal {
private final Map<Long, Operation> 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<Operation> getOperationsBetweenDates(LocalDate dateFrom, LocalDate dateTo) {
if (dateFrom == null || dateTo == null) {
throw new IllegalArgumentException(
"Необходимо указать начало и конец периода"
);
}
if (dateFrom.isAfter(dateTo)) {
throw new IllegalArgumentException(
"Начало периода не может быть позже конца"
);
}
List<Operation> 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;
}
}
+34
View File
@@ -0,0 +1,34 @@
package model;
import java.math.BigDecimal;
public class OperationLine {
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("В строке не указана сумма");
}
if (amount.signum() <= 0) {
throw new IllegalArgumentException("Сумма строки должна быть больше нуля");
}
this.category = category;
this.amount = amount;
}
public Category getCategory() {
return category;
}
public BigDecimal getAmount() {
return amount;
}
}
+8
View File
@@ -0,0 +1,8 @@
package model;
public enum OperationType {
OPENING_BALANCE,
INFLOW,
OUTFLOW,
MOVEMENT
}