-
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
🚀 3단계 - 로또(2등) #1141
base: sjjeong
Are you sure you want to change the base?
🚀 3단계 - 로또(2등) #1141
Changes from all commits
20d2f4b
d7f1b78
4d39584
1dbdff1
17ffe03
cc4ec52
703f515
764631a
c6c5237
7176b68
414e25e
aabe9dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
package lotto | ||
|
||
import lotto.domain.Lotto | ||
import lotto.domain.Lottos | ||
|
||
object LottoVendingMachine { | ||
private const val LOTTO_PRICE = 1000 | ||
|
||
fun buyLotto(money: Int?): List<Lotto> { | ||
fun buyLotto(money: Int?): Lottos { | ||
requireNotNull(money) { | ||
"구매 금액은 null이 아니어야 함" | ||
} | ||
require(money >= LOTTO_PRICE) { | ||
"구매 금액은 1000 보다 커야 함" | ||
} | ||
|
||
return List(money / LOTTO_PRICE) { Lotto() } | ||
return Lottos(List(money / LOTTO_PRICE) { Lotto() }) | ||
} | ||
} | ||
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,22 @@ | ||||||||||
package lotto.domain | ||||||||||
|
||||||||||
class Lotto private constructor(val lottoNumbers: Set<LottoNumber>) { | ||||||||||
|
||||||||||
init { | ||||||||||
val lottoNumbersSize = lottoNumbers.size | ||||||||||
require(lottoNumbersSize == LOTTO_SIZE) | ||||||||||
} | ||||||||||
|
||||||||||
constructor() : this(LottoPreset.shuffled().take(6).sortedBy { it.number }.toSet()) | ||||||||||
|
||||||||||
constructor(vararg number: Int) : this(number.map { LottoNumber(it) }.toSet()) | ||||||||||
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. saso1 이러한 구문을 reference로 넘길 수 있습니다.
Suggested change
lambda로 작성하는 것과 어떤 차이가 있는지 참고할 수 있는 글을 소개해드릴게요 🙂 참고로 저는 가능한 곳에서는 꼭 함수 reference를 활용하는 것을 지향합니다. lambda로 가져온 데이터를 별도로 조작하지 않고 그대로 함수 시그니처로 넘긴다는 것을 드러낼 수 있기 때문입니다. |
||||||||||
|
||||||||||
fun countMatch(winningLotto: Lotto): Int { | ||||||||||
return lottoNumbers.intersect(winningLotto.lottoNumbers).count() | ||||||||||
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. saso2 https://kotlinlang.org/docs/coding-conventions.html#wrap-chained-calls
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
companion object { | ||||||||||
private val LottoPreset = List(LottoNumber.END_LOTTO_NUMBER) { LottoNumber(it + 1) } | ||||||||||
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. 여러 번 인스턴스화하지 않아도 되는 객체에 대해 고려해주셨네요 👏 |
||||||||||
private const val LOTTO_SIZE = 6 | ||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package lotto.domain | ||
|
||
@JvmInline | ||
value class LottoNumber(val number: Int) { | ||
init { | ||
require(number in START_LOTTO_NUMBER..END_LOTTO_NUMBER) | ||
} | ||
|
||
companion object { | ||
const val START_LOTTO_NUMBER = 1 | ||
const val END_LOTTO_NUMBER = 45 | ||
} | ||
} | ||
Comment on lines
+3
to
+13
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. LottoNumber 객체가 매번 생성될 때마다 1~45까지의 Range 객체가 매번 생성되어야 할까요? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package lotto.domain | ||
|
||
class LottoResult private constructor(private val result: Map<Prize, Int>) : Map<Prize, Int> by result { | ||
private val totalPrizeMoney: Long | ||
get() { | ||
return result.map { | ||
it.key.money.toLong() * it.value | ||
}.sum() | ||
} | ||
|
||
override fun get(key: Prize): Int { | ||
return result[key] ?: 0 | ||
} | ||
|
||
fun getRateOfReturn(money: Int): Float { | ||
return totalPrizeMoney.toFloat() / money | ||
} | ||
|
||
companion object { | ||
fun makeLottoResult( | ||
winningLotto: Lotto, | ||
bonusLottoNumber: LottoNumber, | ||
lottos: Lottos, | ||
): LottoResult { | ||
val result = lottos.lottos.groupingBy { lotto -> | ||
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 matchCount = lotto.countMatch(winningLotto) | ||
when (matchCount) { | ||
6 -> Prize.FIRST | ||
5 -> if (bonusLottoNumber in lotto.lottoNumbers) { | ||
Prize.SECOND | ||
} else { | ||
Prize.THIRD | ||
} | ||
|
||
4 -> Prize.FOURTH | ||
3 -> Prize.FIFTH | ||
else -> Prize.NONE | ||
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. 3단계 힌트를 참고하여 Prize에 메세지만 보내서 어떤 Prize인지 가져올 수 있도록 개선해보면 어떨까요? 외부에서 Prize를 판단하지 말고 메세지만 보내 보세요!
|
||
} | ||
}.eachCount() | ||
return LottoResult(result) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package lotto.domain | ||
|
||
class Lottos(lottos: List<Lotto>) { | ||
val lottos: List<Lotto> = lottos.toList() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package lotto.domain | ||
|
||
enum class Prize(val money: Int, val count: Int) { | ||
FIRST(2_000_000_000, 6), | ||
SECOND(30_000_000, 5), | ||
THIRD(1_500_000, 5), | ||
FOURTH(50_000, 4), | ||
FIFTH(5_000, 3), | ||
NONE(0, 0), | ||
; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,24 @@ | ||
package lotto.view | ||
|
||
import lotto.domain.Lotto | ||
import lotto.domain.LottoNumber | ||
|
||
class InputView { | ||
fun inputMoney(): Int { | ||
println("구입금액을 입력해 주세요.") | ||
val input = readlnOrNull()?.toIntOrNull() | ||
requireNotNull(input) | ||
return input | ||
} | ||
|
||
fun inputWinningNumbers(): List<Int> { | ||
return readlnOrNull()?.split(",")?.map { it.trim().toInt() } ?: emptyList() | ||
fun inputWinningNumbers(): Lotto { | ||
println("지난 주 당첨 번호를 입력해 주세요.") | ||
val winningNumbers: List<Int> = readlnOrNull()?.split(",")?.map { it.trim().toInt() } ?: emptyList() | ||
return Lotto(*winningNumbers.toIntArray()) | ||
} | ||
|
||
fun inputBonusNumber(): LottoNumber { | ||
println("보너스 볼을 입력해 주세요.") | ||
return LottoNumber(readlnOrNull()?.toIntOrNull() ?: 0) | ||
} | ||
} |
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.
외부에서
Lottos
를 직접 생성해주지 말고,Lottos
스스로 생성할 수 있도록 해보면 어떨까요?이 과정에서 팩토리 함수나 부 생성자를 사용해볼 수 있겠네요.
다음 글이 도움되길 바랍니다!
https://kt.academy/article/ek-factory-functions