generated from Learning-Is-Vital-In-Development/study-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ci: add ignore file for codecov * refactor: message 정리 * style: unnecessary trailing comma in test * feat: 구매할 로또의 개수를 결정하는 LottoCount 구현 * fix: input 검증을 위한 try catch loop 예외 레벨 조정 * style: trailing comma 정의 * style: multiline expression wrapping * style: new line function signature * impv: plus, minus 연산 추가 * Add LottoNumbers model and update View and Controller A feature to validate the number of lotto numbers was created. For this, a LottoNumbers model was added with a from method to transform a list of Integers into LottoNumbers and validate the size. The View is updated to request and display useful lotto-related information. The Controller is also updated to include interactions with the LottoNumbers model. Now, the application is able to manage the user's lotto numbers directly and enforce the correct number of lotto entries. * style: code formatting * ci: ignore LottoApp
- Loading branch information
Showing
16 changed files
with
241 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ignore: | ||
- "**/view/*" | ||
- "**/controller/*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,36 @@ | ||
package controller | ||
|
||
import model.lotto.LottoCount | ||
import model.lotto.LottoNumbers | ||
import model.money.Money | ||
import view.InputView | ||
import view.OutputView | ||
import view.validInputView | ||
|
||
class LottoApp(private val inputView: InputView, private val outputView: OutputView) { | ||
fun run() { | ||
validInputView({ inputCapital() }) { outputView.printMessage(it) } | ||
val money = validInputView({ inputCapital() }) { outputView.printMessage(it) } | ||
val lottoCount: LottoCount = validInputView({ inputManualCount(money) }) { outputView.printMessage(it) } | ||
validInputView({ inputManualLottoNumber(lottoCount) }) { outputView.printMessage(it) } | ||
} | ||
|
||
private fun inputCapital(): Money { | ||
outputView.requestCapital() | ||
return Money(inputView.requestAmount()) | ||
} | ||
|
||
private fun inputManualCount(money: Money): LottoCount { | ||
outputView.requestManualCount() | ||
val manualCount = inputView.requestAmount().toInt() | ||
return LottoCount.of(manualCount, money) | ||
} | ||
|
||
private fun inputManualLottoNumber(lottoCount: LottoCount): List<LottoNumbers> { | ||
return (1..lottoCount.manualCount) | ||
.map { | ||
outputView.requestManualLottoNumber() | ||
inputView.requestLottoNumber() | ||
} | ||
.map { LottoNumbers.from(it) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package model.lotto | ||
|
||
import model.money.Money | ||
import model.money.PRICE | ||
import view.NOT_ENOUGH_MONEY_MESSAGE | ||
|
||
class LottoCount( | ||
val manualCount: Int, | ||
val autoCount: Int, | ||
) { | ||
companion object { | ||
fun of(manualCount: Int, money: Money): LottoCount { | ||
val totalCount = money / PRICE | ||
val autoCount = totalCount - manualCount | ||
check(autoCount >= 0) { NOT_ENOUGH_MONEY_MESSAGE } | ||
return LottoCount(manualCount, autoCount) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
package model.lotto | ||
|
||
private const val INVALID_LOTTO_NUMBER_MESSAGE = "로또 번호는 1에서 45 사이여야 합니다." | ||
|
||
data class LottoNumber(private val value: Int) { | ||
init { | ||
require(value in 1..45) { INVALID_LOTTO_NUMBER_MESSAGE } | ||
} | ||
|
||
companion object { | ||
private const val INVALID_LOTTO_NUMBER_MESSAGE = "로또 번호는 1에서 45 사이여야 합니다." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package model.lotto | ||
|
||
import view.INVALID_LOTTO_NUMBER_COUNT_MESSAGE | ||
|
||
class LottoNumbers private constructor(private val numbers: List<LottoNumber>) { | ||
companion object { | ||
fun from(numbers: List<Int>): LottoNumbers { | ||
check(numbers.size == 6) { INVALID_LOTTO_NUMBER_COUNT_MESSAGE } | ||
val lottoNumbers = numbers.map { LottoNumber(it) } | ||
return LottoNumbers(lottoNumbers) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,17 @@ | ||
package model.money | ||
|
||
import view.EMPTY_VALUE_ERROR_MESSAGE | ||
import view.INVALID_NUMBER_FORMAT_MESSAGE | ||
|
||
class Amount(private val stringAmount: String) { | ||
private val integers = '0'..'9' | ||
|
||
init { | ||
require(stringAmount.isNotBlank()) { EMPTY_VALUE_ERROR } | ||
require(stringAmount.isInt()) { INVALID_INPUT_ERROR_PREFIX + stringAmount + INVALID_INPUT_ERROR_SUFFIX } | ||
require(stringAmount.isNotBlank()) { EMPTY_VALUE_ERROR_MESSAGE } | ||
require(stringAmount.isInt()) { INVALID_NUMBER_FORMAT_MESSAGE } | ||
} | ||
|
||
fun getBigDecimal() = stringAmount.toBigDecimal() | ||
|
||
private fun String.isInt() = this.all { it in integers } | ||
|
||
companion object { | ||
private const val EMPTY_VALUE_ERROR = "값이 비어있습니다." | ||
private const val INVALID_INPUT_ERROR_PREFIX = "입력한" | ||
private const val INVALID_INPUT_ERROR_SUFFIX = "값은 숫자여야 합니다." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package model.lotto | ||
|
||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.core.spec.style.FreeSpec | ||
import io.kotest.matchers.shouldBe | ||
import model.money.Money | ||
import model.money.PRICE | ||
import view.NOT_ENOUGH_MONEY_MESSAGE | ||
import java.math.BigDecimal | ||
|
||
class LottoCountTest : FreeSpec( | ||
{ | ||
"수동 로또 구매 개수를 입력할 때" - { | ||
"돈이 충분하지 않으면 에러가 발생한다" { | ||
val money = Money.ZERO | ||
val manualCount = 2 | ||
val exception = | ||
shouldThrow<IllegalStateException> { | ||
LottoCount.of(manualCount, money) | ||
} | ||
exception.message shouldBe NOT_ENOUGH_MONEY_MESSAGE | ||
} | ||
|
||
"돈이 충분하면 수동 로또 개수와 자동 로또 개수를 구매 가능 개수만큼만 반환한다" - { | ||
"나누어 떨어지는 경우 모든 액수를 구매에 사용한다" { | ||
val money = PRICE * 3 | ||
val manualCount = 2 | ||
val lottoCount = LottoCount.of(manualCount, money) | ||
lottoCount.manualCount shouldBe 2 | ||
lottoCount.autoCount shouldBe 1 | ||
} | ||
|
||
"나누어 떨어지지 않는 경우 구매 가능한 최대 수량만 구매한다" { | ||
val money = Money(BigDecimal.valueOf(12000)) | ||
val manualCount = 1 | ||
val lottoCount = LottoCount.of(manualCount, money) | ||
lottoCount.manualCount shouldBe 1 | ||
lottoCount.autoCount shouldBe 1 | ||
} | ||
} | ||
|
||
"수동 로또 개수가 0이면 자동 로또 개수만 반환한다" { | ||
val money = PRICE * 3 | ||
val manualCount = 0 | ||
val lottoCount = LottoCount.of(manualCount, money) | ||
lottoCount.manualCount shouldBe 0 | ||
lottoCount.autoCount shouldBe 3 | ||
} | ||
} | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package model.lotto | ||
|
||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.core.spec.style.DescribeSpec | ||
import io.kotest.matchers.shouldBe | ||
import view.INVALID_LOTTO_NUMBER_COUNT_MESSAGE | ||
|
||
class LottoNumbersTest : DescribeSpec( | ||
{ | ||
describe("로또 번호는 6개의 유일한 숫자로 구성되어야 한다") { | ||
it("6개가 아니면 예외가 발생한다") { | ||
val numbers = listOf(1, 2, 3, 4, 5) | ||
val exception = shouldThrow<IllegalStateException> { | ||
LottoNumbers.from(numbers) | ||
} | ||
exception.message shouldBe INVALID_LOTTO_NUMBER_COUNT_MESSAGE | ||
} | ||
|
||
it("6개면 예외가 발생하지 않는다") { | ||
val numbers = listOf(1, 2, 3, 4, 5, 6) | ||
LottoNumbers.from(numbers) | ||
} | ||
|
||
xit("중복된 숫자가 있으면 예외가 발생한다") { | ||
TODO("Not yet implemented") | ||
} | ||
} | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.