Skip to content

Commit fd43a5a

Browse files
author
Holger Harms
committed
Print cost centre expenditures in CSV report
1 parent 5418782 commit fd43a5a

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

src/main/java/de/holhar/accounting/adapter/CliAdapter.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import de.holhar.accounting.config.AppProperties;
44
import de.holhar.accounting.domain.AnnualReport;
5+
import de.holhar.accounting.domain.CostCentre;
56
import de.holhar.accounting.domain.MonthlyReport;
67
import de.holhar.accounting.service.AccountingService;
78
import org.apache.commons.csv.CSVFormat;
@@ -24,8 +25,10 @@
2425
import java.util.ArrayList;
2526
import java.util.List;
2627
import java.util.Map;
28+
import java.util.Optional;
2729
import java.util.Scanner;
2830
import java.util.Set;
31+
import java.util.stream.Collectors;
2932

3033
@Component
3134
public class CliAdapter {
@@ -75,7 +78,8 @@ private void createCSVReport(Path csvPath, Map<Integer, Set<MonthlyReport>> mont
7578
Path reportFile = csvPath.getParent().resolve(Paths.get(fileName));
7679
try (BufferedWriter writer = Files.newBufferedWriter(reportFile);
7780
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT
78-
.withHeader("ID", "Year", "Month", "Income", "Expenditure", "Win", "SavingRate"))
81+
.withHeader("ID", "Year", "Month", "Income", "Expenditure", "Win", "SavingRate", "####",
82+
"Accommodation", "Food", "Health", "Transportation", "Leisure", "Misc"))
7983
) {
8084
List<AnnualReport> annualReportList = new ArrayList<>();
8185
for (Map.Entry<Integer, Set<MonthlyReport>> entry : monthlyReportsPerYear.entrySet()) {
@@ -91,7 +95,7 @@ private void createCSVReport(Path csvPath, Map<Integer, Set<MonthlyReport>> mont
9195
String expenditureString = df.format(a.getExpenditure());
9296
String winString = df.format(a.getWin());
9397
String savingRateString = df.format(a.getSavingRate());
94-
csvPrinter.printRecord(a.getFriendlyName(), a.getYear(), "--", incomeString, expenditureString, winString, savingRateString);
98+
csvPrinter.printRecord(a.getFriendlyName(), a.getYear(), "----", incomeString, expenditureString, winString, savingRateString);
9599
}
96100
csvPrinter.flush();
97101
}
@@ -103,8 +107,36 @@ private void processReports(CSVPrinter csvPrinter, Set<MonthlyReport> reportSet,
103107
String expenditureString = df.format(r.getExpenditure());
104108
String varString = df.format(r.getWin());
105109
String savingRateString = df.format(r.getSavingRate());
106-
csvPrinter.printRecord(r.getFriendlyName(), r.getYear(), r.getMonth(), incomeString, expenditureString, varString, savingRateString);
110+
Map<CostCentre.Type, List<CostCentre>> cMap = getCostCentresAsMap(r.getCostCentres());
111+
csvPrinter.printRecord(
112+
r.getFriendlyName(),
113+
r.getYear(),
114+
r.getMonth(),
115+
incomeString,
116+
expenditureString,
117+
varString,
118+
savingRateString,
119+
"----",
120+
getCostCentreAmountFormatted(cMap, CostCentre.Type.ACCOMMODATION),
121+
getCostCentreAmountFormatted(cMap, CostCentre.Type.FOOD),
122+
getCostCentreAmountFormatted(cMap, CostCentre.Type.HEALTH),
123+
getCostCentreAmountFormatted(cMap, CostCentre.Type.TRANSPORTATION),
124+
getCostCentreAmountFormatted(cMap, CostCentre.Type.LEISURE_ACTIVITIES_AND_PURCHASES),
125+
getCostCentreAmountFormatted(cMap, CostCentre.Type.MISCELLANEOUS)
126+
);
107127
annualReport.addProfitAndExpenses(r.getIncome(), r.getExpenditure());
108128
}
109129
}
130+
131+
private Map<CostCentre.Type, List<CostCentre>> getCostCentresAsMap(Set<CostCentre> costCentres) {
132+
return costCentres.stream().collect(Collectors.groupingBy(CostCentre::getType));
133+
}
134+
135+
private String getCostCentreAmountFormatted(Map<CostCentre.Type, List<CostCentre>> cMap, CostCentre.Type type) {
136+
if (cMap.get(type) != null) {
137+
BigDecimal amount = Optional.ofNullable(cMap.get(type).get(0)).orElse(new CostCentre(type)).getAmount();
138+
return df.format(amount);
139+
}
140+
return "0";
141+
}
110142
}

0 commit comments

Comments
 (0)