Добавил замену и удаление строк операций с проверкой на null

This commit is contained in:
Kashey committed 2026-10-18 18:45:15 +03:00
1 parent c734a3fa05
commit d264ed6583
6 files changed
+77 -27

No files matched your search

+25 -1
View File
@@ -16,31 +16,54 @@ public class Main {
OperationService operationService = new OperationService(journal, ledger); OperationService operationService = new OperationService(journal, ledger);
operationService.post(operation1); operationService.post(operation1);
operationService.post(operation2); operationService.post(operation2);
System.out.println("Ввод начального остатка");
printAccountBalance(card, ledger); printAccountBalance(card, ledger);
printAccountBalance(cash, ledger); printAccountBalance(cash, ledger);
Operation operation3 = Operation.transfer(LocalDate.now(), 301L, card, cash, new BigDecimal(2000)); Operation operation3 = Operation.transfer(LocalDate.now(), 301L, card, cash, new BigDecimal(2000));
operationService.post(operation3); operationService.post(operation3);
System.out.println("Перемещение");
printAccountBalance(card, ledger); printAccountBalance(card, ledger);
printAccountBalance(cash, ledger); printAccountBalance(cash, ledger);
Operation operation4 = Operation.expense(LocalDate.now(), 401L, card); Operation operation4 = Operation.expense(LocalDate.now(), 401L, card);
OperationLine operationLine = new OperationLine(new Category(1L, "Coca cola"), new BigDecimal(200)); OperationLine operationLine = new OperationLine(new Category(1L, "Coca cola"), new BigDecimal(200));
operation4.addLine(operationLine); operation4.addLine(operationLine);
operationLine = new OperationLine(new Category(2L, "Pizza"), new BigDecimal(800)); operationLine = new OperationLine(new Category(2L, "Pizza"), new BigDecimal(800));
operation4.addLine(operationLine); operation4.addLine(operationLine);
operationService.post(operation4); operationService.post(operation4);
Operation operation5 = Operation.expense(LocalDate.now(), 404L, cash); Operation operation5 = Operation.expense(LocalDate.now(), 404L, cash);
operationLine = new OperationLine(new Category(3L, "Mineral water"), new BigDecimal(60)); operationLine = new OperationLine(new Category(3L, "Mineral water"), new BigDecimal(60));
operation5.addLine(operationLine); operation5.addLine(operationLine);
operationService.post(operation5); operationService.post(operation5);
System.out.println("Расход");
printAccountBalance(card, ledger); printAccountBalance(card, ledger);
printAccountBalance(cash, ledger); printAccountBalance(cash, ledger);
operationLine = new OperationLine(new Category(3L, "Mineral water"), new BigDecimal(100));
operation5.replaceLine(0, operationLine);
operationService.post(operation5);
System.out.println("Замена строки");
printAccountBalance(card, ledger);
printAccountBalance(cash, ledger);
// operation5.removeLine(0);
// operationService.post(operation5);
// System.out.println("Удаление строк");
Operation operation6 = Operation.income(LocalDate.now(), 504L, card); Operation operation6 = Operation.income(LocalDate.now(), 504L, card);
operationLine = new OperationLine(new Category(4L, "Salary"), new BigDecimal(20000)); operationLine = new OperationLine(new Category(4L, "Salary"), new BigDecimal(20000));
operation6.addLine(operationLine); operation6.addLine(operationLine);
operationService.post(operation6); operationService.post(operation6);
System.out.println("Приход");
printAccountBalance(card, ledger); printAccountBalance(card, ledger);
printAccountBalance(cash, ledger); printAccountBalance(cash, ledger);
printPosted(operation6, operationService); printPosted(operation6, operationService);
System.out.println("Распроведение прихода");
operationService.unpost(operation6.getId()); operationService.unpost(operation6.getId());
printAccountBalance(card, ledger); printAccountBalance(card, ledger);
printAccountBalance(cash, ledger); printAccountBalance(cash, ledger);
@@ -52,6 +75,7 @@ public class Main {
} }
public static void printPosted(Operation operation, OperationService operationService) { public static void printPosted(Operation operation, OperationService operationService) {
System.out.println("Документ с id = " + operation.getId() + (operationService.isPosted(operation) ? " проведен" : " распроведен")); System.out.println("Документ с id = " + operation.getId() +
(operationService.isPosted(operation) ? " проведен" : " не проведен"));
} }
} }
+15 -8
View File
@@ -4,16 +4,17 @@ import java.util.Objects;
public class Category { public class Category {
private String name; private String name;
private Long id; private final Long id;
public Category(Long id, String name) {
this.id = Objects.requireNonNull(id, "Не указан идентификатор");
setName(name);
}
public Long getId() { public Long getId() {
return id; return id;
} }
public void setId(Long id) {
this.id = id;
}
public String getName() { public String getName() {
return name; return name;
} }
@@ -26,9 +27,15 @@ public class Category {
this.name = name; this.name = name;
} }
public Category(Long id, String name) { @Override
this.id = Objects.requireNonNull(id, "Не указан идентификатор"); public boolean equals(Object o) {
setName(name); 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();
} }
} }
+17 -1
View File
@@ -7,10 +7,26 @@ public class LedgerEntry {
private final LocalDate date; private final LocalDate date;
private final Long operationId; private final Long operationId;
private final Account account; private final Account account;
private Category category; private final Category category;
private final BigDecimal deltaAmount; private final BigDecimal deltaAmount;
public LedgerEntry(LocalDate date, Long operationId, Account account, Category category, 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.date = date;
this.operationId = operationId; this.operationId = operationId;
this.account = account; this.account = account;
+14 -9
View File
@@ -16,7 +16,6 @@ public class Operation {
private final List<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) { private Operation(LocalDate date, Long id, OperationType type, Account sourceAccount, Account destinationAccount, BigDecimal total) {
if (id == null) { if (id == null) {
throw new IllegalArgumentException("Не указан ID документа"); throw new IllegalArgumentException("Не указан ID документа");
} }
@@ -28,6 +27,7 @@ public class Operation {
if (type == null) { if (type == null) {
throw new IllegalArgumentException("Не указан вид документа"); throw new IllegalArgumentException("Не указан вид документа");
} }
this.date = date; this.date = date;
this.id = id; this.id = id;
this.type = type; this.type = type;
@@ -57,7 +57,6 @@ public class Operation {
} }
public BigDecimal getTotal() { public BigDecimal getTotal() {
if (type == OperationType.INFLOW || type == OperationType.OUTFLOW) { if (type == OperationType.INFLOW || type == OperationType.OUTFLOW) {
BigDecimal result = BigDecimal.ZERO; BigDecimal result = BigDecimal.ZERO;
@@ -74,7 +73,6 @@ public class Operation {
} }
public void addLine(OperationLine line) { public void addLine(OperationLine line) {
if (line == null) { if (line == null) {
throw new IllegalArgumentException("Не указана строка документа"); throw new IllegalArgumentException("Не указана строка документа");
} }
@@ -87,8 +85,19 @@ public class Operation {
lines.add(line); lines.add(line);
} }
public static Operation openingBalance(LocalDate date, Long id, Account destinationAccount, BigDecimal total) { 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) { if (destinationAccount == null) {
throw new IllegalArgumentException("Не указан счёт-приёмник"); throw new IllegalArgumentException("Не указан счёт-приёмник");
} }
@@ -101,7 +110,6 @@ public class Operation {
} }
public static Operation income(LocalDate date, Long id, Account destinationAccount) { public static Operation income(LocalDate date, Long id, Account destinationAccount) {
if (destinationAccount == null) { if (destinationAccount == null) {
throw new IllegalArgumentException("Не указан счёт-приёмник"); throw new IllegalArgumentException("Не указан счёт-приёмник");
} }
@@ -110,7 +118,6 @@ public class Operation {
} }
public static Operation expense(LocalDate date, Long id, Account sourceAccount) { public static Operation expense(LocalDate date, Long id, Account sourceAccount) {
if (sourceAccount == null) { if (sourceAccount == null) {
throw new IllegalArgumentException("Не указан счёт-источник"); throw new IllegalArgumentException("Не указан счёт-источник");
} }
@@ -118,9 +125,7 @@ public class Operation {
return new Operation(date, id, OperationType.OUTFLOW, sourceAccount, null, null); return new Operation(date, id, OperationType.OUTFLOW, sourceAccount, null, null);
} }
public static Operation transfer(LocalDate date, Long id, Account sourceAccount, Account destinationAccount, public static Operation transfer(LocalDate date, Long id, Account sourceAccount, Account destinationAccount, BigDecimal total) {
BigDecimal total) {
if (sourceAccount == null) { if (sourceAccount == null) {
throw new IllegalArgumentException("Не указан счёт-источник"); throw new IllegalArgumentException("Не указан счёт-источник");
} }
+6 -7
View File
@@ -11,7 +11,6 @@ public class Ledger {
private final List<LedgerEntry> entries = new ArrayList<>(); private final List<LedgerEntry> entries = new ArrayList<>();
public void post(Operation operation) { public void post(Operation operation) {
if (operation == null) { if (operation == null) {
throw new IllegalArgumentException("Не указан документ"); throw new IllegalArgumentException("Не указан документ");
} }
@@ -37,7 +36,6 @@ public class Ledger {
} }
public void unpost(Long operationId) { public void unpost(Long operationId) {
if (operationId == null) { if (operationId == null) {
throw new IllegalArgumentException("Не указан ID операции"); throw new IllegalArgumentException("Не указан ID операции");
} }
@@ -45,7 +43,6 @@ public class Ledger {
} }
public boolean isPosted(Long operationId) { public boolean isPosted(Long operationId) {
if (operationId == null) { if (operationId == null) {
throw new IllegalArgumentException("Не указан ID операции"); throw new IllegalArgumentException("Не указан ID операции");
} }
@@ -55,7 +52,6 @@ public class Ledger {
} }
private void postOutflow(Operation operation) { private void postOutflow(Operation operation) {
if (operation.getLines().isEmpty()) { if (operation.getLines().isEmpty()) {
throw new IllegalArgumentException("Документ расхода не содержит строк"); throw new IllegalArgumentException("Документ расхода не содержит строк");
} }
@@ -71,7 +67,6 @@ public class Ledger {
} }
private void postInflow(Operation operation) { private void postInflow(Operation operation) {
if (operation.getLines().isEmpty()) { if (operation.getLines().isEmpty()) {
throw new IllegalArgumentException("Документ прихода не содержит строк"); throw new IllegalArgumentException("Документ прихода не содержит строк");
} }
@@ -87,7 +82,6 @@ public class Ledger {
} }
private void postOpeningBalance(Operation operation) { private void postOpeningBalance(Operation operation) {
ArrayList<LedgerEntry> newEntries = new ArrayList<>(); ArrayList<LedgerEntry> newEntries = new ArrayList<>();
LedgerEntry ledgerEntry = new LedgerEntry(operation.getDate(), operation.getId(), LedgerEntry ledgerEntry = new LedgerEntry(operation.getDate(), operation.getId(),
operation.getDestinationAccount(), null, operation.getTotal()); operation.getDestinationAccount(), null, operation.getTotal());
@@ -97,7 +91,6 @@ public class Ledger {
} }
private void postMovement(Operation operation) { private void postMovement(Operation operation) {
ArrayList<LedgerEntry> newEntries = new ArrayList<>(); ArrayList<LedgerEntry> newEntries = new ArrayList<>();
LedgerEntry ledgerEntryExpense = new LedgerEntry(operation.getDate(), operation.getId(), LedgerEntry ledgerEntryExpense = new LedgerEntry(operation.getDate(), operation.getId(),
operation.getSourceAccount(), null, operation.getTotal().negate()); operation.getSourceAccount(), null, operation.getTotal().negate());
@@ -111,7 +104,13 @@ public class Ledger {
} }
public BigDecimal balance(Account account, LocalDate date) { public BigDecimal balance(Account account, LocalDate date) {
if (account == null) {
throw new IllegalArgumentException("Не указан счёт");
}
if (date == null) {
throw new IllegalArgumentException("Не указана дата расчёта остатка");
}
BigDecimal result = BigDecimal.ZERO; BigDecimal result = BigDecimal.ZERO;
for (LedgerEntry entry : entries) { for (LedgerEntry entry : entries) {
-1
View File
@@ -57,7 +57,6 @@ public class OperationService {
"Операция не найдена: " + operationId "Операция не найдена: " + operationId
); );
} }
return operation; return operation;
} }
} }