Добавил проведение перемещений и перенес проверки операций в модель

This commit is contained in:
Kashey committed 2026-10-16 16:41:44 +03:00
1 parent a1c4990243
commit 0775fef034
5 files changed
+85 -33

No files matched your search

+4
View File
@@ -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){
+9 -2
View File
@@ -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);
}
}
+57
View File
@@ -16,6 +16,18 @@ public class Operation {
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;
@@ -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);
}
}
+4
View File
@@ -16,6 +16,10 @@ public class OperationLine {
throw new IllegalArgumentException("В строке не указана сумма");
}
if (amount.signum() <= 0) {
throw new IllegalArgumentException("Сумма строки должна быть больше нуля");
}
this.category = category;
this.amount = amount;
}
+11 -31
View File
@@ -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<LedgerEntry> 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<LedgerEntry> 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) {