Skip to content
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λ“±) #792

Open
wants to merge 1 commit into
base: goyounggyeong
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/kotlin/lotto/LottoPlay.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ fun main() {
printLottoNumber(purchasedLottos)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

움 μ œκ°€ 2λ‹¨κ³„μ—μ„œ ν”Όλ“œλ°± λ“œλ¦¬μ§€ λͺ»ν•œλΆ€λΆ„이 μžˆμ–΄μ„œ μš”κΈ°μ— λ‚¨κΈΈκ²Œμš”!!

  • COMMA_SEPARATORκ°€ LottoPlay에 μœ„μΉ˜ν•  μ΄μœ κ°€ μžˆμ„κΉŒμš”? StringUtilμ—μ„œλ§Œ μ‚¬μš©λ˜λŠ” 것 κ°™μ•„μš”!
  • purchasedLottosλŠ” MutableListκ°€ μ•„λ‹ˆλΌ List여도 μΆ©λΆ„ν•  것 κ°™μ•„μš” :)


val lastWeekWinningString = LottoVendor.readLastWeekWinningString()
val lastWeekBonusNumber = LottoVendor.readLastWeekBonusBallNumber()
val lastWeekWinningNumbers: List<Int> = toIntList(lastWeekWinningString)
purchasedLottos.forEach { it.setLottoPrize(lastWeekWinningNumbers) }
purchasedLottos.forEach { it.setLottoPrize(lastWeekWinningNumbers, lastWeekBonusNumber) }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ν˜„μž¬λŠ” lastWeekWinningNumbersλ₯Ό List둜 λ„˜κ²¨μ£Όκ³  μžˆμ§€λ§Œ, ν•΄λ‹Ή λ³€μˆ˜λ„ ν•˜λ‚˜μ˜ Lotto객체둜 λ§Œλ“€μ–΄λ³΄λ©΄ μ–΄λ–¨κΉŒμš”??
WinningLotto와 같이 Lotto와 BonusNumberλ₯Ό 가지고 μžˆλŠ” 객체λ₯Ό λ§Œλ“€μ–΄λ³Ό 수 μžˆμ„κ²ƒ κ°™μ•„μš”!

