|
1 | 1 | package model; |
2 | 2 |
|
3 | | -import java.util.*; |
4 | | -import java.util.stream.Collectors; |
5 | | -import java.util.stream.IntStream; |
| 3 | +import java.util.List; |
6 | 4 |
|
7 | 5 | public class Lotto { |
8 | 6 |
|
9 | | - // 로또 번호 관련 상수 선언 |
10 | | - public static final int LOTTO_MIN_NUMBER = 1; |
11 | | - public static final int LOTTO_MAX_NUMBER = 45; |
12 | | - public static final int LOTTO_CREATE_SIZE = 6; |
13 | | - public static final int LOTTO_PRICE = 1000; |
14 | | - public static final List<Integer> LOTTO_NUMBER_POOL = |
15 | | - IntStream |
16 | | - .rangeClosed(LOTTO_MIN_NUMBER,LOTTO_MAX_NUMBER) |
17 | | - .boxed() |
18 | | - .collect(Collectors.toList()); |
19 | | - |
20 | | - private List<Integer> numbers = new ArrayList<>(); |
21 | | - |
22 | | - public static int getTicketCount(int purchaseAmount){ |
23 | | - return purchaseAmount / LOTTO_PRICE; |
24 | | - } |
25 | | - |
26 | | - public Lotto(){ |
27 | | - this.numbers = createLottoNumbers(); |
28 | | - } |
| 7 | + private static final int LOTTO_NUMBER_SIZE = 6; |
| 8 | + private static final int LOTTO_NUMBER_MAX = 45; |
| 9 | + private static final int LOTTO_NUMBER_MIN = 1; |
29 | 10 |
|
30 | | - private List<Integer> createLottoNumbers(){ |
31 | | - List<Integer> shuffledNumbers = new ArrayList<>(LOTTO_NUMBER_POOL); |
32 | | - Collections.shuffle(shuffledNumbers); |
33 | | - numbers = shuffledNumbers.subList(0, LOTTO_CREATE_SIZE); |
| 11 | + private final List<Integer> numbers; |
34 | 12 |
|
35 | | - return numbers; |
| 13 | + public Lotto(List<Integer> numbers) { |
| 14 | + if (numbers == null || numbers.isEmpty()) { |
| 15 | + throw new IllegalArgumentException("로또 번호 목록이 비어 있거나 null 입니다."); |
| 16 | + } |
| 17 | + validateNumbers(numbers); |
| 18 | + this.numbers = numbers; // numbers를 초기화 |
36 | 19 | } |
37 | 20 |
|
38 | | - public static List<Lotto> generateLottoTickets(int ticketCount){ |
39 | | - List<Lotto> tickets = new ArrayList<>(); |
40 | | - |
41 | | - for (int i = 0; i < ticketCount; i++){ |
42 | | - tickets.add(new Lotto()); |
| 21 | + private void validateNumbers(List<Integer> numbers) { |
| 22 | + if (numbers.size() != LOTTO_NUMBER_SIZE) { |
| 23 | + throw new IllegalArgumentException("Number size must be equal to " + LOTTO_NUMBER_SIZE); |
43 | 24 | } |
44 | 25 |
|
45 | | - return tickets; |
| 26 | + for (int number : numbers) { |
| 27 | + validateNumber(number); |
| 28 | + } |
46 | 29 | } |
47 | 30 |
|
48 | | - public List<Integer> getNumbers() { |
49 | | - List<Integer> sortedNumbers = new ArrayList<>(numbers); |
50 | | - Collections.sort(sortedNumbers); |
| 31 | + private void validateNumber(int number) { |
| 32 | + if (number < LOTTO_NUMBER_MIN || number > LOTTO_NUMBER_MAX) { |
| 33 | + throw new IllegalArgumentException("Number must be between " + LOTTO_NUMBER_MIN + " and " + LOTTO_NUMBER_MAX); |
| 34 | + } |
| 35 | + } |
51 | 36 |
|
52 | | - return sortedNumbers; |
| 37 | + @Override |
| 38 | + public String toString() { |
| 39 | + return numbers.toString(); |
53 | 40 | } |
54 | 41 | } |
0 commit comments