From 3101bca17d013bd367c0d96d7e96e65ae18c6cce Mon Sep 17 00:00:00 2001 From: Sergey Date: Sat, 29 May 2021 14:28:55 +0800 Subject: [PATCH 1/2] revert to the begining --- src/main/kotlin/com/stringconcat/tdd/Dollar.kt | 5 ----- src/test/kotlin/com/stringconcat/com/DollarTest.kt | 14 -------------- 2 files changed, 19 deletions(-) delete mode 100644 src/main/kotlin/com/stringconcat/tdd/Dollar.kt delete mode 100644 src/test/kotlin/com/stringconcat/com/DollarTest.kt 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/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 From 4db91651490a43b3514cdb3c6d6dd312c63bc79a Mon Sep 17 00:00:00 2001 From: Sergey Date: Sat, 28 Aug 2021 17:35:32 +0800 Subject: [PATCH 2/2] implement money for group 3 --- src/main/kotlin/com/stringconcat/tdd/Money.kt | 39 +++++++++ src/main/kotlin/com/stringconcat/tdd/main.kt | 8 +- .../kotlin/com/stringconcat/tdd/MoneyTest.kt | 83 +++++++++++++++++++ 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/com/stringconcat/tdd/Money.kt create mode 100644 src/test/kotlin/com/stringconcat/tdd/MoneyTest.kt 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..78a5260 --- /dev/null +++ b/src/main/kotlin/com/stringconcat/tdd/Money.kt @@ -0,0 +1,39 @@ +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 + return this.currency == other.currency && this.amount == other.amount + } + + operator fun times(multiplier: Int): Money { + return Money(amount * multiplier, this.currency) + } + + operator fun plus(other: Money): Money { + return Money(this.amount + other.amount, this.currency) + } + + override fun toString(): String { + return "Money(amount=$amount, currency=$currency)" + } + + fun toFranc(): Money { + if (currency == Currency.CHF) return this + return Money(amount * 2, Currency.CHF) + } + + companion object { + fun dollar(amount: Int) = Money(amount, Currency.USD) + fun franc(amount: Int) = Money(amount, Currency.CHF) + } + + enum class Currency() { + USD, CHF + } + +} \ 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..1cfe5c1 100644 --- a/src/main/kotlin/com/stringconcat/tdd/main.kt +++ b/src/main/kotlin/com/stringconcat/tdd/main.kt @@ -1,3 +1,9 @@ fun main() { println("Hello World!") -} \ No newline at end of file +} + +/*todo +5 USD to CHF = 10 CHF +rate??? +5 USD + 10 CHF = 10 USD if rate (2:1) + */ \ 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..2a2a9cb --- /dev/null +++ b/src/test/kotlin/com/stringconcat/tdd/MoneyTest.kt @@ -0,0 +1,83 @@ +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 CHF is equal to 5 CHF`() { + Money.franc(5) shouldBe Money.franc(5) + } + + @Test + fun `5 CHF is NOT equal to 7 CHF`() { + Money.franc(5) shouldNotBe Money.franc(7) + } + + @Test + fun `No CHF is equal to null`() { + Money.franc(5).shouldNotBe(null) + } + + @Test + fun `5 CHF multiply 2 should be 10 CHF`() { + Money.franc(5) * 2 shouldBe Money.franc(10) + } + + @Test + fun `10 CHF multiply 3 should be 30 CHF`() { + Money.franc(10) * 3 shouldBe Money.franc(30) + } + + @Test + fun `2 CHF + 2 CHF is 4 CHF`() { + Money.franc(2) + Money.franc(2) shouldBe Money.franc(4) + } + + @Test + fun `5 USD is equal to 5 USD`() { + Money.dollar(5) shouldBe Money.dollar(5) + } + + @Test + fun `5 USD is NOT equal to 7 USD`() { + Money.dollar(5) shouldNotBe Money.dollar(7) + } + + @Test + fun `No dollar is equal to null`() { + Money.dollar(5).shouldNotBe(null) + } + + @Test + fun `5 USD multiply 2 should be 10 USD`() { + Money.dollar(5) * 2 shouldBe Money.dollar(10) + } + + @Test + fun `10 USD multiply 3 should be 30 USD`() { + Money.dollar(10) * 3 shouldBe Money.dollar(30) + } + + @Test + fun `2 USD + 2 USD is 4 USD`() { + Money.dollar(2) + Money.dollar(2) shouldBe Money.dollar(4) + } + + @Test + fun `2 USD is not equal 2 CHF`() { + Money.dollar(2) shouldNotBe Money.franc(2) + } + + @Test + fun `5 USD to CHF is 10 CHF if rate 2 to 1`() { + Money.dollar(5).toFranc() shouldBe Money.franc(10) + } + + @Test + fun `5 CHF to CHF is 5 CHF`() { + Money.franc(5).toFranc() shouldBe Money.franc(5) + } +} \ No newline at end of file