Skip to content

Commit

Permalink
feat: create lotto
Browse files Browse the repository at this point in the history
  • Loading branch information
sendkite committed Nov 26, 2023
1 parent c40fc18 commit bb5a273
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 1 deletion.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@
+ [x] 커스텀 구분자는 문자열 앞부분의 `//``\n` 사이에 위치
+ [x] 커스텀 구분자는 한 개 지정 가능
+ [x] 커스텀 구분자와 기본 구분자를 함께 사용할 수 있음
+ [x] 구분자를 기준으로 분리한 각 숫자의 합을 반환
+ [x] 구분자를 기준으로 분리한 각 숫자의 합을 반환

### 로또 (자동)

[] 로또 자동 생성기 구현
[x] 로또 한장의 가격은 1000원
[x] 로또 1000원 단위로만 구매 가능
[x] 로또는 1부터 45까지의 숫자 중 6개를 랜덤으로 뽑는다.
[x] 로또는 6개의 숫자가 모두 달라야 한다.
[x] 로또 한장은 6개의 숫자를 가진다.
[x] 로또 한장은 숫자를 오름차순으로 정렬한다.
9 changes: 9 additions & 0 deletions src/main/kotlin/lotto/Lotto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package lotto

class Lotto {

val numbers: List<Int> = generateLottoNumbers()
companion object {
const val PRICE: Int = 1000
}
}
10 changes: 10 additions & 0 deletions src/main/kotlin/lotto/LottoBuyer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package lotto

class LottoBuyer(
private val money: Int
) {

fun buyLottoFrom(lottoStore: LottoStore): List<Lotto> {
return lottoStore.sell(money)
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/lotto/LottoNumber.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package lotto

fun generateLottoNumbers(): List<Int> {
return (1..45).shuffled().take(6).sorted()
}
13 changes: 13 additions & 0 deletions src/main/kotlin/lotto/LottoStore.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package lotto

class LottoStore {

fun sell(money: Int): List<Lotto> {
val count = money / Lotto.PRICE
val lottos = mutableListOf<Lotto>()
for (i in 1..count) {
lottos.add(Lotto())
}
return lottos
}
}
20 changes: 20 additions & 0 deletions src/test/kotlin/lotto/LottoStoreTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package lotto

import io.kotest.matchers.collections.haveSize
import io.kotest.matchers.should
import org.junit.jupiter.api.Test

class LottoStoreTest {

@Test
fun `1000원으로 로또를 구매한다`() {
val lotto1 = LottoStore().sell(1000)
lotto1 should haveSize(1)

val lotto2 = LottoStore().sell(500)
lotto2 should haveSize(0)

val lotto3 = LottoStore().sell(18000)
lotto3 should haveSize(18)
}
}
43 changes: 43 additions & 0 deletions src/test/kotlin/lotto/LottoTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package lotto

import io.kotest.matchers.collections.shouldBeOneOf
import io.kotest.matchers.ints.shouldBeInRange
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test

class LottoTest {

@Test
fun `로또는 6자리 숫자를 가진다`() {
val lotto = Lotto()
lotto.numbers.size shouldBe 6
}

@Test
fun `로또는 1부터 45까지의 숫자를 가진다`() {
val lotto = Lotto()
lotto.numbers.forEach {
it.shouldBeInRange(1..45)
}
}

@Test
fun `로또는 중복되지 않는 숫자를 가진다`() {
val lotto = Lotto()
lotto.numbers.toSet().size shouldBe 6
}

@Test
fun `로또는 숫자는 오름차순 정렬`() {
val lotto = Lotto()
lotto.numbers shouldBe lotto.numbers.sorted()
}

@Test
fun `로또 숫자는 중복되지 않는다`() {
val lotto = Lotto()
lotto.numbers.forEach {
it shouldBeOneOf lotto.numbers
}
}
}

0 comments on commit bb5a273

Please sign in to comment.