-
Notifications
You must be signed in to change notification settings - Fork 76
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
[로또 2단계] 모찌 미션 제출합니다. #150
base: wondroid-world
Are you sure you want to change the base?
Changes from all commits
def0f8a
26230de
83f588f
b34d365
146fcb7
18d8dce
7f6c24b
68fbc17
056b99a
29dec2f
ceaeb6a
87d9363
493bab9
fe1edd5
334aebc
0d3eb9f
81e54d7
3699b71
f8094a5
38eef1b
aeaeb8d
63062df
74b1eb1
468054d
0f60f16
ea357f9
eba7611
d2499d4
8941fd9
9eec7d8
0174611
6484553
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package lotto.model | ||
|
||
import lotto.contants.LottoRuleConstants | ||
|
||
class AutoLottoTicketGenerator : LottoTicketGenerator { | ||
override val type: LottoIssueType = LottoIssueType.AUTO | ||
|
||
Comment on lines
+5
to
+7
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. Generator는 생성자인데, model 에 있는것이 좋을까 싶긴 합니다! |
||
override fun generateLottoTicket(): LottoTicket { | ||
val lottoNumbers = | ||
(LottoRuleConstants.MINIMUM_NUMBER.value..LottoRuleConstants.MAXIMUM_NUMBER.value) | ||
.shuffled() | ||
.take(LottoRuleConstants.LOTTO_PICK_COUNT.value) | ||
.sorted() | ||
.map(::LottoNumber) | ||
return LottoTicket(LottoIssueType.AUTO, lottoNumbers) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package lotto.model | ||
|
||
enum class LottoIssueType( | ||
val issueType: String, | ||
) { | ||
AUTO("자동"), | ||
MANUAL("수동"), | ||
WINNING("당첨"), | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,24 @@ | ||
package lotto.model | ||
|
||
// 로또 발행하는 머신 | ||
class LottoMachine { | ||
fun purchase(count: Int): List<LottoTicket> = List(count) { LottoTicket() } | ||
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. LottoMachine을 통해서 로또를 생성하는군요! |
||
// 로또 티켓을 발행한다. | ||
Comment on lines
4
to
+5
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. class외에 object라는 키워드가 있는데, 이둘은 어떤 차이점이 있을까요?🤔 |
||
fun issueLottoTickets( | ||
customerWantToBuyAutoLottoTicketCount: LottoTicketCount, | ||
manualLottoNumbers: List<List<Int>>, | ||
): List<LottoTicket> = purchaseManualLottoTickets(manualLottoNumbers) + purchaseAutoLottoTickets(customerWantToBuyAutoLottoTicketCount) | ||
|
||
// 자동 로또를 발행 | ||
private fun purchaseAutoLottoTickets(customerWantToBuyAutoLottoTicketCount: LottoTicketCount): List<LottoTicket> { | ||
val autoLottoGenerator = AutoLottoTicketGenerator() | ||
val autoLottoTickets = | ||
List(customerWantToBuyAutoLottoTicketCount.toInt()) { autoLottoGenerator.generateLottoTicket() } | ||
return autoLottoTickets | ||
} | ||
|
||
// 수동 로또를 발행 | ||
private fun purchaseManualLottoTickets(manualLottoNumbers: List<List<Int>>): List<LottoTicket> { | ||
val manualLottoTickets = manualLottoNumbers.map { ManualLottoTicketGenerator(it).generateLottoTicket() } | ||
return manualLottoTickets | ||
} | ||
Comment on lines
+11
to
+23
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. 타입별 로또 티켓에 대한 생성자 좋습니다~👍 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package lotto.model | ||
|
||
import lotto.contants.LottoRuleConstants | ||
|
||
// 돈 계산을 해주는 클래스 | ||
class LottoStoreCashier( | ||
Comment on lines
+5
to
+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. 주석을 이렇게 추가하신 이유가 있을까요?? 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. 그래서 주석이 있는 객체가 존재하고 없는 객체가 존재했군요~ |
||
private val customerMoney: Int, | ||
) { | ||
private val possibleToLottoTicketCount = calculatePossibleToBuyLottoTicketCount() | ||
|
||
// 로또의 가격은 천원이다. 로또를 구매할려면 천원이상 넣어야한다. | ||
init { | ||
require(customerMoney >= LottoRuleConstants.LOTTO_AMOUNT.value) { INSUFFICIENT_MONEY_FOR_LOTTO_PURCHASE } | ||
} | ||
|
||
// 고객이 준 돈에서 몇장을 살 수 있는 지 계산한다. | ||
fun calculatePossibleToBuyLottoTicketCount(): Int = customerMoney / LottoRuleConstants.LOTTO_AMOUNT.value | ||
|
||
// 고객이 몇장을 사고 싶다고 이야기를 하면 가능한지 계산한다. | ||
fun isPossibleToBuy(customerWantBuyLottoTicketCount: Int): Boolean = possibleToLottoTicketCount >= customerWantBuyLottoTicketCount | ||
|
||
// 고객에게 전달할 잔돈을 계산한다. | ||
fun calculateChange(customerWantBuyLottoTicketCount: Int): Int = | ||
customerMoney - (LottoRuleConstants.LOTTO_AMOUNT.value * possibleToLottoTicketCount) | ||
|
||
companion object { | ||
private const val INSUFFICIENT_MONEY_FOR_LOTTO_PURCHASE = "로또는 천원이상 넣어야지 구매 가능합니다." | ||
} | ||
Comment on lines
+11
to
+28
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. 이전 코멘트 다만, 한가지 고민을 해보면 좋을 것 같아서 코멘트를 남겨두었습니다~ |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,28 +3,35 @@ package lotto.model | |
import lotto.contants.LottoRuleConstants | ||
|
||
class LottoTicket( | ||
private val numbers: Set<LottoNumber> = generateLotto(), | ||
val lottoIssueType: LottoIssueType, | ||
private val numbers: List<LottoNumber>, | ||
) { | ||
private val lottoNumbers: Set<LottoNumber> = numbers.toSet() | ||
|
||
init { | ||
require(numbers.size == LottoRuleConstants.LOTTO_PICK_COUNT.value) | ||
{ ERROR_NUMBERS_COUNT } | ||
require(numbers.size == LottoRuleConstants.LOTTO_PICK_COUNT.value) { ERROR_NUMBERS_COUNT } | ||
} | ||
|
||
fun getSize() = lottoNumbers.size | ||
|
||
fun containsNumber(number: LottoNumber): Boolean { | ||
return lottoNumbers.contains(number) | ||
} | ||
|
||
fun getSize() = numbers.size | ||
fun getNumbers(): Set<LottoNumber> { | ||
return lottoNumbers | ||
} | ||
|
||
fun getNumbers() = numbers | ||
fun matchNumbersSize(lottoTicket: LottoTicket) = numbers.intersect(lottoTicket.getNumbers()).size | ||
Comment on lines
-15
to
+25
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. 로또가 몇개가 맞는지에 대한 로직이 LottoTicket 내로 이동을 한 이유가 궁금합니다~🤔 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. 로또 번호가 몇 개 맞는지에 대한 로직이 LottoTicket으로 이동한 이유는 책임의 분리(SRP) 원칙을 따르기 위해서입니다. WinningLotto 클래스는 "당첨된 로또 티켓과 다른 로또 티켓들을 비교"하는 책임을 가집니다. 이 클래스는 당첨 결과를 확인하는 역할에 집중해야 하며, 실제로 번호를 맞추는 로직은 LottoTicket 객체가 처리해야 할 일이라고 생각했습니다. LottoTicket 클래스는 로또 번호를 담고 있는 객체로, 자신에게 맞는 번호가 몇 개인지 확인하는 역할을 맡는 것이 자연스럽다고 생각했습니다. WinningLotto의 관심사는 "당첨된 로또 티켓과 비교하여 몇 등인지 확인하는 것"이지, 어떻게 번호가 맞는지를 구현하는 것은 LottoTicket의 책임이라고 생각햅니다. 각 객체는 자신의 책임에만 집중해야 하기 위해서, LottoTicket 내로 함수를 이동시켰습니다. 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. WinningLotto는 로또의 당첨 번호만을 관리하고 LottoTicket 에서 내 번호가 당첨됐는지를 판별하도록 하셨군요~ |
||
|
||
companion object { | ||
private const val ERROR_NUMBERS_COUNT = "로또 번호의 개수는 6개입니다." | ||
|
||
private fun generateLotto(): Set<LottoNumber> = | ||
(LottoRuleConstants.MINIMUM_NUMBER.value..LottoRuleConstants.MAXIMUM_NUMBER.value) | ||
.shuffled() | ||
.take(LottoRuleConstants.LOTTO_PICK_COUNT.value) | ||
.sorted() | ||
.map { LottoNumber(it) } | ||
.toSet() | ||
|
||
fun create(vararg lottoNumbers: LottoNumber): LottoTicket = LottoTicket(lottoNumbers.toSet()) | ||
fun create( | ||
lottoIssueType: LottoIssueType, | ||
vararg lottoNumbers: Int, | ||
): LottoTicket { | ||
return LottoTicket(lottoIssueType, lottoNumbers.map { LottoNumber(it) }) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package lotto.model | ||
|
||
import lotto.contants.LottoRuleConstants | ||
|
||
class LottoTicketCount( | ||
private val count: Int, | ||
) { | ||
init { | ||
require(count >= LottoRuleConstants.ZERO.value) { LOTTO_TICKET_COUNT_NOT_NEGATIVE } | ||
} | ||
|
||
fun toInt() = count | ||
|
||
Comment on lines
+12
to
+13
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. toInt()로 표현을 하면, 어떤 것을 나타내는지에 대해서 불명확해질 수도 있겠다는 생각도 들긴 하네요! |
||
companion object { | ||
private const val LOTTO_TICKET_COUNT_NOT_NEGATIVE = "로또 구매 장 수는 음수가 될수 없습니다." | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package lotto.model | ||
|
||
interface LottoTicketGenerator { | ||
val type: LottoIssueType | ||
|
||
fun generateLottoTicket(): LottoTicket | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package lotto.model | ||
|
||
// 수동이랑 자동 로또 계산해주는 매니저, 로또 티켓까지 주는 역할 담당 | ||
class LottoTicketIssueManager( | ||
totalCount: Int, | ||
manualCount: Int, | ||
manualLottoNumbers: List<List<Int>>, | ||
) { | ||
private val manualLottoTicketCount = LottoTicketCount(manualCount) | ||
private val autoLottoTicketCount = | ||
LottoTicketCount(totalCount - manualCount) | ||
|
||
// 발행가능한 개수보다 수동으로 로또 발행받고 싶은 개수가 크면 안된다. | ||
init { | ||
require(totalCount >= manualCount) { EXCEEDED_MANUAL_LOTTO_COUNT } | ||
require(manualCount == manualLottoNumbers.size) { LOTTO_COUNT_NOT_MATCH_LOTTO_NUMBERS } | ||
} | ||
|
||
// 로또를 발행해서 준다. | ||
fun getLottoTickets(manualLottoNumbers: List<List<Int>>): List<LottoTicket> = | ||
LottoMachine().issueLottoTickets(autoLottoTicketCount, manualLottoNumbers) | ||
|
||
fun getCustomerWantToBuyManualLottoTicketCount(): Int = autoLottoTicketCount.toInt() | ||
|
||
Comment on lines
+23
to
+24
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. 해당 함수의 네이밍을 아래와 같이 수정을 할수도 있을 것 같은데, 모찌의 생각에는 어떤 네이밍이 좋을 것 같나요🤔 |
||
companion object { | ||
private const val LOTTO_COUNT_NOT_MATCH_LOTTO_NUMBERS = "수동 구매 개수와 수동 번호 입력 개수가 일치하지 않습니다." | ||
private const val EXCEEDED_MANUAL_LOTTO_COUNT = "전체 발행 가능한 로또 개수보다 수동로또 발행 개수가 더 많습니다." | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package lotto.model | ||
|
||
class ManualLottoTicketGenerator( | ||
private val numbers: List<Int>, | ||
) : LottoTicketGenerator { | ||
override val type: LottoIssueType = LottoIssueType.MANUAL | ||
|
||
override fun generateLottoTicket(): LottoTicket = LottoTicket(LottoIssueType.MANUAL, numbers.map(::LottoNumber)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,39 @@ | ||
package lotto.model | ||
|
||
import lotto.contants.LottoRuleConstants.LOTTO_PICK_COUNT | ||
|
||
// 당첨 번호 확인하는 로직 | ||
class WinningLotto( | ||
private val winningNumbers: Set<LottoNumber>, | ||
private val winningLottoTicket: LottoTicket, | ||
private val bonusNumber: LottoNumber, | ||
) { | ||
init { | ||
require(winningNumbers.size == LOTTO_PICK_COUNT.value) { ERROR_NUMBERS_COUNT } | ||
require(!winningNumbers.contains(bonusNumber)) { ERROR_LOTTO_NUMBERS_NOT_CONTAIN_BONUS_NUMBER } | ||
require(!winningLottoTicket.containsNumber(bonusNumber)) { ERROR_LOTTO_NUMBERS_NOT_CONTAIN_BONUS_NUMBER } | ||
} | ||
|
||
fun getResult(lottoTickets: List<LottoTicket>): LottoResult { | ||
val result = lottoTickets.map { getRank(it) } | ||
return LottoResult(result) | ||
} | ||
// 전체 당첨 확인 결과 반환 | ||
fun getRanks(lottoTickets: List<LottoTicket>): List<Rank> = lottoTickets.map { getRank(it) } | ||
|
||
fun getRank(lottoTicket: LottoTicket): Rank { | ||
// 몇 등에 당첨 되었는 지 확인 | ||
private fun getRank(lottoTicket: LottoTicket): Rank { | ||
val countOfMatch: Int = getCountOfMatch(lottoTicket) | ||
val matchBonus: Boolean = getMatchBonus(lottoTicket) | ||
return Rank.valueOf(countOfMatch, matchBonus) | ||
} | ||
|
||
private fun getCountOfMatch(lottoTicket: LottoTicket): Int = winningNumbers.intersect(lottoTicket.getNumbers()).size | ||
// 몇 개 맞는 지 확인 | ||
private fun getCountOfMatch(lottoTicket: LottoTicket): Int = winningLottoTicket.matchNumbersSize(lottoTicket) | ||
|
||
private fun getMatchBonus(lottoTicket: LottoTicket): Boolean = lottoTicket.getNumbers().contains(bonusNumber) | ||
// 보너스 번호 확인 | ||
private fun getMatchBonus(lottoTicket: LottoTicket): Boolean = lottoTicket.containsNumber(bonusNumber) | ||
|
||
companion object { | ||
private const val ERROR_NUMBERS_COUNT = "로또 번호의 개수는 6개입니다." | ||
private const val ERROR_LOTTO_NUMBERS_NOT_CONTAIN_BONUS_NUMBER = "로또 당첨 번호는 보너스 번호를 포함하지 말아야합니다." | ||
|
||
fun create(vararg lottoNumbers: LottoNumber, bonusNumber: LottoNumber): WinningLotto { | ||
return WinningLotto(lottoNumbers.toSet(), bonusNumber) | ||
fun create( | ||
vararg lottoNumbers: Int, | ||
bonusNumber: Int, | ||
): WinningLotto { | ||
val winningNumbers = LottoTicket(LottoIssueType.WINNING, lottoNumbers.map { LottoNumber(it) }) | ||
return WinningLotto(winningNumbers, LottoNumber(bonusNumber)) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package lotto.view | ||
|
||
object OutputView { | ||
fun printMessage(msg: Any) = println(msg) | ||
fun printlnMessage(msg: Any) = println(msg) | ||
|
||
fun printMessage(msg: Any) = print(msg) | ||
} | ||
Comment on lines
3
to
7
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. OutputView와 InputView에서 너무 담당을 하는 것이 없다는 생각이 드네요..😅 |
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.
45 이상의 숫자를 입력하면 앱이 죽도록 되어 있군요~
이부분은 요구사항에 없긴 한데, 한번 고민해보면 좋을 것 같은 내용인 것 같아서 코멘트 남겨두었습니다!
재 입력을 받도록 해볼 수도 있겠다는 생각도 드는군요!💪
(수정이 이루어져야 하는 것은 아니고 어떻게 하면 해당 기능을 구현할 수 있을지 모찌의 의견을 남겨주셔도 좋을 것 같습니다)