generated from Learning-Is-Vital-In-Development/study-template
-
Notifications
You must be signed in to change notification settings - Fork 2
refactor : 로또 도메인 리팩토링 진행 #22
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
Merged
songkg7
merged 3 commits into
Learning-Is-Vital-In-Development:main
from
this-is-spear:refactoring
Nov 17, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,41 +1,10 @@ | ||
package model.lotto | ||
|
||
enum class TicketType { | ||
Manual, | ||
Auto, | ||
} | ||
|
||
data class LottoGame( | ||
val numbers: LottoNumbers, | ||
val type: TicketType, | ||
) { | ||
companion object { | ||
fun of( | ||
numbers: LottoNumbers, | ||
type: TicketType, | ||
): LottoGame { | ||
return LottoGame(numbers, type) | ||
} | ||
} | ||
sealed class LottoGame(val numbers: LottoNumbers) { | ||
class Manual(manualNumbers: LottoNumbers) : LottoGame(manualNumbers) | ||
class Auto(autoNumbers: LottoNumbers) : LottoGame(autoNumbers) | ||
|
||
override fun toString(): String { | ||
return numbers.toString() | ||
} | ||
} | ||
|
||
object LottoGameGenerator { | ||
fun generate(count: Int): List<LottoGame> { | ||
val allPossibleNumbers = (1..45).toList() | ||
return List(count) { | ||
val selectedNumbers = allPossibleNumbers.shuffled().take(6).sorted() | ||
val lottoNumbers = LottoNumbers.from(selectedNumbers) | ||
LottoGame.of(lottoNumbers, TicketType.Auto) | ||
} | ||
} | ||
|
||
fun generate(manualNumbers: List<LottoNumbers>): List<LottoGame> { | ||
return manualNumbers.map { lottoNumbers -> | ||
LottoGame.of(lottoNumbers, TicketType.Manual) | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -5,12 +5,10 @@ class LottoTickets( | |
val elements: List<LottoTicket>, | ||
) { | ||
companion object { | ||
fun of(elements: List<LottoTicket>): LottoTickets { | ||
return LottoTickets(elements) | ||
} | ||
|
||
fun createWithGames(elements: List<LottoGame>): LottoTickets { | ||
val tickets = elements.chunked(5).map { LottoTicket.of(it) } | ||
fun of(manualNumbers: List<LottoNumbers>, autoNumbers: List<LottoNumbers>): LottoTickets { | ||
val manualGames = manualNumbers.map { LottoGame.Manual(it) }.toList() | ||
val autoGames = autoNumbers.map { LottoGame.Auto(it) }.toList() | ||
val tickets = (manualGames + autoGames).chunked(5).map { LottoTicket(it) } | ||
Comment on lines
+9
to
+11
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. 아름답습니다... 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. 이게 언어지... |
||
return LottoTickets(tickets) | ||
} | ||
} | ||
|
This file contains hidden or 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 hidden or 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,18 @@ | ||
package model | ||
|
||
import model.lotto.LottoGame | ||
import model.lotto.LottoNumbers | ||
|
||
fun generateFixtureLottoGames(count: Int): List<LottoGame> { | ||
if (count == 0) { | ||
return emptyList() | ||
} | ||
|
||
return List(count) { | ||
val selectedNumbers = (1..45).toList() | ||
.shuffled() | ||
.take(6) | ||
.sorted() | ||
LottoGame.Auto(LottoNumbers.from(selectedNumbers)) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,38 @@ | ||
package model.lotto | ||
|
||
import io.kotest.assertions.throwables.shouldThrowExactly | ||
import io.kotest.core.spec.style.FreeSpec | ||
import io.kotest.matchers.shouldBe | ||
import model.generateFixtureLottoGames | ||
|
||
class LottoTicketTest : FreeSpec( | ||
{ | ||
"LottoTicket을 생성할 때" - { | ||
"1개의 로또 게임이 포함 될 수 있다." - { | ||
val count = 1 | ||
val ticket = LottoTicket(generateFixtureLottoGames(count)) | ||
ticket.games.size shouldBe count | ||
} | ||
|
||
"5개의 로또 게임이 포함 될 수 있다." - { | ||
val count = 5 | ||
val ticket = LottoTicket(generateFixtureLottoGames(count)) | ||
ticket.games.size shouldBe count | ||
} | ||
|
||
"로또 게임이 없으면 안된다." - { | ||
val games = generateFixtureLottoGames(0) | ||
println(games) | ||
shouldThrowExactly<IllegalArgumentException> { | ||
LottoTicket(emptyList()) | ||
}.message shouldBe REQUIRED_LEAST_ONE_GAME | ||
} | ||
|
||
"로또 게임이 6개 이상 포함되면 안된다." - { | ||
shouldThrowExactly<IllegalArgumentException> { | ||
LottoTicket(generateFixtureLottoGames(6)) | ||
}.message shouldBe REQUIRED_MAXIMUM_FIVE_GAME | ||
} | ||
} | ||
}, | ||
) |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
패턴 매칭 😀
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.
이런 방법을 패턴 매칭이라 하는군요!! 😲
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.
저도 새로운거 알아갑니다~