Добавил проведение расходов по строкам документа
This commit is contained in:
1 parent
0775fef034
commit
9742b55e43
2 files changed
+30
-4
No files matched your search
@@ -19,6 +19,18 @@ public class Main {
|
||||
ledger.post(operation3);
|
||||
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);
|
||||
ledger.post(operation4);
|
||||
Operation operation5 = Operation.expense(LocalDate.now(), 404L, cash);
|
||||
operationLine = new OperationLine(new Category(3L,"Mineral water"), new BigDecimal(60));
|
||||
operation5.addLine(operationLine);
|
||||
ledger.post(operation5);
|
||||
printAccountBalance(card, ledger);
|
||||
printAccountBalance(cash, ledger);
|
||||
}
|
||||
|
||||
public static void printAccountBalance(Account account, Ledger ledger){
|
||||
|
||||
+18
-4
@@ -5,6 +5,7 @@ import model.*;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
public class Ledger {
|
||||
private final ArrayList<LedgerEntry> entries = new ArrayList<>();
|
||||
@@ -21,15 +22,12 @@ public class Ledger {
|
||||
case OPENING_BALANCE:
|
||||
postOpeningBalance(operation);
|
||||
break;
|
||||
|
||||
case OUTFLOW:
|
||||
//do something
|
||||
postOutflow(operation);
|
||||
break;
|
||||
|
||||
case INFLOW:
|
||||
//do something
|
||||
break;
|
||||
|
||||
case MOVEMENT:
|
||||
postMovement(operation);
|
||||
break;
|
||||
@@ -38,6 +36,22 @@ public class Ledger {
|
||||
}
|
||||
}
|
||||
|
||||
private void postOutflow(Operation operation) {
|
||||
|
||||
if (operation.getLines().isEmpty()) {
|
||||
throw new IllegalArgumentException("Документ расхода не содержит строк");
|
||||
}
|
||||
ArrayList<LedgerEntry> newEntries = new ArrayList<>();
|
||||
List<OperationLine> operationLines = operation.getLines();
|
||||
for (OperationLine operationLine : operationLines) {
|
||||
LedgerEntry ledgerEntry = new LedgerEntry(operation.getDate(), operation.getId(),
|
||||
operation.getSourceAccount(), operationLine.getCategory(), operationLine.getAmount().negate());
|
||||
newEntries.add(ledgerEntry);
|
||||
}
|
||||
entries.removeIf(entry -> entry.getOperationId().equals(operation.getId()));
|
||||
entries.addAll(newEntries);
|
||||
}
|
||||
|
||||
private void postOpeningBalance(Operation operation) {
|
||||
|
||||
ArrayList<LedgerEntry> newEntries = new ArrayList<>();
|
||||
|
||||
Reference in new issue
Block a user