forked from next-step/java-lotto-clean-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLottoController.java
More file actions
75 lines (62 loc) · 2.8 KB
/
LottoController.java
File metadata and controls
75 lines (62 loc) · 2.8 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
package controller;
import domain.BonusBall;
import domain.Lotto;
import domain.LottoNumber;
import domain.LottoShop;
import domain.Lottos;
import domain.ManualLottoCount;
import domain.PurchaseAmount;
import domain.WinningLotto;
import domain.WinningStatistics;
import dto.WinningResult;
import view.InputView;
import view.OutputView;
import java.util.function.Supplier;
public class LottoController {
private final InputView inputView;
private final OutputView outputView;
private final LottoShop lottoShop;
public LottoController(InputView inputView, OutputView outputView, LottoShop lottoShop) {
this.inputView = inputView;
this.outputView = outputView;
this.lottoShop = lottoShop;
}
public void run() {
PurchaseAmount purchaseAmount = retryUntilValid(() -> new PurchaseAmount(inputView.readAmount()));
ManualLottoCount manualLottoCount = retryUntilValid(() -> new ManualLottoCount(inputView.readManualCount(), purchaseAmount));
Lottos lottos = retryUntilValid(() -> purchaseLottos(purchaseAmount, manualLottoCount));
printPurchaseResult(lottos, manualLottoCount);
WinningLotto winningLotto = readWinningLotto();
WinningStatistics winningStatistics = WinningStatistics.from(lottos, winningLotto);
printWinningResult(winningStatistics, purchaseAmount);
}
private Lottos purchaseLottos(PurchaseAmount purchaseAmount, ManualLottoCount manualLottoCount) {
Lottos manualLottos = Lottos.from(inputView.readManualNumbers(manualLottoCount.count()));
return lottoShop.purchase(purchaseAmount, manualLottos);
}
private void printPurchaseResult(Lottos lottos, ManualLottoCount manualLottoCount) {
int autoCount = lottos.size() - manualLottoCount.count();
outputView.printResultHeader(manualLottoCount.count(), autoCount);
outputView.printLottos(lottos.toNumberLists());
}
private WinningLotto readWinningLotto() {
Lotto winningLotto = retryUntilValid(() -> Lotto.from(inputView.readWinningNumbers()));
return retryUntilValid(() -> {
BonusBall bonusBall = new BonusBall(new LottoNumber(inputView.readBonusBall()));
return new WinningLotto(winningLotto, bonusBall);
});
}
private void printWinningResult(WinningStatistics winningStatistics, PurchaseAmount purchaseAmount) {
outputView.printWinningStatistics(WinningResult.from(winningStatistics));
outputView.printProfitRate(winningStatistics.calculateProfitRate(purchaseAmount));
}
private <T> T retryUntilValid(Supplier<T> supplier) {
while (true) {
try {
return supplier.get();
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
}
}
}
}