-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLottoController.java
More file actions
135 lines (118 loc) · 4.17 KB
/
Copy pathLottoController.java
File metadata and controls
135 lines (118 loc) · 4.17 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package lotto.controller;
import static lotto.utils.LottoConstants.LOTTO_PRICE;
import java.util.List;
import lotto.model.dto.WinningDataDto;
import lotto.service.LottoControlService;
import lotto.service.RandomService;
import lotto.utils.ExceptionConstants;
import lotto.utils.Utility;
import lotto.view.InputView;
import lotto.view.OutputView;
public class LottoController {
InputView inputView;
OutputView outputView;
LottoControlService lottoControlService;
RandomService randomService;
ErrorHandler errorHandler;
private static final int MAX_ATTEMPTS = 100;
public LottoController(InputView inputView, OutputView outputView, LottoControlService lottoControlService,
RandomService randomService) {
this.inputView = inputView;
this.outputView = outputView;
this.lottoControlService = lottoControlService;
this.randomService = randomService;
errorHandler = new ErrorHandler();
}
public void run() {
try {
int amount = inputPrice();
inputLottoNumber();
outputLottoData(amount);
inputBonusNumber();
composeLottoSystem();
printResult(amount);
} catch (Exception e) {
outputView.printException(e);
}
}
private int inputPrice() {
while (true) {
if (errorHandler.willGenerateError()) {
throw new IllegalStateException(ExceptionConstants.INTERNAL_SERVER_ERROR.getMessage());
}
try {
int price = inputView.getPrice();
validatePrice(price);
lottoControlService.buyLotto(price / LOTTO_PRICE, randomService::getLottoNumbers);
errorHandler.resetAttemptCount();
return price / LOTTO_PRICE;
} catch (Exception e) {
outputView.printException(e);
}
}
}
private void validatePrice(int price) {
if (!Utility.isDividedByThousand(price)) {
throw new IllegalArgumentException(ExceptionConstants.INDIVISIBLE_PRICE.getMessage());
}
}
private void outputLottoData(int amount) {
outputView.printResultGuide(amount);
lottoControlService.getLotto().forEach(lotto -> outputView.printLotto(lotto));
}
private void inputLottoNumber() {
while (true) {
if (errorHandler.willGenerateError()) {
throw new IllegalStateException(ExceptionConstants.INTERNAL_SERVER_ERROR.getMessage());
}
try {
String line = inputView.getLottoNumber();
lottoControlService.setWinningNumbers(line);
errorHandler.resetAttemptCount();
break;
} catch (Exception e) {
outputView.printException(e);
}
}
}
private void inputBonusNumber() {
while (true) {
if (errorHandler.willGenerateError()) {
throw new IllegalStateException(ExceptionConstants.INTERNAL_SERVER_ERROR.getMessage());
}
try {
int bonusNumber = inputView.getBonusNumber();
lottoControlService.setBonusNumber(bonusNumber);
errorHandler.resetAttemptCount();
break;
} catch (Exception e) {
outputView.printException(e);
}
}
}
private void composeLottoSystem() {
lottoControlService.composeLotto();
}
private void printResult(int amount) {
List<WinningDataDto> lottoResult = lottoControlService.checkWinning();
outputView.printResult(lottoResult);
float result = lottoControlService.calculateROI(lottoResult, amount * LOTTO_PRICE);
outputView.printRateOfReturn(result);
}
static class ErrorHandler {
private int attempts;
ErrorHandler() {
this.attempts = 0;
}
boolean willGenerateError() {
attempts++;
if (attempts > MAX_ATTEMPTS) {
return true;
}
return false;
}
void resetAttemptCount() {
attempts = 0;
}
}
}