From 3101bca17d013bd367c0d96d7e96e65ae18c6cce Mon Sep 17 00:00:00 2001 From: Sergey Date: Sat, 29 May 2021 14:28:55 +0800 Subject: [PATCH 1/3] 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 f06170c88a80e98818b136de6c4dcbb494d69030 Mon Sep 17 00:00:00 2001 From: Sergey Date: Sat, 29 May 2021 21:56:27 +0800 Subject: [PATCH 2/3] what we did during practice --- src/main/kotlin/com/stringconcat/tdd/Money.kt | 39 +++++++++++ .../kotlin/com/stringconcat/tdd/Wallet.kt | 21 ++++++ src/main/kotlin/com/stringconcat/tdd/main.kt | 8 ++- .../kotlin/com/stringconcat/tdd/MoneyTest.kt | 69 +++++++++++++++++++ .../kotlin/com/stringconcat/tdd/WalletTest.kt | 17 +++++ 5 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/com/stringconcat/tdd/Money.kt create mode 100644 src/main/kotlin/com/stringconcat/tdd/Wallet.kt create mode 100644 src/test/kotlin/com/stringconcat/tdd/MoneyTest.kt create mode 100644 src/test/kotlin/com/stringconcat/tdd/WalletTest.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..2fd26ec --- /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 +) { + + private val rate: Int = 2 + + companion object { + fun dollar(amount: Int) = Money(amount, Currency.USD) + fun franc(amount: Int) = Money(amount, Currency.CHF) + } + + operator fun plus(other: Money): Wallet { + return if (this.currency != other.currency) { + Wallet(this, other) + } else { + Wallet(Money(this.amount + other.amount, this.currency)) + } + } + + override fun equals(other: Any?): Boolean { + if (other !is Money) return false + return this.amount == other.amount && this.currency == other.currency + } + + operator fun times(multiplier: Int): Money { + return Money(amount * multiplier, currency) + } + + override fun toString(): String { + return "Money(amount=$amount, currency=$currency)" + } + + enum class Currency { + USD, CHF + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/stringconcat/tdd/Wallet.kt b/src/main/kotlin/com/stringconcat/tdd/Wallet.kt new file mode 100644 index 0000000..282923a --- /dev/null +++ b/src/main/kotlin/com/stringconcat/tdd/Wallet.kt @@ -0,0 +1,21 @@ +package com.stringconcat.tdd + +import kotlin.math.roundToInt + +class Wallet(vararg val money: Money) { + + + override fun equals(other: Any?): Boolean { + if (other !is Wallet) return false + return this.money.contentEquals(other.money) + } + + override fun toString(): String { + return "Wallet(money=${money.contentToString()})" + } + + fun asDollars(rate: Double): Money { + return Money.dollar((money.first().amount / rate).roundToInt()) + } + +} diff --git a/src/main/kotlin/com/stringconcat/tdd/main.kt b/src/main/kotlin/com/stringconcat/tdd/main.kt index a7a5e00..2bd21e3 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 +} + +// 2 CHF + 4 USD = 5 USD (if rate 4:1) +// 2 CHF + 4 USD = 10 CHF (if rate 2:1) +// 2 EUR + 4 USD = 8 USD +// no negative amount +// to big amount (more than int) \ 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..118e6a0 --- /dev/null +++ b/src/test/kotlin/com/stringconcat/tdd/MoneyTest.kt @@ -0,0 +1,69 @@ +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 dollars is 5 dollars`() { + Money.dollar(5) shouldBe Money.dollar(5) + } + + @Test + fun `5 dollars is NOT 2 dollars`() { + Money.dollar(5) shouldNotBe Money.dollar(2) + } + + @Test + fun `5 dollars * 2 is 10 dollars`() { + Money.dollar(5) * 2 shouldBe Money.dollar(10) + } + + @Test + fun `5 dollars * 3 is 15 dollars`() { + Money.dollar(5) * 3 shouldBe Money.dollar(15) + } + + @Test + fun `5 franc is 5 franc`() { + Money.franc(5) shouldBe Money.franc(5) + } + + @Test + fun `5 franc is NOT 2 franc`() { + Money.franc(5) shouldNotBe Money.franc(2) + } + + @Test + fun `5 franc * 2 is 10 franc`() { + Money.franc(5) * 2 shouldBe Money.franc(10) + } + + @Test + fun `5 franc * 3 is 15 franc`() { + Money.franc(5) * 3 shouldBe Money.franc(15) + } + + @Test + fun `5 CHF + 5 CHF is 10 CHF`() { + Money.franc(5) + Money.franc(5) shouldBe Wallet(Money.franc(10)) + } + + @Test + fun `5 dollar + 5 dollar is 10 dollars`() { + Money.dollar(5) + Money.dollar(5) shouldBe Wallet(Money.dollar(10)) + } + + @Test + fun `5 dollars is not 5 francs`() { + Money.dollar(5) shouldNotBe Money.franc(5) + } + + @Test + fun `2 CHF + 4 USD is wallet that contains 2 CHF and 4 USD`() { + Money.franc(2) + Money.dollar(4) shouldBe Wallet(Money.franc(2), Money.dollar(4) ) + } + +} \ No newline at end of file diff --git a/src/test/kotlin/com/stringconcat/tdd/WalletTest.kt b/src/test/kotlin/com/stringconcat/tdd/WalletTest.kt new file mode 100644 index 0000000..565e0d9 --- /dev/null +++ b/src/test/kotlin/com/stringconcat/tdd/WalletTest.kt @@ -0,0 +1,17 @@ +package com.stringconcat.tdd + +import io.kotest.matchers.shouldBe +import org.junit.jupiter.api.Test + +internal class WalletTest { + + @Test + fun `wallet containing 2 dollars is another wallet containing 2 dollars`() { + Wallet(Money.dollar(2)) shouldBe Wallet(Money.dollar(2)) + } + + @Test + fun `wallet that contains 2 CHF returns 1 dollars if rate 2 to 1`() { + Wallet(Money.franc(2)).asDollars(2.0) shouldBe Money.dollar(1) + } +} \ No newline at end of file From db186c8553910b563ed1ddb3bfe0564adc54b202 Mon Sep 17 00:00:00 2001 From: Sergey Date: Sat, 29 May 2021 22:08:39 +0800 Subject: [PATCH 3/3] homework is added --- Readme.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Readme.md diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..2df7113 --- /dev/null +++ b/Readme.md @@ -0,0 +1,27 @@ +#TDD practice + +| **Instrument** | **Shares** | **Price** | **Total** | +|----------------|------------|-----------|-----------| +| IBM | 1000 | 25 USD | 25000 USD | +| Novartis | 400 | 150 CHF | 60000 USD | +| | | **Total** | 65000 USD | + +Задание: необходимо реализовать доменную модель для отображение заданого отчета. + +Примеры использования: +``` +1 USD = 1 USD +1 USD != 2 USD +1 USD != 1 CFH +2 USB * 2 = 4 USD +2 CHF + 4 USD = 5 USD (if rate 4:1) +2 CHF + 4 USD = 10 CHF (if rate 2:1) +2 EUR + 4 USD = 8 USD +no negative amount +to big amount (more than int) +``` +**Задание на дом** +1. Доделать Wallet, чтобы он мог использоваться с несколькими "стопками денег" +2. Привнести EUR в проект, при том что 3 валюты могли конвертироваться друг в друга +3. (задание со звездочкой) Сделать так, чтобы + `Waller(dollar(30), franc(20)) + franc(20)` работало \ No newline at end of file