-
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
π 4λ¨κ³ - λ‘λ(μλ) #1137
base: aimbe
Are you sure you want to change the base?
Changes from all commits
31194c9
ab0105e
3fcae1f
a516fb1
1f4ddda
361771b
11fced2
fd2be19
6c078aa
15f908d
5a3c02f
169d68e
04e9740
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,18 +1,23 @@ | ||
package lotto.controller | ||
|
||
import lotto.domain.LottoPrice | ||
import lotto.domain.LottoPurchaseManager | ||
import lotto.service.LottoService | ||
import lotto.view.InputView | ||
import lotto.view.ResultView | ||
|
||
fun main() { | ||
val purchaseAmount = InputView().readPurchaseAmount() | ||
val lottos = LottoService().purchase(LottoPrice(purchaseAmount)) | ||
ResultView().printPurchaseResult(lottos) | ||
val purchaseAmount = LottoPrice(InputView().readPurchaseAmount()) | ||
|
||
val manualCount = InputView().readManualLottoCount() | ||
val manualLottos = InputView().readManualLottoNumbers(manualCount) | ||
|
||
val lottos = LottoService(LottoPurchaseManager()).purchase(purchaseAmount, manualLottos) | ||
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. νμ¬ LottoService κ°μ²΄λ₯Ό λλ² μμ±ν΄μ μ¬μ©νκ³ μλλ°μ. |
||
ResultView().printPurchaseResult(manualCount, lottos) | ||
|
||
val winningNumbers = InputView().readWinningNumbers() | ||
val bonusBall = InputView().readBonusBall(winningNumbers) | ||
val winningResult = LottoService().checkWinning(lottos, winningNumbers, bonusBall) | ||
val winningResult = LottoService(LottoPurchaseManager()).checkWinning(lottos, winningNumbers, bonusBall) | ||
|
||
ResultView().printWinningStatistics(winningResult) | ||
ResultView().printProfitRate(winningResult.calculateProfitRate(purchaseAmount)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package lotto.domain | ||
|
||
import Lottos | ||
|
||
class LottoPurchaseManager { | ||
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 purchase( | ||
price: LottoPrice, | ||
manualLottos: Lottos, | ||
): Lottos { | ||
val totalCount = price.calculatePurchaseCount() | ||
val autoCount = totalCount - manualLottos.size | ||
require(autoCount >= 0) { "μλ ꡬ맀 κ°μκ° μ΄ κ΅¬λ§€ κ°λ₯ κ°μλ₯Ό μ΄κ³Όνμ΅λλ€." } | ||
|
||
val autoLottos = LottoFactory.create(autoCount) | ||
return Lottos(manualLottos + autoLottos) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -19,7 +19,16 @@ class Lottos(private val tickets: List<Lotto>) { | |||||||||||||
): Map<Rank, Int> { | ||||||||||||||
return tickets | ||||||||||||||
.map { it.match(winningNumber, bonusBall) } | ||||||||||||||
.groupBy { it } | ||||||||||||||
.mapValues { it.value.size } | ||||||||||||||
.groupingBy { | ||||||||||||||
it | ||||||||||||||
}.eachCount() | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
operator fun plus(lotto: Lottos): List<Lotto> { | ||||||||||||||
return tickets + lotto.tickets | ||||||||||||||
} | ||||||||||||||
Comment on lines
+27
to
+29
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. μλμ κ°μ΄ λ¦¬ν΄ νμ μ Lottosλ‘ λ³κ²½ν΄λ³΄λ©΄ μ΄λ¨κΉμ? π
Suggested change
|
||||||||||||||
|
||||||||||||||
companion object { | ||||||||||||||
fun from(lottos: List<Lotto>): Lottos = Lottos(lottos) | ||||||||||||||
} | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package lotto.view | ||
|
||
import Lottos | ||
import lotto.domain.Lotto | ||
import lotto.domain.LottoNumber | ||
|
||
|
@@ -29,4 +30,23 @@ class InputView { | |
require(!winningNumbers.numbers.contains(bonusBallNumber)) { "보λμ€ λ³Όμ λΉμ²¨ λ²νΈμ μ€λ³΅λ μ μμ΅λλ€." } | ||
return bonusBallNumber | ||
} | ||
|
||
fun readManualLottoCount(): Int { | ||
println("\nμλμΌλ‘ ꡬ맀ν λ‘λ μλ₯Ό μ λ ₯ν΄ μ£ΌμΈμ.") | ||
return readln().toInt() | ||
} | ||
|
||
fun readManualLottoNumbers(count: Int): Lottos { | ||
println("\nμλμΌλ‘ ꡬ맀ν λ²νΈλ₯Ό μ λ ₯ν΄ μ£ΌμΈμ.") | ||
|
||
val manualLottos = | ||
(1..count).map { | ||
val numbers = | ||
readln().split(",") | ||
.map { it.trim().toInt() } | ||
.map { LottoNumber(it) } | ||
Lotto(numbers) | ||
} | ||
return Lottos.from(manualLottos) | ||
Comment on lines
+42
to
+50
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. (μ΄λ―Έ μμμ μΈκΈνμμ§λ§) μ€μ λ‘ μ λ ₯ λ°μ κ°μ Domain ModelμΈ Lotto κ·Έλ¦¬κ³ Lottosλ‘ λ³κ²½νλ λ‘μ§μ Controllerμ μμΉμν€λ©΄ μ’μ κ² κ°μ΅λλ€. π |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import Lottos | |
import lotto.domain.Lotto | ||
import lotto.domain.LottoNumber | ||
import lotto.domain.LottoPrice | ||
import lotto.domain.LottoPurchaseManager | ||
import lotto.domain.Rank | ||
import lotto.service.LottoService | ||
import org.assertj.core.api.Assertions.assertThat | ||
|
@@ -17,12 +18,25 @@ class LottoGameTest { | |
} | ||
|
||
@ParameterizedTest | ||
@CsvSource("1000", "2000", "3000") | ||
fun `ꡬ맀ν λ‘λ λ§νΌ λλ€ν λ²νΈλ₯Ό μμ±νλ€`(money: Int) { | ||
@CsvSource( | ||
"2000, 1", | ||
"3000, 2", | ||
) | ||
fun `μλκ³Ό μλ λ‘λλ₯Ό ν©μ³μ ꡬ맀 κΈμ‘λ§νΌ λ‘λλ₯Ό μμ±νλ€`( | ||
money: Int, | ||
manualCount: Int, | ||
) { | ||
val lottoPrice = LottoPrice(money) | ||
val purchasedLottos = LottoService().purchase(lottoPrice) | ||
val manualLottos = | ||
List(manualCount) { | ||
Lotto(createLottoNumbers(1, 2, 3, 4, 5, 6)) | ||
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 purchasedLottos = LottoService(LottoPurchaseManager()).purchase(lottoPrice, Lottos(manualLottos)) | ||
|
||
assertThat(purchasedLottos.size).isEqualTo(lottoPrice.calculatePurchaseCount()) | ||
assertThat(purchasedLottos.lottos.take(manualCount)) | ||
.containsExactlyElementsOf(manualLottos) | ||
|
||
purchasedLottos.lottos.forEach { | ||
assertThat(it.numbers.distinct().size).isEqualTo(6) | ||
|
@@ -58,7 +72,7 @@ class LottoGameTest { | |
val winningNumbers = Lotto(createLottoNumbers(1, 2, 3, 4, 5, 6)) | ||
val bonusNumber = LottoNumber(7) | ||
|
||
val winningResult = LottoService().checkWinning(lottos, winningNumbers, bonusNumber) | ||
val winningResult = LottoService(LottoPurchaseManager()).checkWinning(lottos, winningNumbers, bonusNumber) | ||
|
||
assertThat(winningResult.getWinningCount(Rank.NONE)).isEqualTo(0) | ||
} | ||
|
@@ -77,7 +91,7 @@ class LottoGameTest { | |
val winningNumbers = Lotto(createLottoNumbers(1, 2, 3, 4, 5, 6)) | ||
val bonusNumber = LottoNumber(7) | ||
|
||
val winningResult = LottoService().checkWinning(lottos, winningNumbers, bonusNumber) | ||
val winningResult = LottoService(LottoPurchaseManager()).checkWinning(lottos, winningNumbers, bonusNumber) | ||
|
||
assertThat(winningResult.getWinningCount(Rank.FIRST)).isEqualTo(1) | ||
assertThat(winningResult.getWinningCount(Rank.SECOND)).isEqualTo(1) | ||
|
@@ -96,7 +110,7 @@ class LottoGameTest { | |
val winningNumbers = Lotto(createLottoNumbers(1, 2, 3, 4, 5, 6)) | ||
val bonusNumber = LottoNumber(7) | ||
|
||
val winningResult = LottoService().checkWinning(lottos, winningNumbers, bonusNumber) | ||
val winningResult = LottoService(LottoPurchaseManager()).checkWinning(lottos, winningNumbers, bonusNumber) | ||
|
||
assertThat(winningResult.getWinningCount(Rank.FIFTH)).isEqualTo(0) | ||
assertThat(winningResult.getWinningCount(Rank.FOURTH)).isEqualTo(0) | ||
|
@@ -118,7 +132,7 @@ class LottoGameTest { | |
val winningNumbers = Lotto(createLottoNumbers(1, 2, 3, 4, 5, 6)) | ||
val bonusNumber = LottoNumber(7) | ||
|
||
val winningResult = LottoService().checkWinning(lottos, winningNumbers, bonusNumber) | ||
val winningResult = LottoService(LottoPurchaseManager()).checkWinning(lottos, winningNumbers, bonusNumber) | ||
|
||
assertThat(winningResult.getWinningCount(Rank.FIRST)).isEqualTo(1) | ||
assertThat(winningResult.getWinningCount(Rank.SECOND)).isEqualTo(1) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package lotto.domain | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
|
||
class RankTest { | ||
@Test | ||
fun `6κ° λ§μΆλ©΄ 1λ±μ΄λ€`() { | ||
val rank = Rank.from(matchCount = 6, matchBonus = false) | ||
assertThat(rank).isEqualTo(Rank.FIRST) | ||
} | ||
|
||
@Test | ||
fun `5κ°μ 보λμ€ λ³Όμ λ§μΆλ©΄ 2λ±μ΄λ€`() { | ||
val rank = Rank.from(matchCount = 5, matchBonus = true) | ||
assertThat(rank).isEqualTo(Rank.SECOND) | ||
} | ||
|
||
@Test | ||
fun `5κ° λ§μΆκ³ 보λμ€ λ³Όμ λͺ» λ§μΆλ©΄ 3λ±μ΄λ€`() { | ||
val rank = Rank.from(matchCount = 5, matchBonus = false) | ||
assertThat(rank).isEqualTo(Rank.THIRD) | ||
} | ||
|
||
@Test | ||
fun `4κ° λ§μΆλ©΄ 4λ±μ΄λ€`() { | ||
val rank = Rank.from(matchCount = 4, matchBonus = false) | ||
assertThat(rank).isEqualTo(Rank.FOURTH) | ||
} | ||
|
||
@Test | ||
fun `3κ° λ§μΆλ©΄ 5λ±μ΄λ€`() { | ||
val rank = Rank.from(matchCount = 3, matchBonus = false) | ||
assertThat(rank).isEqualTo(Rank.FIFTH) | ||
} | ||
|
||
@Test | ||
fun `2κ° μ΄ν λ§μΆλ©΄ λ―ΈλΉμ²¨μ΄λ€`() { | ||
val rank2 = Rank.from(matchCount = 2, matchBonus = false) | ||
val rank1 = Rank.from(matchCount = 1, matchBonus = false) | ||
val rank0 = Rank.from(matchCount = 0, matchBonus = false) | ||
|
||
assertThat(rank2).isEqualTo(Rank.NONE) | ||
assertThat(rank1).isEqualTo(Rank.NONE) | ||
assertThat(rank0).isEqualTo(Rank.NONE) | ||
} | ||
Comment on lines
+37
to
+46
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. νλμ ν μ€νΈ μΌμ΄μ€μ§λ§ λ€μν μΈμμ λν ν μ€νΈλ Parameterized Testλ₯Ό νμ©ν΄μ μ‘°κΈ λ κΉλνκ² λ³κ²½ν΄λ³Ό μ μμ κ² κ°μμ. π |
||
} |
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.
Viewκ° Modelμ μμ‘΄νκ³ μλ κ² κ°μμ. π€
View λ¨μμλ μ λ ₯ κ°μΌλ‘
List<List<Int>>
μ κ²°κ³Όλ₯Ό κ°μ Έμ€κ³ , μ΄ κ²°κ³Όλ Controllerμμ λ³νν΄μ£Όλλ‘ ν΄μ£Όλ©΄ μ’μ κ² κ°μ΅λλ€.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.
μΆκ°μ μΌλ‘ μ΄ μλ(μΌλ‘ μ λ ₯ λ°μ) λ‘λλ₯Ό μμ±ν΄μ£Όλ μν μ μ΄λ€ κ°μ²΄μκ² μμν μ§λ κ³ λ―Όν΄λ³΄μλ©΄ μ’μ κ² κ°μμ. π