-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathLottoController.java
More file actions
79 lines (66 loc) · 2.56 KB
/
LottoController.java
File metadata and controls
79 lines (66 loc) · 2.56 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
package lotto.controller;
import lotto.domain.Lotto;
import lotto.domain.Ranking;
import lotto.domain.WinnigResult;
import lotto.util.Constants;
import lotto.util.ExceptionMessage;
import lotto.util.LottoGenerator;
import lotto.util.MoneyValidator;
import lotto.view.InputView;
import lotto.view.OutputView;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class LottoController {
private final InputView inputView;
private final OutputView outputView;
public LottoController(InputView inputView, OutputView outputView) {
this.inputView = inputView;
this.outputView = outputView;
}
public void start(){
try {
int money=inputView.readMoney();
LottoGenerator lottoGenerator = new LottoGenerator();
List<Lotto> lottos = lottoGenerator.generate(money);
List<Integer> winningNumbers = inputView.readWinningNumbers();
int bonusNum = inputView.readBonusNumber(winningNumbers);
WinnigResult winnigResult = new WinnigResult(new Lotto(winningNumbers), bonusNum);
lottoResult(lottos, winnigResult, money / 1000);
}catch (IllegalArgumentException e){
System.out.println(e.getMessage());
}
}
private void lottoResult(List<Lotto> lottoList, WinnigResult winnigResult, int ticketNum){
Map<Ranking, Integer> result = setResult();
Ranking rank;
OutputView.printSuccessResult();
for (int i = 0; i < lottoList.size(); i++) {
rank = winnigResult.match(lottoList.get(i));
result.put(rank, result.get(rank) + 1);
}
printResult(result);
printEarningRate(result, ticketNum);
}
private void printResult(Map<Ranking, Integer> result) {
for (int i = Ranking.values().length - 1; i >= 0; i--) {
Ranking.values()[i].printMessage(result.get(Ranking.values()[i]));
}
}
private void printEarningRate(Map<Ranking, Integer> result, int lottoAmount) {
double EarningRate = 0;
for (Ranking rank : result.keySet()) {
EarningRate =
EarningRate + ((double) (rank.getWinningAmount()) / (lottoAmount * Constants.LOTTO_PRICE) * (result.get(
rank)) * (100));
}
OutputView.printRevenueRate(EarningRate);
}
private Map<Ranking, Integer> setResult() {
Map<Ranking, Integer> result = new LinkedHashMap<>();
for (Ranking rank : Ranking.values()) {
result.put(rank, 0);
}
return result;
}
}