-
Notifications
You must be signed in to change notification settings - Fork 322
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
[Step2] 로또(자동) #724
base: kyudong3
Are you sure you want to change the base?
[Step2] 로또(자동) #724
Conversation
* InputView 생성
* 로또 1장의 가격은 1000원이다 * Lotto -> Lottery로 이름 변경
* 구매한 금액에 해당하는 로또 갯수 반환
* 구매 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다
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.
return readln().split(",").map { | ||
it.trim().toInt() | ||
} |
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.
메서드를 연속해서 호출하는 경우에는 행 구분을 해주세요!
https://kotlinlang.org/docs/coding-conventions.html#wrap-chained-calls
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.
kotlin convention을 다시 한 번 제대로 읽어봐야겠어요 감사합니다! 🙏🏼
val lotteryTickets = mutableListOf<Lottery>() | ||
repeat(count) { | ||
lotteryTickets.add( | ||
Lottery((MIN_LOTTERY_NUMBER..MAX_LOTTERY_NUMBER).shuffled().take(6).sorted()) | ||
) | ||
} |
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.
가변적인 MutableList
를 사용하는 것 보다 불변인 List
로 생성하는 것이 더 안정적일 것 같습니다~!
아래와 같이 개선해보면 좋을것 같습니다.
val lotteryTickets = mutableListOf<Lottery>() | |
repeat(count) { | |
lotteryTickets.add( | |
Lottery((MIN_LOTTERY_NUMBER..MAX_LOTTERY_NUMBER).shuffled().take(6).sorted()) | |
) | |
} | |
val lotteryTickets = List(count) { | |
Lottery((MIN_LOTTERY_NUMBER..MAX_LOTTERY_NUMBER).shuffled().take(6).sorted()) | |
} |
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.
리턴할 때 UserLottery에 List 타입으로 선언해 추가해주었는데, buy 메소드 안에서도 가변적인 리스트보다 불변 리스트를 생성해 리턴하면 더 좋을 것 같네요!! 👍🏼
fun getWinStatistic( | ||
lotteryTickets: List<Lottery>, | ||
lastWinnerNumbers: List<Int> | ||
): Pair<List<LotteryResult>, Double> { | ||
val winResult = checkLotteryTickets(lotteryTickets, lastWinnerNumbers) | ||
|
||
val lotteryResults = mutableListOf<LotteryResult>() | ||
var sum = 0 | ||
winResult.forEach { (count, matchCount) -> | ||
val prize = WinnerPrize.getPrize(count).money | ||
val message = "${count}개 일치 (${prize}원) - ${matchCount}개" | ||
lotteryResults.add(LotteryResult(prize, matchCount, message)) | ||
sum += (prize * matchCount) | ||
} | ||
|
||
return lotteryResults.toList() to sum.toDouble() |
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.
List<Lottery>
를 가지고 있는 일급컬렉션에게 winningNumbers를 전달하여 위 책임을 위임할 수 있을것 같습니다.
그러면 전체적인 코드가 조금 더 간결해 질 수 있을것 같습니다.
@@ -0,0 +1,18 @@ | |||
package lotto | |||
|
|||
enum class WinnerPrize(val money: Int) { |
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.
1등에 여러번 당첨되면 Int
로는 감당하기 힘들어 질 것 같네요 😄
3 -> LAST | ||
4 -> THIRD | ||
5 -> SECOND | ||
else -> FIRST |
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.
WinnerPrize
가 count
에 대한 정보도 가지고 있을 수 있지 않을까요?
@@ -0,0 +1,20 @@ | |||
package lotto |
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.
패키지를 구조를 변경하여 domain 과 view 를 분리해주세요.
): List<Pair<Int, Int>> = | ||
lotteryTickets.map { lottery -> | ||
lottery.numbers.intersect( | ||
lastWinnerNumbers.sorted().toSet() |
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.
정렬할 필요가 있을까요??
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.
다시 생각해보니 sort할 필요는 없는 것 같네요..! 🙏🏼
3 -> LAST | ||
4 -> THIRD | ||
5 -> SECOND | ||
else -> FIRST |
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.
FIRST
는 전부 맞은것으로 판별하면 되지 않을까요?
lottery.numbers.intersect( | ||
lastWinnerNumbers.sorted().toSet() | ||
).count() | ||
}.filter { it >= 3 } |
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.
3개가 넘게 일치하는지를 이곳에서 판별하지 않고
WinnerPrize
에게 물어보는 형식으로 변경할 수 있을것 같습니다.
private fun checkLotteryTickets( | ||
lotteryTickets: List<Lottery>, | ||
lastWinnerNumbers: List<Int> | ||
): List<Pair<Int, Int>> = |
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.
Pair
로 Int
와 Int
가 쌍으로 나가는것보다 조금더 의미있는 객체로 내보내는것이 코드를 이해하기 더 수월할 것 같습니다.
step2 로또(자동) 리뷰 잘 부탁드립니다! 🙏🏼