Skip to content

Commit 68ba233

Browse files
committed
Create bank account
Small first step to help drive the initial core of the domain project. Extract micro types for the account id and the amount. The latter is not currency specific but just minor units, which is pence for British pounds. Externalise account id generation to avoid having static references in the class, which would have made the test more difficult to write.
1 parent 9ebd103 commit 68ba233

5 files changed

Lines changed: 53 additions & 0 deletions

File tree

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ subprojects {
1515
dependencies {
1616
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit5'
1717
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
18+
testImplementation 'com.natpryce:hamkrest:1.8.0.1'
1819
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
1920
}
2021

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package lmirabal.bank
2+
3+
import lmirabal.bank.model.Amount
4+
import lmirabal.bank.model.BankAccount
5+
import lmirabal.bank.model.BankAccountId
6+
7+
class BankService(val idFactory: () -> BankAccountId = { BankAccountId.random() }) {
8+
fun createAccount(): BankAccount {
9+
return BankAccount(idFactory(), Amount.ZERO)
10+
}
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package lmirabal.bank.model
2+
3+
data class Amount(val minorUnits: Long) {
4+
5+
companion object {
6+
val ZERO = Amount(0)
7+
}
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package lmirabal.bank.model
2+
3+
import java.util.UUID
4+
5+
data class BankAccount(val id: BankAccountId, val balance: Amount)
6+
7+
data class BankAccountId(val value: UUID) {
8+
9+
companion object {
10+
fun random() = BankAccountId(UUID.randomUUID())
11+
}
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package lmirabal.bank
2+
3+
import com.natpryce.hamkrest.assertion.assertThat
4+
import com.natpryce.hamkrest.equalTo
5+
import lmirabal.bank.model.Amount
6+
import lmirabal.bank.model.BankAccount
7+
import lmirabal.bank.model.BankAccountId
8+
import org.junit.jupiter.api.Test
9+
10+
class BankServiceTest {
11+
@Test
12+
fun createsAnAccount() {
13+
val id = BankAccountId.random()
14+
val idFactory = { id }
15+
val bank = BankService(idFactory)
16+
17+
val account = bank.createAccount()
18+
19+
assertThat(account, equalTo(BankAccount(id, Amount.ZERO)))
20+
}
21+
}

0 commit comments

Comments
 (0)