val winningRatio = LottoPlayResultAnalysis.getWinningRatio(purchaseAmount, purchasedLottos)
LottoPlayer.printResult(purchasedLottos, winningRatio)
}
13 changes: 11 additions & 2 deletions src/main/kotlin/lotto/model/Lotto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ class Lotto {
var lottoPrize: LottoPrize = LottoPrize.NONE_PRIZE
val purchasedLottoNumbers: List<Int> = LOTTO_NUMBERS.shuffled().take(LOTTO_NUMBER_COUNT).sorted()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ§€λ‚œμ£Ό λ¦¬λ·°μ—μ„œ LottoNumberλ₯Ό κ°’κ°μ²΄λ‘œ wrappingν•˜λŠ” μ˜ˆμ‹œλ₯Ό λ³΄μ—¬μ€¬λŠ”λ°μš”! μ΄λ²ˆμ— 값객체λ₯Ό λ§Œλ“€μ–΄λ³΄μ‹œλ©΄ μ–΄λ–¨κΉŒμš”?


fun setLottoPrize(lastWeekWinningNumbers: List<Int>): LottoPrize {
private fun match(lastWeekWinningNumbers: List<Int>): Int {
val matchingNumbers = purchasedLottoNumbers.intersect(lastWeekWinningNumbers.toSet()).size
lottoPrize = LottoPrize.getLottoPrize(matchingNumbers)
return matchingNumbers
}

fun setLottoPrize(lastWeekWinningNumbers: List<Int>, lastWeekBonusNumber: Int): LottoPrize {
val matchingNumbers = match(lastWeekWinningNumbers)
val bonusBallMatching: Int =
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Booleanνƒ€μž…μ΄μ—¬λ„ μΆ©λΆ„ν•˜μ§€ μ•Šμ„κΉŒμš”?
  • ν•΄λ‹Ήλ³€μˆ˜μ—λ§Œ 리턴값을 λͺ…μ‹œν•˜μ‹  μ΄μœ κ°€ μžˆμ„κΉŒμš”?

if (purchasedLottoNumbers.contains(lastWeekBonusNumber)) CONTAIN_BALL else NOT_CONTAIN_BALL
lottoPrize = LottoPrize.getLottoPrize(matchingNumbers, bonusBallMatching)
return lottoPrize
}

companion object {
private val LOTTO_NUMBERS: List<Int> = (1..45).toList()
private const val LOTTO_NUMBER_COUNT = 6
private const val CONTAIN_BALL = 1
private const val NOT_CONTAIN_BALL = 0
}
}
29 changes: 20 additions & 9 deletions src/main/kotlin/lotto/model/LottoPrize.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
package lotto.model

enum class LottoPrize(val matchingCount: Int, val prizeAmount: Long) {
FIRST(6, 2000000000),
SECOND(5, 1500000),
THIRD(4, 50000),
FOURTH(3, 5000),
NONE_PRIZE(0, 0);
enum class LottoPrize(val matchingCount: Int, val bonusMatching: Int = 0, val prizeAmount: Long) {
FIRST(6, 0, 2000000000),
BONUS(5, 1, 30000000),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

보톡 λ‘œλ˜μ—μ„œ 2등을 5개 일치 + λ³΄λ„ˆμŠ€ 일치둜 μ΄μ•ΌκΈ°ν•˜λŠ”λ°μš”! ν•΄λ‹Ή enum의 값이 BONUSκ°€ λ˜μ–΄μ•Όν•˜μ§€ μ•Šμ„κΉŒμš”?

SECOND(5, 0, 1500000),
THIRD(4, 0, 50000),
FOURTH(3, 0, 5000),
NONE_PRIZE(0, 0, 0);

companion object {
private const val PRIZING_CONT = 3
fun getLottoPrize(matchingCount: Int): LottoPrize {
if(matchingCount >= PRIZING_CONT) { return NONE_PRIZE }
return values().find { it.matchingCount == matchingCount }!!
private const val SECOND_PRIZE_MATCHING_COUNT = 5
fun getLottoPrize(matchingCount: Int, lastWeekBonusNumber: Int): LottoPrize {
if (matchingCount < PRIZING_CONT) {
return NONE_PRIZE
}

val prize = values().find { it.matchingCount == matchingCount } ?: return NONE_PRIZE

if (matchingCount == SECOND_PRIZE_MATCHING_COUNT) {
if ( lastWeekBonusNumber > 0) return BONUS
else return SECOND
}
return prize
}
}
}
2 changes: 0 additions & 2 deletions src/main/kotlin/lotto/service/LottoTicketCountCalculator.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package lotto.service

object LottoTicketCountCalculator {

private const val LOTTO_PRICE = 1000

fun getCount(purchaseAmount: Int) : Int = purchaseAmount / LOTTO_PRICE

}
3 changes: 2 additions & 1 deletion src/main/kotlin/lotto/view/LottoPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ object LottoPlayer {
println("3개 일치 (5000원)- ${LottoPlayResultAnalysis.staticPrize(winningLottos, 5000)}")
println("4개 일치 (50000원)- ${LottoPlayResultAnalysis.staticPrize(winningLottos, 50000)}")
println("5개 일치 (1500000원)- ${LottoPlayResultAnalysis.staticPrize(winningLottos, 1500000)}")
println("5개 일치, λ³΄λ„ˆμŠ€ λ³Ό 일치(30000000원) - ${LottoPlayResultAnalysis.staticPrize(winningLottos, 30000000)}")
println("6개 일치 (2000000000원)-${LottoPlayResultAnalysis.staticPrize(winningLottos, 2000000000)}")
println()
println("총 수읡λ₯ μ€ ${formatWinningRatio(winningRatio)} μž…λ‹ˆλ‹€.(기쀀이 1이기 λ•Œλ¬Έμ— 결과적으둜 ${if (winningRatio >= 1) "이득" else "손해"}λΌλŠ” μ˜λ―Έμž„)")
}

}
}
11 changes: 8 additions & 3 deletions src/main/kotlin/lotto/view/LottoVendor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ object LottoVendor {
}

fun printLottoNumber(purchasedLottos:List<Lotto>) {
purchasedLottos.forEach { print(it) }
purchasedLottos.forEach { println(it.purchasedLottoNumbers.toString()) }
}

fun readLastWeekWinningString(): String {
println("μ§€λ‚œ μ£Ό 당첨 번호λ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš”.")
return readLine().toString()
return readln()
}
}

