Skip to content

Commit 1ac04a3

Browse files
committed
Add CSV input support
1 parent 3e99305 commit 1ac04a3

File tree

8 files changed

+86
-21
lines changed

8 files changed

+86
-21
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ build/
55
#IntelliJ IDEA
66
.idea
77
*.iml
8+
9+
#Test bank statements
10+
src/test/resources/*.csv

build.gradle.kts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,16 @@ repositories {
1616
// Use jcenter for resolving dependencies.
1717
// You can declare any Maven/Ivy/file repository here.
1818
jcenter()
19+
maven("https://jitpack.io")
1920
}
2021

2122
dependencies {
22-
// Align versions of all Kotlin components
23+
// Kotlin
2324
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
24-
25-
// Use the Kotlin JDK 8 standard library.
2625
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
26+
implementation("com.github.ptnplanet:Java-Naive-Bayes-Classifier:1.0.7")
2727

28-
// Use the Kotlin test library.
2928
testImplementation("org.jetbrains.kotlin:kotlin-test")
30-
31-
// Use the Kotlin JUnit integration.
3229
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
3330
}
3431

src/main/kotlin/com/github/hannotify/Main.kt renamed to src/main/kotlin/com/github/hannotify/classyfire/Main.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.hannotify
1+
package com.github.hannotify.classyfire
22

33
fun main() {
44
println("Hello from the Classy Fire!")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.github.hannotify.classyfire.data
2+
3+
import java.math.BigDecimal
4+
import java.time.LocalDate
5+
6+
data class Transaction(val date: LocalDate, val description: String, val beneficiaryIban: String,
7+
val amount: BigDecimal, val transactionType: String, val remarks: String)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.github.hannotify.classyfire.input
2+
3+
import com.github.hannotify.classyfire.data.Transaction
4+
5+
interface InputReader {
6+
fun read(filePath: String): Collection<Transaction>
7+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.github.hannotify.classyfire.input.impl
2+
3+
import com.github.hannotify.classyfire.data.Transaction
4+
import com.github.hannotify.classyfire.input.InputReader
5+
import java.io.File
6+
import java.math.BigDecimal
7+
import java.time.LocalDate
8+
import java.time.format.DateTimeFormatter
9+
10+
class IngCsvInputReader : InputReader {
11+
override fun read(filePath: String): Collection<Transaction> {
12+
val transactions = mutableListOf<Transaction>()
13+
14+
File(filePath).readLines()
15+
.filter { !isHeaderLine(it) }
16+
.forEach { transactions.add(toTransaction(it)) }
17+
18+
return transactions
19+
}
20+
21+
private fun isHeaderLine(line: String): Boolean {
22+
return line.startsWith("\"Datum")
23+
}
24+
25+
private fun toTransaction(line: String): Transaction {
26+
val fields = line.split("\",\"")
27+
.map { it.removePrefix("\"") }
28+
.map { it.removeSuffix("\"") }
29+
30+
val date = LocalDate.parse(fields[0], DateTimeFormatter.ofPattern("yyyyMMdd"))
31+
val description = fields[1]
32+
val beneficiaryIban = fields[3]
33+
val amount = toAmount(fields[6], fields[5])
34+
val transactionType = fields[7]
35+
val remarks = fields[8]
36+
37+
return Transaction(date, description, beneficiaryIban, amount, transactionType, remarks);
38+
}
39+
40+
private fun toAmount(amountString: String, bijAf: String): BigDecimal {
41+
val amount = BigDecimal(amountString.replace(',', '.'))
42+
43+
return if ("Bij" == bijAf) amount else amount.negate()
44+
}
45+
}

src/test/kotlin/com/github/hannotify/AppTest.kt

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.github.hannotify.classyfire.input.impl
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
6+
internal class IngCsvInputReaderTest {
7+
8+
@Test
9+
fun read() {
10+
// Arrange
11+
val inputReader = IngCsvInputReader()
12+
13+
// Act
14+
val transactions = inputReader.read("src/test/resources/test.csv")
15+
16+
// Assert
17+
assertEquals(96, transactions.size);
18+
println(transactions)
19+
}
20+
}

0 commit comments

Comments
 (0)