-
Notifications
You must be signed in to change notification settings - Fork 591
Expand file tree
/
Copy pathApp.js
More file actions
183 lines (166 loc) · 5.31 KB
/
App.js
File metadata and controls
183 lines (166 loc) · 5.31 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { MissionUtils } from "@woowacourse/mission-utils";
import Lotto from "./Lotto.js";
class App {
constructor() {
this.winningNumbers = [];
this.bonusNumber = 0;
}
async inputMoney() {
let money;
let isValidated = false;
while (!isValidated) {
const inputString = await MissionUtils.Console.readLineAsync(
"구입금액을 입력해 주세요.\n"
);
money = Number(inputString.trim());
isValidated = this.validateMoney(money);
}
return money;
}
validateMoney(money) {
if (isNaN(money)) {
MissionUtils.Console.print("[ERROR] 숫자만 입력해주세요.\n");
}else if(money < 1000){
MissionUtils.Console.print("[ERROR] 최소 1000 이상을 입력해야 합니다.\n");
}
else if (money % 1000 !== 0) {
MissionUtils.Console.print("[ERROR] 1000원 단위로 입력해주세요.\n");
} else return true;
}
publishLotto(money) {
const lottoCount = money / 1000;
const lottos = [];
for (let i = 0; i < lottoCount; i++) {
const randomNumbers = MissionUtils.Random.pickUniqueNumbersInRange(
1,
45,
6
);
try {
const lotto = new Lotto(randomNumbers);
lottos.push(lotto);
} catch (error) {
MissionUtils.Console.print(error.message);
i--;
}
}
return lottos;
}
printLottos(lottos) {
MissionUtils.Console.print(`\n${lottos.length}개를 구매했습니다.`);
for (const lotto of lottos) {
MissionUtils.Console.print(lotto.toString());
}
}
async inputNumbers() {
let numbers;
let isValidated = false;
while (!isValidated) {
const inputString = await MissionUtils.Console.readLineAsync(
"\n당첨 번호를 입력해주세요.\n"
);
numbers = inputString.split(",").map((number) => Number(number.trim()));
isValidated = this.validateNumbers(numbers);
}
this.winningNumbers = numbers;
}
validateNumbers(numbers) {
if (numbers.length !== 6) {
MissionUtils.Console.print("[ERROR] 6개의 숫자를 입력해주세요.\n");
} else if (numbers.some((number) => isNaN(number))) {
MissionUtils.Console.print("[ERROR] 숫자만 입력해주세요.\n");
} else if (numbers.some((number) => number < 1 || number > 45)) {
MissionUtils.Console.print("[ERROR] 1~45 사이의 숫자를 입력해주세요.\n");
} else if (new Set(numbers).size !== 6) {
MissionUtils.Console.print("[ERROR] 중복된 숫자를 입력할 수 없습니다.\n");
} else return true;
}
async inputBonusNumber() {
let bonusNumber;
let isValidated = false;
while (!isValidated) {
const inputString = await MissionUtils.Console.readLineAsync(
"\n보너스 번호를 입력해주세요.\n"
);
bonusNumber = Number(inputString.trim());
isValidated = this.validateBonusNumber(bonusNumber);
}
this.bonusNumber = bonusNumber;
}
validateBonusNumber(bonusNumber) {
if (isNaN(bonusNumber)) {
MissionUtils.Console.print("[ERROR] 숫자만 입력해주세요.\n");
} else if (bonusNumber < 1 || bonusNumber > 45) {
MissionUtils.Console.print("[ERROR] 1~45 사이의 숫자를 입력해주세요.\n");
} else if (this.winningNumbers.includes(bonusNumber)) {
MissionUtils.Console.print("[ERROR] 당첨 번호와 중복될 수 없습니다.\n");
} else return true;
}
countMatchingNumbers(lotto) {
let count = 0;
lotto.getNumbers().forEach((number) => {
if (this.winningNumbers.includes(number)) {
count++;
}
});
return count;
}
computeRank(lotto) {
const count = this.countMatchingNumbers(lotto);
const isBonusMatched = lotto.hasNumber(this.bonusNumber);
switch (count) {
case 6:
return "1등";
case 5:
return isBonusMatched ? "2등" : "3등";
case 4:
return "4등";
case 3:
return "5등";
default:
return "꽝";
}
}
computeTotalResult(lottos) {
const result = { "1등": 0, "2등": 0, "3등": 0, "4등": 0, "5등": 0, 꽝: 0 };
for (const lotto of lottos) {
const rank = this.computeRank(lotto);
result[rank]++;
}
return result;
}
printResult(result) {
MissionUtils.Console.print(`
당첨 통계\n---
3개 일치 (5,000원) - ${result["5등"]}개
4개 일치 (50,000원) - ${result["4등"]}개
5개 일치 (1,500,000원) - ${result["3등"]}개
5개 일치, 보너스 볼 일치 (30,000,000원) - ${result["2등"]}개
6개 일치 (2,000,000,000원) - ${result["1등"]}개`);
}
computeProfit(result, money) {
const totalPrize =
result["5등"] * 5000 +
result["4등"] * 50000 +
result["3등"] * 1500000 +
result["2등"] * 30000000 +
result["1등"] * 2000000000;
const profit = ((totalPrize / money) * 100).toFixed(1);
return profit;
}
printProfit(profit) {
MissionUtils.Console.print(`총 수익률은 ${profit}%입니다.`);
}
async play() {
const money = await this.inputMoney();
const lottos = this.publishLotto(money);
this.printLottos(lottos);
await this.inputNumbers();
await this.inputBonusNumber();
const result = this.computeTotalResult(lottos);
this.printResult(result);
const profit = this.computeProfit(result, money);
this.printProfit(profit);
}
}
export default App;