forked from knh4437/java-lotto
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLottoController.java
More file actions
73 lines (64 loc) · 2.5 KB
/
LottoController.java
File metadata and controls
73 lines (64 loc) · 2.5 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package lotto.controller;
import lotto.view.InputView;
import lotto.domain.Lotto;
import lotto.domain.*;
import lotto.domain.strategy.LottoGenerateStrategy;
import lotto.domain.strategy.LottoGenerator;
import lotto.domain.strategy.UserLottoGenerateStrategy;
import lotto.domain.strategy.WinningLottoGenerateStrategy;
import lotto.view.OutputView;
public class LottoController {
private final LottoGenerator lottoGenerator = new LottoGenerator();
private Budget budget;
public void runMachine() {
final Lottos userLottos = buyLotto();
final WinningLotto winningLotto = drawLotto();
final WinningStatistics winningStatistics = makeStatistics(userLottos, winningLotto);
OutputView.printWinningStaticstics(winningStatistics);
}
private Lottos buyLotto() {
try {
budget = Budget.from(InputView.getBudgetInput());
setLottoGeneratorStrategy(new UserLottoGenerateStrategy());
Lottos userMultipleLottos = lottoGenerator.generateLottosByBudget(budget);
return userMultipleLottos;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return buyLotto();
}
}
private WinningLotto drawLotto() {
try {
final Lotto lotto = getWinningLotto();
final Bonus bonus = getBonus();
return WinningLotto.of(lotto, bonus);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return drawLotto();
}
}
private Lotto getWinningLotto() {
try {
InputView.printRequireWinningNumbersMessage();
setLottoGeneratorStrategy(new WinningLottoGenerateStrategy());
return lottoGenerator.generateLotto();
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return getWinningLotto();
}
}
private Bonus getBonus(){
try {
return Bonus.from(InputView.getBonusInput());
} catch (IllegalArgumentException e){
System.out.println(e.getMessage());
return getBonus();
}
}
private WinningStatistics makeStatistics(final Lottos userLottos, final WinningLotto winningLotto) {
return WinningStatistics.of(userLottos, winningLotto, budget);
}
public void setLottoGeneratorStrategy(final LottoGenerateStrategy lottoGenerateStrategy) {
this.lottoGenerator.setLottoGenerateStrategy(lottoGenerateStrategy);
}
}