-
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
Step4 수동 로또 #1134
base: goodbyeyo
Are you sure you want to change the base?
Step4 수동 로또 #1134
Changes from 24 commits
99369f0
005d6e4
e7da511
bce5ce9
89f9d1c
d978556
847f352
86bc2a4
86f6958
caac3eb
6f6d991
57f7cea
b0d50ff
2eedaa5
74cdcd6
262e88d
e9abd55
6677340
e2fddbb
a115791
cfee66b
c6f4996
3e9cf86
84a5ddf
07bd5fb
08c5320
69535ed
82770fe
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 |
---|---|---|
@@ -1,14 +1,7 @@ | ||
package lotto | ||
|
||
import lotto.controller.LottoController | ||
import lotto.view.InputView | ||
import lotto.view.OutputView | ||
|
||
fun main() { | ||
val amount = InputView.getUserAmount() | ||
val lottoTickets = LottoController.purchaseLotto(amount) | ||
OutputView.printPurchaseResult(lottoTickets) | ||
val winningLotto = InputView.getUserWinningLotto() | ||
val lottoResults = LottoController.calculateLottoRank(lottoTickets, winningLotto) | ||
OutputView.printResults(lottoResults, amount) | ||
LottoController.startLottoGame() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
package lotto.controller | ||
|
||
import lotto.domain.LottoResults | ||
import lotto.domain.LottoTickets | ||
import lotto.domain.WinningLotto | ||
import lotto.view.InputView | ||
import lotto.view.OutputView | ||
|
||
object LottoController { | ||
fun purchaseLotto(amount: Int): LottoTickets { | ||
return LottoTickets.purchase(amount) | ||
} | ||
|
||
fun calculateLottoRank( | ||
lottoTickets: LottoTickets, | ||
winningLotto: WinningLotto, | ||
): LottoResults { | ||
return lottoTickets.calculateLottoRank(winningLotto) | ||
fun startLottoGame() { | ||
val purchasedDetail = InputView.getPurchaseDetail() | ||
val autoLottoTickets = LottoTickets.generateAutoLottoTickets(purchasedDetail.autoLottoQuantity) | ||
val lottoTickets = LottoTickets(purchasedDetail.manualLottoTickets + autoLottoTickets) | ||
OutputView.printPurchaseResult(lottoTickets, purchasedDetail) | ||
val winningLotto = InputView.getUserWinningLotto() | ||
val lottoResults = lottoTickets.calculateLottoRank(winningLotto) | ||
OutputView.printResults(lottoResults, purchasedDetail.purchaseAmount) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package lotto.dto | ||
|
||
import lotto.domain.LottoTickets | ||
import lotto.domain.LottoTickets.Companion.LOTTO_TICKET_PRICE | ||
|
||
data class PurchaseDetail( | ||
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.
https://velog.io/@paulus0617/kotlin-getter-setter 요글을 읽어보시면 그래서 코틀린 진영에서는 필드가 아닌 프로퍼티라는 네이밍으로 사용하고 있습니다 :) 디컴파일 해보시면 실제 javaCode에서는 혹시 제가 질문을 잘 못이해한거라면 다시한번 말씀부탁드림니다! |
||
val purchaseAmount: Int, | ||
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. https://edu.nextstep.camp/s/l3Ppxbm8/ls/ROS71xiT 에 보면 |
||
val autoLottoQuantity: Int, | ||
val manualLottoQuantity: Int, | ||
val manualLottoTickets: LottoTickets, | ||
) { | ||
init { | ||
require(manualLottoQuantity * LOTTO_TICKET_PRICE <= purchaseAmount) { "수동으로 구입할 로또의 수량이 구입금액보다 많습니다" } | ||
require(autoLottoQuantity <= purchaseAmount / LOTTO_TICKET_PRICE) { "자동으로 구입할 로또의 수량이 구입금액보다 많습니다" } | ||
} | ||
constructor( | ||
purchaseAmount: Int, | ||
manualLottoTickets: LottoTickets, | ||
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. 부생성자와 팩터리 메서드 두가지 방식으로 구현이 가능했을 것 같은데요! 부생성자 방식을 선택하신 이유가 있을까요! 혹은 상욱님만의 기준이 있을까요? |
||
) : this( | ||
purchaseAmount, | ||
purchaseAmount / LOTTO_TICKET_PRICE - manualLottoTickets.size, | ||
manualLottoTickets.size, | ||
manualLottoTickets, | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,40 @@ package lotto.view | |
|
||
import lotto.domain.LottoNumber | ||
import lotto.domain.LottoTicket | ||
import lotto.domain.LottoTickets | ||
import lotto.domain.WinningLotto | ||
import lotto.dto.PurchaseDetail | ||
|
||
object InputView { | ||
fun getUserAmount(): Int { | ||
private const val DELIMITER = "," | ||
|
||
fun getPurchaseDetail(): PurchaseDetail { | ||
val purchaseAmount = getPurchaseAmount() | ||
val manualLottoQuantity = getManualLottoQuantity() | ||
val manualLottoTickets = getManualLottoTickets(manualLottoQuantity) | ||
return PurchaseDetail(purchaseAmount, manualLottoTickets) | ||
} | ||
|
||
private fun getManualLottoTickets(manualLottoQuantity: Int): LottoTickets { | ||
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. LottoTicket을 만드는 책임이 View에 있는 것 같아요! 자동로또와 동일�한 레벨로 옮겨보면 어떨까요? |
||
val manualLottoTickets = mutableListOf<LottoTicket>() | ||
println("수동으로 구매할 번호를 입력해 주세요.") | ||
repeat(manualLottoQuantity) { | ||
val line = readlnOrNull()?.takeIf { it.isNotBlank() } ?: throw IllegalArgumentException("로또 번호를 입력해주세요.") | ||
val numbers = | ||
line.split(DELIMITER) | ||
.map { it.trim().toIntOrNull() ?: throw IllegalArgumentException("로또 번호는 숫자여야 합니다.") } | ||
manualLottoTickets.add(LottoTicket.from(numbers.toSet())) | ||
} | ||
return LottoTickets(manualLottoTickets) | ||
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. https://kotlinlang.org/docs/constructing-collections.html#initializer-functions-for-lists 위 문서를 참고하여 mutableListOf없이 만드는 법도 알아보시면 좋을 것 같습니다! |
||
} | ||
|
||
private fun getManualLottoQuantity(): Int { | ||
println("수동으로 구매할 로또 수를 입력해 주세요.") | ||
val quantity = readln() | ||
return quantity.toIntOrNull() ?: throw IllegalArgumentException("유효한 숫자를 입력해주세요") | ||
} | ||
|
||
private fun getPurchaseAmount(): Int { | ||
println("구입 금액을 입력해 주세요.") | ||
val amount = readln() | ||
return amount.toIntOrNull() ?: throw IllegalArgumentException("구입 금액이 유효하지 않습니다. 숫자를 입력해주세요") | ||
|
@@ -14,7 +44,7 @@ object InputView { | |
fun getUserWinningLotto(): WinningLotto { | ||
println("지난 주 당첨 번호를 입력해 주세요.") | ||
val winningLottoNumbers: String = readln() | ||
val numbers = winningLottoNumbers.split(",").map { it.toInt() }.toSet() | ||
val numbers = winningLottoNumbers.split(DELIMITER).map { it.toInt() }.toSet() | ||
|
||
println("보너스 볼을 입력해 주세요.") | ||
val bonusNumber: String = readln() | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package lotto.dto | ||
|
||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.matchers.shouldBe | ||
import lotto.domain.LottoTicket | ||
import lotto.domain.LottoTickets | ||
import org.junit.jupiter.api.Test | ||
|
||
class PurchaseDetailTest { | ||
@Test | ||
fun `수동으로 구입할 로또의 수량은 구입금액보다 많을 수 없다`() { | ||
shouldThrow<IllegalArgumentException> { | ||
PurchaseDetail( | ||
purchaseAmount = 1_000, | ||
manualLottoTickets = | ||
LottoTickets( | ||
listOf( | ||
LottoTicket.from(setOf(1, 2, 3, 4, 5, 7)), | ||
LottoTicket.from(setOf(1, 2, 3, 4, 5, 9)), | ||
LottoTicket.from(setOf(1, 2, 3, 4, 8, 9)), | ||
), | ||
), | ||
) | ||
}.also { | ||
it.message shouldBe "수동으로 구입할 로또의 수량이 구입금액보다 많습니다" | ||
} | ||
} | ||
|
||
@Test | ||
fun `자동으로 구입할 로또의 수량은 구입금액보다 많을 수 없다`() { | ||
shouldThrow<IllegalArgumentException> { | ||
PurchaseDetail( | ||
purchaseAmount = 5_000, | ||
autoLottoQuantity = 10, | ||
manualLottoQuantity = 3, | ||
manualLottoTickets = | ||
LottoTickets( | ||
listOf( | ||
LottoTicket.from(setOf(1, 2, 3, 4, 5, 7)), | ||
LottoTicket.from(setOf(1, 2, 3, 4, 5, 9)), | ||
LottoTicket.from(setOf(1, 2, 3, 4, 8, 9)), | ||
), | ||
), | ||
) | ||
}.also { | ||
it.message shouldBe "자동으로 구입할 로또의 수량이 구입금액보다 많습니다" | ||
} | ||
} | ||
|
||
@Test | ||
fun `만원 구매금액으로 수동 로또 3장을 구입하면 자동 로또는 7장이다`() { | ||
val purchaseDetail = | ||
PurchaseDetail( | ||
purchaseAmount = 10_000, | ||
manualLottoTickets = | ||
LottoTickets( | ||
listOf( | ||
LottoTicket.from(setOf(1, 2, 3, 4, 5, 7)), | ||
LottoTicket.from(setOf(1, 2, 3, 4, 5, 9)), | ||
LottoTicket.from(setOf(1, 2, 3, 4, 8, 9)), | ||
), | ||
), | ||
) | ||
purchaseDetail.autoLottoQuantity shouldBe 7 | ||
} | ||
} |
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.
해당 함수는 이제 미사용으로 보이네요! :)