-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathLottoController.java
More file actions
97 lines (88 loc) · 3.13 KB
/
LottoController.java
File metadata and controls
97 lines (88 loc) · 3.13 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
package lotto.controller;
import lotto.domain.*;
import lotto.view.InputView;
import lotto.view.OutputView;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class LottoController {
public LottoController(){
}
private static final int ticketPrice = 1000;
private static final int percentage = 100;
private static PlayerLottoAmount playerLottoAmount;
private static List<Integer> lotto = new ArrayList<>();
private static List<Lotto> lottoList;
private static WinResult winResult;
public void run(){
try{
start();
}catch(IllegalArgumentException e){
e.printStackTrace();
}
}
public void start(){
int ticketCnt = inputPlayerAmount();
OutputView.printTicketCount(ticketCnt);
lottoList = makeLottoList(ticketCnt);
winResult = validateBonus();
lottoResult(lottoList,winResult,ticketCnt);
}
public int inputPlayerAmount(){
playerLottoAmount = new PlayerLottoAmount(InputView.inputPlayerAmount());
return playerLottoAmount.calcLottoCount();
}
public WinResult validateBonus(){
Lotto lotto = new Lotto(InputView.inputLottoWinningNum());
List<Integer> winNum = lotto.getLottoNumbers();
int pick = InputView.inputBonusNumber();
lotto.validateBonusNumber(winNum,pick);
winResult = new WinResult(new Lotto(winNum),pick);
return winResult;
}
private static List<Lotto> makeLottoList(int ticketCnt){
lottoList = new ArrayList<>();
for (int i = 0; i<ticketCnt;i++){
lottoList.add(makeLotto());
}
return lottoList;
}
private static Lotto makeLotto(){
LottoNumbers lottoNumbers = new LottoNumbers();
lotto = new ArrayList<>();
lotto = lottoNumbers.setRandomNumbers();
System.out.println(lotto);
return new Lotto(lotto);
}
private void lottoResult(List<Lotto> lottoList, WinResult winLotto,int amount){
Map<Ranking,Integer> result = setResult();
Ranking rank;
OutputView.printSuccessResult();
for(int i= 0;i<lottoList.size();i++){
rank = winLotto.match(lottoList.get(i));
result.put(rank,result.get(rank) + 1);
}
printResult(result);
printEarn(result,amount);
}
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 printEarn(Map<Ranking,Integer> result ,int lottoAmount){
double earnRate = 0;
for (Ranking rank : result.keySet()){
earnRate = earnRate + ((double) (rank.getWinAmount())/ (lottoAmount * ticketPrice) * (result.get(rank)) * (percentage));
}
OutputView.printRevenueRate(earnRate);
}
private Map<Ranking,Integer> setResult(){
Map<Ranking, Integer> result = new LinkedHashMap<>();
for (Ranking rank : Ranking.values()){
result.put(rank,0);
}
return result;
}
}