-
Notifications
You must be signed in to change notification settings - Fork 357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
π 2λ¨κ³ - λ‘λ(μλ) #994
base: dajeongda
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package lotto | ||
|
||
import java.math.RoundingMode | ||
import java.text.DecimalFormat | ||
|
||
class BenefitCalculator { | ||
fun calculate(result: Map<Int, Int>): String { | ||
val spend = result.map { it.value }.sum() * LOTTO_PRICE | ||
val benefit = result.map { | ||
when (it.key) { | ||
1 -> WINNER_REWARD * it.value | ||
2 -> SECOND_REWARD * it.value | ||
3 -> THIRD_REWARD * it.value | ||
4 -> FORTH_REWARD * it.value | ||
else -> 0 | ||
} | ||
}.sum() | ||
|
||
val rate = benefit.toDouble() / spend | ||
df.roundingMode = RoundingMode.FLOOR | ||
return df.format(rate) | ||
} | ||
|
||
companion object { | ||
private const val WINNER_REWARD = 2000000000 | ||
private const val SECOND_REWARD = 1500000 | ||
private const val THIRD_REWARD = 50000 | ||
private const val FORTH_REWARD = 5000 | ||
private const val LOTTO_PRICE = 1000 | ||
private val df = DecimalFormat("0.00") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package lotto | ||
|
||
import lotto.view.InputView | ||
import lotto.view.ResultView | ||
|
||
class LottoGame { | ||
fun match( | ||
userLotto: Set<Int>, | ||
Comment on lines
+7
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Intμλ 1~45 λΌλ λ‘λ λ²νΈλ₯Ό μ ννκΈ°μλ λ무 λ²μ©μ μΈ μλ£νμ΄λΌ μκ°ν΄μ. |
||
winningLotto: Set<Int>, | ||
): Int { | ||
val matchCount = userLotto.intersect(winningLotto.toSet()).size | ||
return rank(matchCount) | ||
} | ||
|
||
private fun rank( | ||
matchCount: Int, | ||
): Int { | ||
return when (matchCount) { | ||
6 -> 1 | ||
5 -> 2 | ||
4 -> 3 | ||
3 -> 4 | ||
else -> 0 | ||
} | ||
} | ||
} | ||
|
||
fun main() { | ||
val inputView = InputView() | ||
val resultView = ResultView() | ||
val shop = LottoShop() | ||
val game = LottoGame() | ||
val money = inputView.get("ꡬμ κΈμ‘μ μ λ ₯ν΄ μ£ΌμΈμ.") | ||
// μΌλ§λ₯Ό μ΄ μ μλ€. | ||
val count = money / 1000 | ||
val lottos = shop.buy(count) | ||
println("${count}κ°λ₯Ό ꡬ맀νμ΅λλ€.") | ||
lottos.forEach { | ||
println(it) | ||
} | ||
val winningLotto = inputView.getWinningLotto("μ§λ μ£Ό λΉμ²¨ λ²νΈλ₯Ό μ λ ₯ν΄ μ£ΌμΈμ.") | ||
// λ§μΆ° λ³Έλ€. | ||
val result = lottos.groupingBy { | ||
game.match(it, winningLotto) | ||
}.eachCount() | ||
|
||
val benefitCalculator = BenefitCalculator() | ||
val benefit = benefitCalculator.calculate(result) | ||
resultView.show(result, benefit) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package lotto | ||
|
||
import kotlin.random.Random | ||
|
||
class LottoGenerator { | ||
fun generate(): Set<Int> { | ||
val numbers = mutableSetOf<Int>() | ||
while (numbers.size < LIMIT_SIZE) { | ||
numbers.add(Random.nextInt(MAXIMUM)) | ||
} | ||
return numbers | ||
} | ||
|
||
companion object { | ||
private const val MAXIMUM = 45 | ||
private const val LIMIT_SIZE = 6 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package lotto | ||
|
||
class LottoShop { | ||
fun buy(count: Int): List<Set<Int>> { | ||
Comment on lines
+3
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 컬λ μ
μ μλ£ν νμ
μ΄ κ²Ήμ³μ ΈμμΌλ©΄ μ΄λ€ κ°μ μλ―Ένλμ§ μ½κ² μμ보기 μ΄λ €μμ§λ€κ³ μκ°ν΄μ. |
||
val lottoGenerator = LottoGenerator() | ||
val lottos = mutableListOf<Set<Int>>() | ||
repeat(count) { | ||
lottos.add(lottoGenerator.generate()) | ||
} | ||
|
||
return lottos | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package lotto.view | ||
|
||
class InputView { | ||
fun get(prompt: String): Int { | ||
print(prompt) | ||
return readln().toInt() | ||
} | ||
|
||
fun getWinningLotto(prompt: String): Set<Int> { | ||
print(prompt) | ||
return readln().split(",").map { it.toInt() }.toSet() | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package lotto.view | ||
|
||
class ResultView { | ||
fun show(result: Map<Int, Int>, benefit: String) { | ||
println("λΉμ²¨ ν΅κ³") | ||
println("---------") | ||
(1..4).reversed().forEach { | ||
showOne(it, result.getOrDefault(it, 0)) | ||
} | ||
|
||
val bep = if (benefit.toDouble() > 1.0) "μ΄μ΅" else "μν΄" | ||
println("μ΄ μμ΅λ₯ μ ${benefit}μ λλ€. (κΈ°μ€μ΄ 1μ΄κΈ° λλ¬Έμ κ²°κ³Όμ μΌλ‘ ${bep}λΌλ μλ―Έμ)") | ||
} | ||
|
||
private fun showOne(rank: Int, count: Int) { | ||
when (rank) { | ||
1 -> println("6κ° μΌμΉ (2000000000μ) - ${count}κ°") | ||
2 -> println("5κ° μΌμΉ (1500000μ) - ${count}κ°") | ||
3 -> println("4κ° μΌμΉ (50000μ) - ${count}κ°") | ||
4 -> println("3κ° μΌμΉ (5000μ) - ${count}κ°") | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package lotto | ||
|
||
import org.assertj.core.api.AssertionsForClassTypes.assertThat | ||
import org.junit.jupiter.api.Test | ||
|
||
class BenefitCalculatorTest { | ||
|
||
@Test | ||
fun `14μ₯ μ€μ 4λ± 1μ₯ λ―ΈλΉμ²¨ 13μ₯μΌ κ²½μ° μμ΅λ₯ μ 0,35μ΄λ€`() { | ||
val calculator = BenefitCalculator() | ||
val result = mapOf( | ||
1 to 0, | ||
2 to 0, | ||
3 to 0, | ||
4 to 1, | ||
0 to 13 | ||
) | ||
|
||
val actual = calculator.calculate(result) | ||
assertThat(actual).isEqualTo("0.35") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package lotto | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
|
||
class LottoGameTest { | ||
|
||
@Test | ||
fun `1λ±μ 6κ° μΌμΉ` () { | ||
val lottoGame = LottoGame() | ||
val userLotto = setOf(1, 2, 3, 4, 5, 6) | ||
val winningLotto = setOf(1, 2, 3, 4, 5, 6) | ||
val actual = lottoGame.match(userLotto, winningLotto) | ||
|
||
actual shouldBe 1 | ||
} | ||
|
||
@Test | ||
fun `2λ±μ 5κ° μΌμΉ`() { | ||
val lottoGame = LottoGame() | ||
val userLotto = setOf(1, 2, 3, 4, 5, 6) | ||
val winningLotto = setOf(1, 2, 3, 4, 5, 7) | ||
val actual = lottoGame.match(userLotto, winningLotto) | ||
|
||
actual shouldBe 2 | ||
} | ||
|
||
@Test | ||
fun `3λ±μ 4κ° μΌμΉ`() { | ||
val lottoGame = LottoGame() | ||
val userLotto = setOf(1, 2, 3, 4, 5, 6) | ||
val winningLotto = setOf(1, 2, 3, 4, 7, 8) | ||
val actual = lottoGame.match(userLotto, winningLotto) | ||
|
||
actual shouldBe 3 | ||
} | ||
|
||
@Test | ||
fun `4λ±μ 3κ° μΌμΉ`() { | ||
val lottoGame = LottoGame() | ||
val userLotto = setOf(1, 2, 3, 4, 5, 6) | ||
val winningLotto = setOf(1, 2, 3, 7, 8, 9) | ||
val actual = lottoGame.match(userLotto, winningLotto) | ||
|
||
actual shouldBe 4 | ||
} | ||
|
||
@Test | ||
fun `2κ° κ²½μ° μΌμΉ νλ©΄ 0`(userLotto: Set<Int>) { | ||
val lottoGame = LottoGame() | ||
val userLotto = setOf(1, 2, 12, 13, 14, 15) | ||
val winningLotto = setOf(1, 2, 3, 7, 8, 9) | ||
val actual = lottoGame.match(userLotto, winningLotto) | ||
|
||
actual shouldBe 4 | ||
} | ||
|
||
@Test | ||
fun `1κ° κ²½μ° μΌμΉ νλ©΄ 0`(userLotto: Set<Int>) { | ||
val lottoGame = LottoGame() | ||
val userLotto = setOf(1, 11, 12, 13, 14, 16) | ||
val winningLotto = setOf(1, 2, 3, 7, 8, 9) | ||
val actual = lottoGame.match(userLotto, winningLotto) | ||
|
||
actual shouldBe 4 | ||
} | ||
|
||
@Test | ||
fun `0κ° κ²½μ° μΌμΉ νλ©΄ 0`(userLotto: Set<Int>) { | ||
val lottoGame = LottoGame() | ||
val userLotto = setOf(10, 11, 12, 13, 14, 16) | ||
val winningLotto = setOf(1, 2, 3, 7, 8, 9) | ||
val actual = lottoGame.match(userLotto, winningLotto) | ||
|
||
actual shouldBe 4 | ||
} | ||
Comment on lines
+8
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ΄λ° ννμ ν
μ€νΈλ |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package lotto | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
|
||
class LottoGeneratorTest { | ||
|
||
@Test | ||
fun `λ‘λ μμ±κΈ°λ μ«μ 6κ°λ₯Ό μμ±νλ€`() { | ||
val lottoGenerator = LottoGenerator() | ||
val lotto = lottoGenerator.generate() | ||
lotto.size shouldBe 6 | ||
} | ||
|
||
@Test | ||
fun `λ‘λ μμ±κΈ°λ μ«μκ° κ²ΉμΉμ§ μλλ€`() { | ||
val lottoGenerator = LottoGenerator() | ||
val lotto = lottoGenerator.generate() | ||
lotto.distinct().size shouldBe 6 | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package lotto | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
|
||
class LottoShopTest { | ||
|
||
@Test | ||
fun `λ‘λ nμ₯ μ¬κΈ°`() { | ||
val lottoShop = LottoShop() | ||
val count = 5 | ||
val lottos = lottoShop.buy(5) | ||
lottos.size shouldBe count | ||
} | ||
Comment on lines
+8
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. λͺ μ₯μ μλμ§ κ²μ¦νλ κ²μ ν
μ€νΈμ ν° μλ―Έλ₯Ό κ°μ§μ§ λͺ»νλ€κ³ μκ°ν΄μ. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ‘λ λ±μλ₯Ό λλ©μΈ κ°μ²΄λ‘ ννν΄λ³΄λ 건 μ΄λ¨κΉμ? enum λ±μΌλ‘ ꡬνν΄λ³΄μ λ μ’κ² μ΄μ :)