-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
116 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/de/lgohlke/homebanking/AccountStatusSummaryWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package de.lgohlke.homebanking; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.FileVisitResult; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.SimpleFileVisitor; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
@RequiredArgsConstructor | ||
@Slf4j | ||
class AccountStatusSummaryWriter { | ||
private static final String SUMMARY_CSV = "summary.csv"; | ||
private final Path baseDir; | ||
|
||
@SneakyThrows | ||
public void writeSummaryToCSV() { | ||
Collection<AccountStatus> statuses = collectStatuses(); | ||
AccountStatusCSVConverter converter = new AccountStatusCSVConverter(); | ||
List<String> lines = converter.convert(statuses); | ||
var statusLines = String.join("\n", lines); | ||
try { | ||
log.info("writing {}", SUMMARY_CSV); | ||
Files.writeString(baseDir.resolve(SUMMARY_CSV), statusLines); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private List<AccountStatus> collectStatuses() throws IOException { | ||
List<AccountStatus> accountStatuses = new ArrayList<>(); | ||
SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<>() { | ||
@Override | ||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { | ||
if (attrs.isRegularFile() && file.toString().endsWith(".csv")) { | ||
List<String> lines = Files.readAllLines(file); | ||
var status = new AccountStatusCSVConverter().convert(lines); | ||
accountStatuses.add(status); | ||
} | ||
return super.visitFile(file, attrs); | ||
} | ||
}; | ||
Files.walkFileTree(baseDir, visitor); | ||
|
||
return keepOneStatusPerDateAndIBAN(accountStatuses); | ||
} | ||
|
||
private static List<AccountStatus> keepOneStatusPerDateAndIBAN(List<AccountStatus> accountStatuses) { | ||
return accountStatuses | ||
.stream() | ||
.map(status -> { | ||
String key = status.date() + "_" + status.iban(); | ||
return Map.entry(key, | ||
status); | ||
}) | ||
.collect(Collectors.groupingBy(Map.Entry::getKey)) | ||
.values() | ||
.stream() | ||
.map(List::getFirst) | ||
.map(Map.Entry::getValue) | ||
.toList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/test/java/de/lgohlke/homebanking/AccountStatusSummaryWriterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package de.lgohlke.homebanking; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class AccountStatusSummaryWriterTest { | ||
@Test | ||
void name(@TempDir Path tempdir) throws IOException { | ||
List<AccountStatus> statuses = List.of( | ||
AccountStatus.parse("2024-08-27", "DE75 5001 0517 2221 8623 18", "2123,54 €", "Giro1"), | ||
AccountStatus.parse("2024-08-28", "DE75 5001 0517 2221 8623 18", "2123,54 €", "Giro1"), | ||
AccountStatus.parse("2024-08-29", "DE75 5001 0517 2221 8623 18", "12123,55 €", "Giro1"), | ||
AccountStatus.parse("2024-08-29", "DE75 5001 0517 2221 8623 18", "12123,55 €", "Giro1"), | ||
AccountStatus.parse("2024-08-29", "DE75 5001 0517 2221 8623 18", "2123,55 €", "Giro1"), | ||
AccountStatus.parse("2024-08-27", "DE88 5001 0517 2235 7114 53", "5153,54 €", "Giro2") | ||
); | ||
|
||
AccountStatusCSVWriter accountStatusCSVWriter = new AccountStatusCSVWriter(tempdir); | ||
accountStatusCSVWriter.writeStatusesToCSV(statuses); | ||
|
||
AccountStatusSummaryWriter summaryWriter = new AccountStatusSummaryWriter(tempdir); | ||
summaryWriter.writeSummaryToCSV(); | ||
|
||
Path summary = tempdir.resolve("summary.csv"); | ||
assertThat(summary).isNotEmptyFile(); | ||
|
||
List<String> lines = Files.readAllLines(summary); | ||
assertThat(lines).hasSize(4); | ||
} | ||
|
||
} |