Добавил журнал операций, сервис проведения и проверку состояния проведения
This commit is contained in:
1 parent
54723f5310
commit
c734a3fa05
4 files changed
+153
-13
No files matched your search
+23
-12
@@ -1,5 +1,6 @@
|
|||||||
import model.*;
|
import model.*;
|
||||||
import service.Ledger;
|
import service.Ledger;
|
||||||
|
import service.OperationService;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
@@ -11,36 +12,46 @@ public class Main {
|
|||||||
Operation operation1 = Operation.openingBalance(LocalDate.now(), 101L, card, new BigDecimal(15000));
|
Operation operation1 = Operation.openingBalance(LocalDate.now(), 101L, card, new BigDecimal(15000));
|
||||||
Operation operation2 = Operation.openingBalance(LocalDate.now(), 201L, cash, new BigDecimal(3000));
|
Operation operation2 = Operation.openingBalance(LocalDate.now(), 201L, cash, new BigDecimal(3000));
|
||||||
Ledger ledger = new Ledger();
|
Ledger ledger = new Ledger();
|
||||||
ledger.post(operation1);
|
OperationJournal journal = new OperationJournal();
|
||||||
ledger.post(operation2);
|
OperationService operationService = new OperationService(journal, ledger);
|
||||||
|
operationService.post(operation1);
|
||||||
|
operationService.post(operation2);
|
||||||
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));
|
||||||
ledger.post(operation3);
|
operationService.post(operation3);
|
||||||
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);
|
||||||
ledger.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);
|
||||||
ledger.post(operation5);
|
operationService.post(operation5);
|
||||||
printAccountBalance(card, ledger);
|
printAccountBalance(card, ledger);
|
||||||
printAccountBalance(cash, ledger);
|
printAccountBalance(cash, ledger);
|
||||||
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);
|
||||||
ledger.post(operation6);
|
operationService.post(operation6);
|
||||||
printAccountBalance(card, ledger);
|
printAccountBalance(card, ledger);
|
||||||
printAccountBalance(cash, ledger);
|
printAccountBalance(cash, ledger);
|
||||||
|
printPosted(operation6, operationService);
|
||||||
|
operationService.unpost(operation6.getId());
|
||||||
|
printAccountBalance(card, ledger);
|
||||||
|
printAccountBalance(cash, ledger);
|
||||||
|
printPosted(operation6, operationService);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void printAccountBalance(Account account, Ledger ledger){
|
public static void printAccountBalance(Account account, Ledger ledger) {
|
||||||
System.out.println("Баланс " + account.getName() + " = " + ledger.balance(account, LocalDate.now()));
|
System.out.println("Баланс " + account.getName() + " = " + ledger.balance(account, LocalDate.now()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void printPosted(Operation operation, OperationService operationService) {
|
||||||
|
System.out.println("Документ с id = " + operation.getId() + (operationService.isPosted(operation) ? " проведен" : " распроведен"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
+19
-1
@@ -8,7 +8,7 @@ import java.time.LocalDate;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Ledger {
|
public class Ledger {
|
||||||
private final ArrayList<LedgerEntry> entries = new ArrayList<>();
|
private final List<LedgerEntry> entries = new ArrayList<>();
|
||||||
|
|
||||||
public void post(Operation operation) {
|
public void post(Operation operation) {
|
||||||
|
|
||||||
@@ -36,6 +36,24 @@ public class Ledger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void unpost(Long operationId) {
|
||||||
|
|
||||||
|
if (operationId == null) {
|
||||||
|
throw new IllegalArgumentException("Не указан ID операции");
|
||||||
|
}
|
||||||
|
entries.removeIf(entry -> entry.getOperationId().equals(operationId));
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isPosted(Long operationId) {
|
||||||
|
|
||||||
|
if (operationId == null) {
|
||||||
|
throw new IllegalArgumentException("Не указан ID операции");
|
||||||
|
}
|
||||||
|
|
||||||
|
return entries.stream()
|
||||||
|
.anyMatch(entry -> entry.getOperationId().equals(operationId));
|
||||||
|
}
|
||||||
|
|
||||||
private void postOutflow(Operation operation) {
|
private void postOutflow(Operation operation) {
|
||||||
|
|
||||||
if (operation.getLines().isEmpty()) {
|
if (operation.getLines().isEmpty()) {
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package service;
|
||||||
|
|
||||||
|
import model.Operation;
|
||||||
|
import model.OperationJournal;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class OperationService {
|
||||||
|
|
||||||
|
private final OperationJournal journal;
|
||||||
|
private final Ledger ledger;
|
||||||
|
|
||||||
|
public OperationService(OperationJournal journal, Ledger ledger) {
|
||||||
|
|
||||||
|
this.journal = Objects.requireNonNull(
|
||||||
|
journal, "Не указан журнал операций"
|
||||||
|
);
|
||||||
|
this.ledger = Objects.requireNonNull(
|
||||||
|
ledger, "Не указан регистр движений"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save(Operation operation) {
|
||||||
|
journal.saveOperation(operation);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void post(Long operationId) {
|
||||||
|
post(requiredOperation(operationId));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void post(Operation operation) {
|
||||||
|
ledger.post(operation);
|
||||||
|
journal.saveOperation(operation);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unpost(Long operationId) {
|
||||||
|
ledger.unpost(requiredOperation(operationId).getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isPosted(Operation operation) {
|
||||||
|
if (operation == null) {
|
||||||
|
throw new IllegalArgumentException("Не указан документ");
|
||||||
|
}
|
||||||
|
return ledger.isPosted(operation.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Operation requiredOperation(Long operationId) {
|
||||||
|
|
||||||
|
if (operationId == null) {
|
||||||
|
throw new IllegalArgumentException("Не указан ID операции");
|
||||||
|
}
|
||||||
|
|
||||||
|
Operation operation = journal.getOperationById(operationId);
|
||||||
|
|
||||||
|
if (operation == null) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Операция не найдена: " + operationId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return operation;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user