-
Notifications
You must be signed in to change notification settings - Fork 323
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
step3: 로또(2등) 구현 #948
base: kdh85
Are you sure you want to change the base?
step3: 로또(2등) 구현 #948
Changes from all commits
8190087
6897ca8
ae8dcd7
a97f954
215b40b
bfc4c5e
4668731
f8ad71e
fbafaf0
38a2aac
be9ab91
5b65c3f
f0f3a9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,50 @@ | ||
package lotto.domain | ||
|
||
class Lotto private constructor( | ||
private val lottoNumbers: Set<LottoNumber> = HashSet() | ||
) { | ||
import lotto.enums.Rank | ||
|
||
val lotto: Set<LottoNumber> = lottoNumbers | ||
class Lotto( | ||
val lottoNumbers: Set<LottoNumber> = HashSet() | ||
) { | ||
init { | ||
require(lottoNumbers.size == MAX_NUMBERS_COUNT) { | ||
"로또를 올바르게 생성하려면 반드시 6개의 번호를 넣어주세요." | ||
} | ||
|
||
fun makeMatchCountByNumbers(winningLotto: Lotto): Int { | ||
var count = 0 | ||
winningLotto.lotto.forEach { winNumber -> | ||
count += countByMatchNumbers(winNumber) | ||
require(lottoNumbers.groupBy { it }.size == 6) { | ||
"중복된 숫자를 제외한 6개의 숫자를 입력해 주세요" | ||
} | ||
return count | ||
} | ||
|
||
private fun countByMatchNumbers(winNumber: LottoNumber): Int { | ||
return lotto.count { | ||
it == winNumber | ||
fun makeMatchCountByNumbers(lotto: Lotto): Int { | ||
return this.lottoNumbers.intersect(lotto.lottoNumbers) | ||
.count() | ||
} | ||
|
||
|
||
fun matchCounts(bundle: List<Lotto>): List<Int> { | ||
val counts = mutableListOf<Int>() | ||
bundle.forEach { lotto -> | ||
val countByNumbers = lotto.makeMatchCountByNumbers(this)//0~6개 | ||
|
||
val rank = Rank.values().firstOrNull { | ||
it.matchCount == countByNumbers | ||
} ?: Rank.NONE_RANK | ||
|
||
counts.add(countByNumbers) | ||
} | ||
return counts | ||
} | ||
|
||
fun isMatchBonus(bonusNumber: LottoNumber): Boolean { | ||
return lottoNumbers.contains(bonusNumber) | ||
Comment on lines
+38
to
+39
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. "보너스 번호"를 가지고 있는지는 로또 티켓의 관심사일까요? |
||
} | ||
|
||
|
||
companion object { | ||
|
||
private const val MAX_NUMBERS_COUNT = 6 | ||
|
||
fun from(numbers: List<Int>): Lotto { | ||
|
||
require(numbers.size == MAX_NUMBERS_COUNT) { | ||
"로또를 올바르게 생성하려면 반드시 6개의 번호를 넣어주세요." | ||
} | ||
|
||
require(numbers.groupBy { it }.size == 6) { | ||
"중복된 숫자를 제외한 6개의 숫자를 입력해 주세요" | ||
} | ||
|
||
val lottoNumbers = numbers.map { | ||
LottoNumber.from(it) | ||
}.toSet() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package lotto.domain | ||
|
||
import lotto.enums.Rank | ||
|
||
class LottoRecord( | ||
val rank: Rank, | ||
quantity: Int = 0, | ||
) { | ||
Comment on lines
+5
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. 기록에 대한 객체를 만든뒤 거기에 계속 수를 더하는 가변 객체를 만들기 보다, |
||
init { | ||
require(quantity >= 0) { | ||
"등급별 수량은 0이상 입니다." | ||
} | ||
} | ||
|
||
var quantity = quantity | ||
private set | ||
Comment on lines
+9
to
+16
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. 멤버변수는 생성자 보다 위에 위치하는 것이 코틀린 컨벤션입니다 :) |
||
|
||
fun matchCount(): Int { | ||
return rank.matchCount | ||
} | ||
|
||
fun addQuantityByCount(count: Int) { | ||
quantity += count | ||
} | ||
|
||
fun reward(): Int { | ||
return rank.reward | ||
} | ||
|
||
fun totalReward(): Int { | ||
return rank.reward * quantity | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package lotto.domain | ||
|
||
import lotto.enums.Rank | ||
import kotlin.math.floor | ||
|
||
class LottoRecords( | ||
private val lottoRecords: Set<LottoRecord> | ||
) { | ||
fun calculateRecords(recordsByRank: List<Rank>): Set<LottoRecord> { | ||
lottoRecords.forEach { lottoRecord -> | ||
addQuantityByRank(recordsByRank, lottoRecord) | ||
} | ||
return lottoRecords | ||
} | ||
|
||
private fun addQuantityByRank( | ||
recordsByRank: List<Rank>, | ||
lottoRecord: LottoRecord | ||
) { | ||
val matchCount = recordsByRank.count { | ||
it == lottoRecord.rank | ||
} | ||
lottoRecord.addQuantityByCount(matchCount) | ||
} | ||
|
||
fun calculateRateOfReturn(amount: Int): Double { | ||
val sumTotalReward = lottoRecords.sumOf { | ||
it.totalReward() | ||
}.toDouble() | ||
return floor(sumTotalReward / amount * 100.0) / 100.0 | ||
} | ||
|
||
companion object { | ||
|
||
fun fromRank(): LottoRecords { | ||
val lottoRecords = mutableSetOf<LottoRecord>() | ||
Rank.values().forEach { | ||
lottoRecords.add(LottoRecord(it)) | ||
} | ||
return LottoRecords(lottoRecords) | ||
} | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package lotto.domain | ||
|
||
import lotto.enums.Rank | ||
|
||
class WinningLotto( | ||
val winningLotto: Lotto, | ||
val bonusNumber: LottoNumber | ||
) { | ||
init { | ||
require(!winningLotto.lottoNumbers.contains(bonusNumber)) { | ||
"로또 번호와 보너스번호는 중복이 될 수 없습니다. 중복된 번호 : $bonusNumber" | ||
} | ||
} | ||
|
||
fun matchByRank(bundle: LottoBundle): List<Rank> { | ||
return bundle.findAllByMatchRanks(this) | ||
} | ||
} |
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.
로또 티켓이 당첨인지에 대한 검증은 당첨 티켓이 갖게해보는 건 어떨까요?
현실 세계에서는 사람이 당첨번호를 보고, 티켓이 몇등인지 알아내지만,
당첨 티켓에게 비교할 로또를 주면서 어떤 결과인지 메시지를 던져 물어보는 건 컴퓨터의 세계에서 자연스러운 일이라 생각해요!