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