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.
* feat: LottoTicket model and enhance Print function * feat: auto ticket generation functionality to LottoApp * style: Adjust formatting in LottoTicket for better readability and ktlint compliance * style: Adjust formatting in LottoTicket for better readability and ktlint compliance * feat: Add toString method in LottoTicket * test: Add LottoTicket unit tests * feat: Added checking logic for duplicate numbers in a lotto ticket. * style: ktlint * style: ktlint * feat: Improve readability by sorting auto-generated Lotto numbers * refactor: Decouple LottoTicket generation logic to LottoTicketGenerator object * refactor: Removed count methods from LottoNumbers for separation of concerns * refactor: Decouple manual ticket generation for better separation of concerns * test: LottoTicketGenerator's generate method * style: Removed unnecessary line in LottoTicketGeneratorTest * style: lint codes
- Loading branch information
Showing
8 changed files
with
154 additions
and
12 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 |
---|---|---|
@@ -1,13 +1,19 @@ | ||
package model.lotto | ||
|
||
internal const val INVALID_LOTTO_NUMBER_COUNT_MESSAGE = "로또 번호는 6개여야 합니다." | ||
internal const val INVALID_LOTTO_NUMBER_DUPLICATE_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 } | ||
check(numbers.distinct().size == 6) { INVALID_LOTTO_NUMBER_DUPLICATE_MESSAGE } | ||
val lottoNumbers = numbers.map { LottoNumber(it) } | ||
return LottoNumbers(lottoNumbers) | ||
} | ||
} | ||
|
||
override fun toString(): String { | ||
return numbers.joinToString(separator = ", ", prefix = "[", postfix = "]") { it.toString() } | ||
} | ||
} |
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,41 @@ | ||
package model.lotto | ||
|
||
enum class TicketType { | ||
Manual, | ||
Auto, | ||
} | ||
|
||
data class LottoTicket( | ||
val numbers: LottoNumbers, | ||
val type: TicketType, | ||
) { | ||
companion object { | ||
fun of( | ||
numbers: LottoNumbers, | ||
type: TicketType, | ||
): LottoTicket { | ||
return LottoTicket(numbers, type) | ||
} | ||
} | ||
|
||
override fun toString(): String { | ||
return numbers.toString() | ||
} | ||
} | ||
|
||
object LottoTicketGenerator { | ||
fun generate(count: Int): List<LottoTicket> { | ||
val allPossibleNumbers = (1..45).toList() | ||
return List(count) { | ||
val selectedNumbers = allPossibleNumbers.shuffled().take(6).sorted() | ||
val lottoNumbers = LottoNumbers.from(selectedNumbers) | ||
LottoTicket.of(lottoNumbers, TicketType.Auto) | ||
} | ||
} | ||
|
||
fun generate(manualNumbers: List<LottoNumbers>): List<LottoTicket> { | ||
return manualNumbers.map { lottoNumbers -> | ||
LottoTicket.of(lottoNumbers, TicketType.Manual) | ||
} | ||
} | ||
} |
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,29 @@ | ||
package model.lotto | ||
|
||
import io.kotest.core.spec.style.FreeSpec | ||
import io.kotest.matchers.shouldBe | ||
|
||
class LottoTicketGeneratorTest : FreeSpec( | ||
{ | ||
"LottoTicketGenerator 생성" - { | ||
"일정 수의 티켓 생성" { | ||
val count = 5 | ||
val tickets = LottoTicketGenerator.generate(count) | ||
|
||
tickets.size shouldBe count | ||
} | ||
|
||
"수동 티켓 생성" { | ||
val manualNumbers = | ||
listOf( | ||
LottoNumbers.from(listOf(1, 2, 3, 4, 5, 6)), | ||
LottoNumbers.from(listOf(7, 8, 9, 10, 11, 12)), | ||
) | ||
val tickets = LottoTicketGenerator.generate(manualNumbers) | ||
|
||
tickets.size shouldBe manualNumbers.size | ||
tickets.forEach { it.type shouldBe TicketType.Manual } | ||
} | ||
} | ||
}, | ||
) |
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,28 @@ | ||
package model.lotto | ||
|
||
import io.kotest.core.spec.style.FreeSpec | ||
import io.kotest.matchers.shouldBe | ||
|
||
class LottoTicketTest : FreeSpec( | ||
{ | ||
"LottoTicket 생성" - { | ||
"주어진 LottoNumbers 와 TicketType 을 가진 LottoTicket 을 반환" { | ||
val numbers = LottoNumbers.from(listOf(1, 2, 3, 4, 5, 6)) | ||
val ticketType = TicketType.Manual | ||
val ticket = LottoTicket.of(numbers, ticketType) | ||
|
||
ticket.numbers shouldBe numbers | ||
ticket.type shouldBe ticketType | ||
} | ||
} | ||
|
||
"LottoTicket 출력" - { | ||
"LottoTicket 의 numbers 필드를 문자열화 하여 반환" { | ||
val numbers = LottoNumbers.from(listOf(1, 2, 3, 4, 5, 6)) | ||
val ticket = LottoTicket.of(numbers, TicketType.Manual) | ||
|
||
ticket.toString() shouldBe numbers.toString() | ||
} | ||
} | ||
}, | ||
) |