fun readLastWeekBonusBallNumber(): Int {
println("λ³΄λ„ˆμŠ€ 볼을 μž…λ ₯ν•΄ μ£Όμ„Έμš”.")
return readln().toInt()
}
}
42 changes: 36 additions & 6 deletions src/test/kotlin/lotto/LottoPlayTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class LottoPlayTest {
@ParameterizedTest
@ValueSource(ints = [2])
fun testNotPrizeAmount(matchingCount: Int) {
Assertions.assertThat(LottoPrize.getLottoPrize(matchingCount).prizeAmount).isEqualTo(0)
Assertions.assertThat(LottoPrize.getLottoPrize(matchingCount, 0).prizeAmount).isEqualTo(0)
}

@DisplayName(value = "μ§€λ‚œμ£Ό 당첨 λ²ˆν˜Έμ™€ 3개 일치 μƒκΈˆ 5000원 ")
Expand All @@ -45,7 +45,12 @@ class LottoPlayTest {
val winningLottoNumber = Lotto()
val lastWeekWinningLottoNumber = winningLottoNumber.purchasedLottoNumbers.toMutableList()

Assertions.assertThat(winningLottoNumber.setLottoPrize(lastWeekWinningLottoNumber.subList(0,3)).prizeAmount)
Assertions.assertThat(
winningLottoNumber.setLottoPrize(
lastWeekWinningLottoNumber.subList(0, 3),
lastWeekWinningLottoNumber.last() + 1
).prizeAmount
)
.isEqualTo(matchingCount.toLong())
}

Expand All @@ -56,7 +61,12 @@ class LottoPlayTest {
val winningLottoNumber = Lotto()
val lastWeekWinningLottoNumber = winningLottoNumber.purchasedLottoNumbers.toMutableList()

Assertions.assertThat(winningLottoNumber.setLottoPrize(lastWeekWinningLottoNumber.subList(0,4)).prizeAmount)
Assertions.assertThat(
winningLottoNumber.setLottoPrize(
lastWeekWinningLottoNumber.subList(0, 4),
lastWeekWinningLottoNumber.last() + 1
).prizeAmount
)
.isEqualTo(matchingCount.toLong())
}

Expand All @@ -67,7 +77,28 @@ class LottoPlayTest {
val winningLottoNumber = Lotto()
val lastWeekWinningLottoNumber = winningLottoNumber.purchasedLottoNumbers.toMutableList()

Assertions.assertThat(winningLottoNumber.setLottoPrize(lastWeekWinningLottoNumber.subList(0,5)).prizeAmount)
Assertions.assertThat(
winningLottoNumber.setLottoPrize(
lastWeekWinningLottoNumber.subList(0, 5),
lastWeekWinningLottoNumber.last() + 1
).prizeAmount
)
.isEqualTo(matchingCount.toLong())
}

@DisplayName(value = "μ§€λ‚œμ£Ό 당첨 λ²ˆν˜Έμ™€ 5개 λ³΄λ„ˆμŠ€λ³Ό 일치 μƒκΈˆ 3000000원")
@ParameterizedTest
@ValueSource(strings = ["30000000"])
fun testBonusPrizeAmount(matchingCount: String) {
val winningLottoNumber = Lotto()
val lastWeekWinningLottoNumber = winningLottoNumber.purchasedLottoNumbers.toMutableList()

Assertions.assertThat(
winningLottoNumber.setLottoPrize(
lastWeekWinningLottoNumber.subList(0, 5),
lastWeekWinningLottoNumber.last()
).prizeAmount
)
.isEqualTo(matchingCount.toLong())
}

Expand All @@ -77,8 +108,7 @@ class LottoPlayTest {
fun testFirstPrizeAmount(matchingCount: String) {
val winningLottoNumber = Lotto()
val lastWeekWinningLottoNumber = winningLottoNumber.purchasedLottoNumbers.toList()
Assertions.assertThat(winningLottoNumber.setLottoPrize(lastWeekWinningLottoNumber).prizeAmount)
Assertions.assertThat(winningLottoNumber.setLottoPrize(lastWeekWinningLottoNumber, 1).prizeAmount)
.isEqualTo(matchingCount.toLong())
}

}