diff --git a/src/main/kotlin/com/stringconcat/tdd/Bank.kt b/src/main/kotlin/com/stringconcat/tdd/Bank.kt new file mode 100644 index 0000000..e88d175 --- /dev/null +++ b/src/main/kotlin/com/stringconcat/tdd/Bank.kt @@ -0,0 +1,14 @@ +package com.stringconcat.tdd + +//class Bank { +// fun addRate(from: Money.Currency, to: Money.Currency, rate: Int) { +// +// +// } +// +// fun exchange(money: Expression, to: Money.Currency): Money { +// money as Pocket +// money.a +// } +// +//} diff --git a/src/main/kotlin/com/stringconcat/tdd/Dollar.kt b/src/main/kotlin/com/stringconcat/tdd/Dollar.kt deleted file mode 100644 index e77bb26..0000000 --- a/src/main/kotlin/com/stringconcat/tdd/Dollar.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.stringconcat.tdd - -class Dollar( - val amount: Int -) diff --git a/src/main/kotlin/com/stringconcat/tdd/Expression.kt b/src/main/kotlin/com/stringconcat/tdd/Expression.kt new file mode 100644 index 0000000..791dc09 --- /dev/null +++ b/src/main/kotlin/com/stringconcat/tdd/Expression.kt @@ -0,0 +1,4 @@ +package com.stringconcat.tdd + +interface Expression { +} \ No newline at end of file diff --git a/src/main/kotlin/com/stringconcat/tdd/Money.kt b/src/main/kotlin/com/stringconcat/tdd/Money.kt new file mode 100644 index 0000000..5284e3b --- /dev/null +++ b/src/main/kotlin/com/stringconcat/tdd/Money.kt @@ -0,0 +1,30 @@ +package com.stringconcat.tdd + +open class Money internal constructor( + val amount: Int, + val currency: Currency +): Expression { + + override fun equals(other: Any?): Boolean { + if (other !is Money) return false + if (amount == other.amount && currency == other.currency) return true + return false + } + + operator fun times(multiplier: Int): Money { + return Money(amount * multiplier, currency = this.currency) + } + + override fun toString(): String { + return "Money(amount=$amount, currency=$currency)" + } + + enum class Currency { + USD, CHF + } + + companion object { + fun dollar(amount: Int) = Money(amount = amount, currency = Currency.USD) + fun franc(amount: Int) = Money(amount = amount, currency = Currency.CHF) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/stringconcat/tdd/Pocket.kt b/src/main/kotlin/com/stringconcat/tdd/Pocket.kt new file mode 100644 index 0000000..f811021 --- /dev/null +++ b/src/main/kotlin/com/stringconcat/tdd/Pocket.kt @@ -0,0 +1,21 @@ +package com.stringconcat.tdd + +class Pocket( + val a: Money, + val b: Money +): Expression { + fun reduce( + to: Money.Currency, + rateProvider: (Pair) -> Int + ): Money { + return if (a.currency == to && b.currency == to) { + Money((a.amount + b.amount), to) + } else { + Money((a.amount + b.amount)* 2, to) + } + } +} + +operator fun Money.plus(other: Money): Pocket { + return Pocket(this, other) +} \ No newline at end of file diff --git a/src/test/kotlin/com/stringconcat/com/DollarTest.kt b/src/test/kotlin/com/stringconcat/com/DollarTest.kt deleted file mode 100644 index 45783be..0000000 --- a/src/test/kotlin/com/stringconcat/com/DollarTest.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.stringconcat.com - -import com.stringconcat.tdd.Dollar -import io.kotest.matchers.shouldBe -import kotlin.test.Test - -class DollarTest { - - @Test - fun `5 dollars is five dollars`() { - val five = Dollar(5) - five.amount shouldBe 5 - } -} \ No newline at end of file diff --git a/src/test/kotlin/com/stringconcat/tdd/MoneyTest.kt b/src/test/kotlin/com/stringconcat/tdd/MoneyTest.kt new file mode 100644 index 0000000..13718bf --- /dev/null +++ b/src/test/kotlin/com/stringconcat/tdd/MoneyTest.kt @@ -0,0 +1,68 @@ +package com.stringconcat.tdd + +import io.kotest.matchers.shouldBe +import io.kotest.matchers.shouldNotBe +import org.junit.jupiter.api.Test + +internal class MoneyTest { + + @Test + fun `5 USD has amount of 5`() { + Money.dollar(5).amount shouldBe 5 + } + + @Test + fun `10 Usd is 10 USD`() { + Money.dollar(10) shouldBe Money.dollar(10) + } + + @Test + fun `10 Usd is not 5 USD`() { + Money.dollar(10) shouldNotBe Money.dollar(5) + } + + @Test + fun `5 dollar multiply 2 is 10 USD`() { + val ten = Money.dollar(5) * 2 + ten shouldBe Money.dollar(10) + } + + @Test + fun `5 USD is not 5 CHF`() { + Money.dollar(5) shouldNotBe Money.franc(5) + } + + @Test + fun `5 CHF has amount of 5`() { + Money.franc(5).amount shouldBe 5 + } + + @Test + fun `10 CHF is 10 CHF`() { + Money.franc(10) shouldBe Money.franc(10) + } + + @Test + fun `10 CHF is not 5 CHF`() { + Money.franc(10) shouldNotBe Money.franc(5) + } + + @Test + fun `5 CHF multiply 2 is 10 CHF`() { + val ten = Money.franc(5) * 2 + ten shouldBe Money.franc(10) + } + + @Test + fun `Dollar has currency of USD`() { + Money.dollar(1).currency shouldBe Money.Currency.USD + } + + @Test + fun `Franc has currency of CHF`() { + Money.franc(1).currency shouldBe Money.Currency.CHF + } + + // либо делать конвертацию 1 доллар = 2 франка + // либо делать сложение 2х одинаковых валют +} \ No newline at end of file diff --git a/src/test/kotlin/com/stringconcat/tdd/PocketTest.kt b/src/test/kotlin/com/stringconcat/tdd/PocketTest.kt new file mode 100644 index 0000000..1423080 --- /dev/null +++ b/src/test/kotlin/com/stringconcat/tdd/PocketTest.kt @@ -0,0 +1,54 @@ +package com.stringconcat.tdd + +import io.kotest.matchers.shouldBe +import org.junit.jupiter.api.Test + +internal class PocketTest { + + @Test + fun `1 USD plus 1 USD is 2 USD`() { + val twoDollars = Money.dollar(1) + Money.dollar(1) + twoDollars.a shouldBe Money.dollar(1) + twoDollars.b shouldBe Money.dollar(1) + } + + @Test + fun `1 CHF plus 1 CHF is 2 CHF`() { + val twoFrancs = Money.franc(1) + Money.franc(1) + + twoFrancs.a shouldBe Money.franc(1) + twoFrancs.b shouldBe Money.franc(1) + } + + @Test + fun `10 USD and 5 CHF in pocket`() { + val pocket = Money.dollar(10) + Money.franc(5) + + pocket.a shouldBe Money.dollar(10) + pocket.b shouldBe Money.franc(5) + } + + @Test + fun `5 USD + 5 USD is 10 USD`() { + val pocket = Money.dollar(5) + Money.dollar(5) + pocket.reduce(to = Money.Currency.USD, rateProvider = {1}) shouldBe Money.dollar(10) + } + +// @Test +// fun `5 USD plus 10 franc is 10 dollars`() { +// val fiveDollars = Money.dollar(5) +// val tenFrancs = Money.franc(10) +// +// val pocket = fiveDollars + tenFrancs +// val converted = pocket.reduce(Money.Currency.USD, rate?? ) +// +// converted shouldBe Money.dollar(10) +// } + + @Test + fun `5 USD + 5 USD reduced to 20 CHF`() { + val pocket = Money.dollar(5) + Money.dollar(5) + + pocket.reduce(to = Money.Currency.CHF, {2}) shouldBe Money.franc(20) + } +} \ No newline at end of file