-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathLottoController.js
More file actions
84 lines (73 loc) · 2.29 KB
/
Copy pathLottoController.js
File metadata and controls
84 lines (73 loc) · 2.29 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
import WinningLotto from "../model/WinningLotto.js";
import LottoService from "../service/LottoService.js";
import LottoValidator from "../service/LottoValidator.js";
import { InputView, OutputView } from "../view.js";
class LottoController {
#inputView;
#outputView;
#validator;
#lottoService;
constructor() {
this.#inputView = InputView;
this.#outputView = OutputView;
this.#validator = new LottoValidator();
this.#lottoService = new LottoService();
}
async controlLotto() {
const amount = await this.#inputAmount();
const { change, lottos } = this.#lottoService.publish(amount);
this.#printPurchaseResult(amount, change, lottos);
const winningNumbers = await this.#inputWinningNumbers();
const bonusNumber = await this.#inputBonusNumber(winningNumbers);
const winningLotto = new WinningLotto(winningNumbers, bonusNumber);
const { resultMap } = this.#lottoService.fillMapResult(
lottos,
winningLotto
);
this.#outputView.printResult(resultMap);
}
#printPurchaseResult(amount, change, lottos) {
const lottoArray = [];
for (let lotto of lottos) {
lottoArray.push(lotto.getNumbers());
}
this.#outputView.printPurchasedLottos(lottoArray);
if (change !== 0) {
this.#outputView.printChangeMessage(amount, change);
}
}
async #inputAmount() {
while (true) {
try {
const amount = await this.#inputView.askAmount();
this.#validator.validateAmount(amount);
return amount;
} catch (error) {
this.#outputView.printErrorMessage(error.message);
}
}
}
async #inputWinningNumbers() {
while (true) {
try {
const winningNumbers = await this.#inputView.askWinningLotto();
this.#validator.validateWinningNumbers(winningNumbers);
return winningNumbers;
} catch (error) {
this.#outputView.printErrorMessage(error.message);
}
}
}
async #inputBonusNumber(winningNumbers) {
while (true) {
try {
const bonusNumber = await this.#inputView.askBonusNumber();
this.#validator.validateBonusNumber(winningNumbers, bonusNumber);
return bonusNumber;
} catch (error) {
this.#outputView.printErrorMessage(error.message);
}
}
}
}
export default LottoController;