-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathLottoTicketsFactory.java
More file actions
59 lines (50 loc) · 2.25 KB
/
LottoTicketsFactory.java
File metadata and controls
59 lines (50 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package lotto.domain;
import java.util.ArrayList;
import java.util.List;
public class LottoTicketsFactory {
public static LottoTickets create(PurchaseAmount purchaseAmount) {
int count = purchaseAmount.getLottoCount();
List<Lotto> lottos = generateAutoLottos(count);
return new LottoTickets(lottos, new ManualLottos(null));
}
public static LottoTickets create(PurchaseAmount purchaseAmount, ManualLottos manualLottos) {
validateManualCount(purchaseAmount, manualLottos);
// List<Lotto> allLottos = createLottos(purchaseAmount, manualLottos);
List<Lotto> lottos = convertToLottos(manualLottos);
List<Lotto> allLottos = createLottos(purchaseAmount, lottos, manualLottos.getCount());
return new LottoTickets(allLottos, manualLottos);
}
private static List<Lotto> convertToLottos(ManualLottos manualLottos) {
List<Lotto> lottos = new ArrayList<>();
for (String input : manualLottos.getManualLottosInput()) {
lottos.add(parseLotto(input));
}
return lottos;
}
private static Lotto parseLotto(String input) {
String[] tokens = input.split(",");
List<Integer> numbers = new ArrayList<>();
for (String token : tokens) {
numbers.add(Integer.parseInt(token.trim()));
}
return Lotto.from(numbers);
}
private static void validateManualCount(PurchaseAmount purchaseAmount, ManualLottos manualLottos) {
if (manualLottos.getCount() > purchaseAmount.getLottoCount()) {
throw new IllegalArgumentException("수동로또 개수가 구입 가능한 개수보다 많습니다.");
}
}
private static List<Lotto> createLottos(PurchaseAmount purchaseAmount, List<Lotto> manualLottosList, int manualCount) {
List<Lotto> lottos = new ArrayList<>(manualLottosList);
int autoCount = purchaseAmount.getLottoCount() - manualCount;
lottos.addAll(generateAutoLottos(autoCount));
return lottos;
}
private static List<Lotto> generateAutoLottos(int count) {
List<Lotto> lottos = new ArrayList<>();
for (int i = 0; i < count; i++) {
lottos.add(Lotto.from(LottoNumberGenerator.generate()));
}
return lottos;
}
}