-
Notifications
You must be signed in to change notification settings - Fork 362
Step4 - Lotto (Manual) #1158
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
base: mingyum-kim
Are you sure you want to change the base?
Step4 - Lotto (Manual) #1158
Changes from all commits
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 |
---|---|---|
@@ -1,12 +1,24 @@ | ||
package lotto.domain | ||
|
||
import lotto.controller.WinningNumbers | ||
|
||
class Ticket(val lottoNumber: Set<LottoNumber>) { | ||
constructor() : this(generateLottoNumber()) | ||
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. It's a nice approach to distinguish between generating a ticket randomly and manually. Here’s a reference you might find helpful: |
||
|
||
init { | ||
require(lottoNumber.size == 6) { | ||
throw IllegalArgumentException("Please enter $AMOUNT_OF_NUMBER_FOR_TICKET numbers") | ||
} | ||
} | ||
|
||
constructor(input: List<Int>) : this(input.map { LottoNumber(it) }.toSet()) | ||
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. Before merging, it might be good to double-check if the class layout follows the recommended order across the code. |
||
|
||
fun contains(bonusNumber: LottoNumber): Boolean { | ||
return lottoNumber.contains(bonusNumber) | ||
fun checkMatches(winningNumbers: WinningNumbers): Int { | ||
return winningNumbers.numbers.intersect(lottoNumber).size | ||
} | ||
|
||
fun checkBonusMatches(winningNumbers: WinningNumbers): Boolean { | ||
return lottoNumber.contains(winningNumbers.bonusNumber) | ||
} | ||
|
||
companion object { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,29 +6,28 @@ class Tickets(val tickets: List<Ticket>) { | |
val size: Int | ||
get() = tickets.size | ||
|
||
constructor(amountOfTicket: Int) : this(generateTickets(amountOfTicket)) | ||
constructor(numberOfRandomTickets: Int, manualTickets: List<Ticket>) | ||
: this(generateTicketsWithManualTickets(numberOfRandomTickets, manualTickets)) | ||
|
||
fun checkMatches(winningNumbers: WinningNumbers): WinningStatistics { | ||
val statistics = WinningStatistics() | ||
for (ticket in tickets) { | ||
val matches = checkMatches(winningNumbers, ticket) | ||
val bonusMatches = checkBonusMatches(winningNumbers, ticket) | ||
val matches = ticket.checkMatches(winningNumbers) | ||
val bonusMatches = ticket.checkBonusMatches(winningNumbers) | ||
statistics.add(matches, bonusMatches) | ||
Comment on lines
+15
to
17
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. Continue to #1156 (comment): Oh, my mistake 🥲 I explained it inappropriately — sorry for the confusion. Since both are needed to determine the prize, wouldn’t it make more sense to treat them as a single match result? |
||
} | ||
return statistics | ||
} | ||
|
||
private fun checkMatches(winningNumbers: WinningNumbers, ticket: Ticket): Int{ | ||
return winningNumbers.numbers.intersect(ticket.lottoNumber).size | ||
} | ||
|
||
private fun checkBonusMatches(winningNumbers: WinningNumbers, ticket: Ticket): Boolean { | ||
return ticket.contains(winningNumbers.bonusNumber) | ||
} | ||
|
||
companion object { | ||
private fun generateTickets(amountOfTicket: Int): List<Ticket> { | ||
return List(amountOfTicket) { Ticket() } | ||
} | ||
|
||
private fun generateTicketsWithManualTickets(numberOfTickets: Int, manualTickets: List<Ticket>): List<Ticket> { | ||
val generatedTickets = manualTickets.toMutableList() | ||
generatedTickets.addAll(generateTickets(numberOfTickets)) | ||
return generatedTickets | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,42 @@ | ||
package lotto.view | ||
|
||
import lotto.domain.Ticket | ||
|
||
class InputView { | ||
fun enterPurchaseAmount(): Int { | ||
println("Please enter the purchase amount.") | ||
return readln().toIntOrNull() ?: throw IllegalArgumentException("Please enter a valid purchase.") | ||
} | ||
|
||
fun enterWinningNumbers() : List<Int> { | ||
fun enterWinningNumbers(): List<Int> { | ||
println("\nPlease enter last week’s winning numbers.") | ||
val input = readlnOrNull() ?: throw IllegalArgumentException("Please enter a valid numbers.") | ||
return input.split(",").map { it.trim().toIntOrNull() | ||
?: throw IllegalArgumentException("Please enter a valid numbers.") } | ||
return enterLottoNumbers() | ||
} | ||
|
||
fun enterBonusNumber(): Int { | ||
println("Please enter the bonus number.") | ||
val input = readlnOrNull() ?: throw IllegalArgumentException("Please enter a valid number.") | ||
return input.toIntOrNull() ?: throw java.lang.IllegalArgumentException("Please enter a valid number.") | ||
} | ||
|
||
fun enterManualTickets(): List<Ticket> { | ||
println("\nEnter the number of manual tickets to purchase:") | ||
val numberOfManualTickets = readlnOrNull()?.toIntOrNull() | ||
?: throw IllegalArgumentException("Please enter a valid number of manual tickets.") | ||
|
||
println("\nEnter the numbers for manual tickets.") | ||
val tickets = mutableListOf<Ticket>() | ||
repeat(numberOfManualTickets) { | ||
tickets.add(Ticket(enterLottoNumbers())) | ||
} | ||
return tickets | ||
} | ||
|
||
private fun enterLottoNumbers(): List<Int> { | ||
val input = readlnOrNull() ?: throw IllegalArgumentException("Please enter a valid numbers.") | ||
return input.split(",").map { | ||
it.trim().toIntOrNull() | ||
?: throw IllegalArgumentException("Please enter a valid numbers.") | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package lotto.domain | ||
|
||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
|
||
class TicketTest { | ||
@Test | ||
fun `fail when under 6 numbers is entered`() { | ||
assertThrows<IllegalArgumentException> { | ||
Ticket(listOf(1, 2, 3, 4, 5)) | ||
} | ||
} | ||
} |
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.
When we purchase the tickets, a new instance of
LottoShop
is created each time.Do you think that's necessary? How else could we handle that?
Here’s a reference you might find helpful:
(+) Similarly,
LottoNumber
is a value object with a fixed range of values.It might be worth thinking about how instances are managed.
I recommend revisiting what we covered in the last feedback session!