From 9c50f8e831369915e966e2bcebf34965b14418c5 Mon Sep 17 00:00:00 2001 From: sergeybukharov Date: Mon, 25 Oct 2021 14:01:13 +0800 Subject: [PATCH] group 4 code --- .../kotlin/com/stringconcat/tdd/Currency.kt | 5 ++ .../kotlin/com/stringconcat/tdd/Dollar.kt | 5 -- src/main/kotlin/com/stringconcat/tdd/Money.kt | 40 ++++++++++++ src/main/kotlin/com/stringconcat/tdd/main.kt | 6 +- .../kotlin/com/stringconcat/com/DollarTest.kt | 14 ---- .../kotlin/com/stringconcat/tdd/DollarTest.kt | 65 +++++++++++++++++++ .../kotlin/com/stringconcat/tdd/FrancTest.kt | 50 ++++++++++++++ 7 files changed, 165 insertions(+), 20 deletions(-) create mode 100644 src/main/kotlin/com/stringconcat/tdd/Currency.kt delete mode 100644 src/main/kotlin/com/stringconcat/tdd/Dollar.kt create mode 100644 src/main/kotlin/com/stringconcat/tdd/Money.kt delete mode 100644 src/test/kotlin/com/stringconcat/com/DollarTest.kt create mode 100644 src/test/kotlin/com/stringconcat/tdd/DollarTest.kt create mode 100644 src/test/kotlin/com/stringconcat/tdd/FrancTest.kt diff --git a/src/main/kotlin/com/stringconcat/tdd/Currency.kt b/src/main/kotlin/com/stringconcat/tdd/Currency.kt new file mode 100644 index 0000000..5cb6717 --- /dev/null +++ b/src/main/kotlin/com/stringconcat/tdd/Currency.kt @@ -0,0 +1,5 @@ +package com.stringconcat.tdd + +enum class Currency { + USD, CHF +} \ No newline at end of file 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/Money.kt b/src/main/kotlin/com/stringconcat/tdd/Money.kt new file mode 100644 index 0000000..d048108 --- /dev/null +++ b/src/main/kotlin/com/stringconcat/tdd/Money.kt @@ -0,0 +1,40 @@ +package com.stringconcat.tdd + +open class Money( + val amount: Int, + val currency: Currency + ) { + + override fun equals(other: Any?): Boolean { + if (other !is Money) return false + if (other.currency != currency) return false + return amount == other.amount + } + + operator fun times(multiplier: Int): Money { + return money(amount * multiplier, currency) + } + + operator fun plus(money: Money): Money { + return money(amount + money.amount, money.currency) + } + + override fun toString(): String { + return "$amount$currency" + } + + fun toDollar(rate: Int): Money { + return dollar(amount/rate) + } + + companion object { + fun dollar(amount: Int) = Money(amount, Currency.USD) + fun franc(amount: Int) = Money(amount, Currency.CHF) + private fun money(amount: Int, currency: Currency): Money = + if (currency == Currency.USD) { + dollar(amount) + } else { + franc(amount) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/stringconcat/tdd/main.kt b/src/main/kotlin/com/stringconcat/tdd/main.kt index a7a5e00..fde4fb7 100644 --- a/src/main/kotlin/com/stringconcat/tdd/main.kt +++ b/src/main/kotlin/com/stringconcat/tdd/main.kt @@ -1,3 +1,7 @@ fun main() { println("Hello World!") -} \ No newline at end of file +} + +// 4 CHF = 2 USD (if rate 2:1) +// 2 USD + 4 CHF = 4 USD (if rate 2:1) +// 2 USD + 4 CHF + 8 CHF + 2 USD 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/DollarTest.kt b/src/test/kotlin/com/stringconcat/tdd/DollarTest.kt new file mode 100644 index 0000000..c6ff136 --- /dev/null +++ b/src/test/kotlin/com/stringconcat/tdd/DollarTest.kt @@ -0,0 +1,65 @@ +package com.stringconcat.tdd + +import io.kotest.matchers.shouldBe +import io.kotest.matchers.shouldNotBe +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test + +internal class DollarTest { + + @Test + fun `2 CHF is not 2 USD`() { + Money.dollar(2) shouldNotBe Money.franc(2) + } + + @Test + fun `2 multiply 5 USD is 10 USD`() { + Money.dollar(5) * 2 shouldBe Money.dollar(10) + } + + @Test + fun `3 multiply 5 USD is 15 USD`() { + Money.dollar(5) * 3 shouldBe Money.dollar(15) + } + + @Test + fun `zero dollars are zero dollars`() { + Money.dollar(0) shouldBe Money.dollar(0) + } + + @Test + fun `2 USD is 2 USD`() { + Money.dollar(2) shouldBe Money.dollar(2) + } + + @Test + fun `2 USD is not 3 USD`() { + Money.dollar(2) shouldNotBe Money.dollar(3) + } + + @Test + fun `2 USD is not equal to null`() { + Money.dollar(2).equals(null) shouldBe false + } + + @Test + fun `2 USD is not equal to any other object`() { + Money.dollar(2) shouldNotBe "any string" + } + + @Test + fun `2 USD + 2 USD is 4 USD`() { + Money.dollar(2) + Money.dollar(2) shouldBe Money.dollar(4) + } + + @Test + fun `4 CHF is 2 USD if rate 2 to 1`() { + Money.franc(4).toDollar(rate = 2) shouldBe (Money.dollar(2)) + } + + @Test + fun `5 USD to USD = 5 USD`() { + Money.dollar(5).toDollar(1) shouldBe (Money.dollar(5)) + } +} \ No newline at end of file diff --git a/src/test/kotlin/com/stringconcat/tdd/FrancTest.kt b/src/test/kotlin/com/stringconcat/tdd/FrancTest.kt new file mode 100644 index 0000000..89d755d --- /dev/null +++ b/src/test/kotlin/com/stringconcat/tdd/FrancTest.kt @@ -0,0 +1,50 @@ +package com.stringconcat.tdd + +import io.kotest.matchers.shouldBe +import io.kotest.matchers.shouldNotBe +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test + +internal class FrancTest { + + @Test + fun `2 multiply 5 CHF is 10 CHF`() { + Money.franc(5) * 2 shouldBe Money.franc(10) + } + + @Test + fun `3 multiply 5 CHF is 15 CHF`() { + Money.franc(5) * 3 shouldBe Money.franc(15) + } + + @Test + fun `zero Francs are zero Francs`() { + Money.franc(0) shouldBe Money.franc(0) + } + + @Test + fun `2 CHF is 2 CHF`() { + Money.franc(2) shouldBe Money.franc(2) + } + + @Test + fun `2 CHF is not 3 CHF`() { + Money.franc(2) shouldNotBe Money.franc(3) + } + + @Test + fun `2 CHF is not equal to null`() { + Money.franc(2).equals(null) shouldBe false + } + + @Test + fun `2 CHF is not equal to any other object`() { + Money.franc(2) shouldNotBe "any string" + } + + @Test + fun `2 CHF + 2 CHF is 4 CHF`() { + Money.franc(2) + Money.franc(2) shouldBe Money.franc(4) + } +} \ No newline at end of file