-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathLottoController.java
More file actions
145 lines (123 loc) · 4.94 KB
/
LottoController.java
File metadata and controls
145 lines (123 loc) · 4.94 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
package controller;
import model.*;
import view.InputView;
import view.OutputView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class LottoController {
private InputView inputView = new InputView();
private OutputView outputView = new OutputView();
private LottoList lottoList;
public LottoController() {
}
public void startLottoGame() {
UserCount userMoney = receiveMoneyInput();
ManualCount manualCount = manualLottoCount();
if(manualCount.getManualCount() == 0){
displayLottoTickets(userMoney, null);
}
else{
displayLottoTickets(userMoney, manualLottoNumbers(manualCount.getManualCount()));
}
Lotto winningNumbers = getWinningNumbers();
BonusNumber bonusNum = receiveBonusNumberInput();
validateDuplicate(winningNumbers, bonusNum);
List<Integer> matchCounts = calculateMatches(lottoList, winningNumbers, bonusNum);
double winRate = calculateWinningRate(matchCounts, userMoney);
outputView.printResults(matchCounts, Math.floor(winRate*100)/100 );
}
private ManualCount manualLottoCount(){
return new ManualCount(inputView.inputManualLottoCount());
}
private List<Lotto> manualLottoNumbers(int count){
List<String> manualLottoNums = inputView.manualLottos(count);
List<Lotto> manualLottos = new ArrayList<>();
for (String manualLotto : manualLottoNums) {
Lotto lotto = new Lotto();
manualLottos.add(lotto.convertToList(manualLotto));
}
return manualLottos;
}
private BonusNumber receiveBonusNumberInput() {
return new BonusNumber(inputView.inputBonusNumber());
}
private void validateDuplicate(Lotto winningLotto, BonusNumber bonusNumber){
winningLotto.validateDuplicateWithBonus(bonusNumber.getBonusNumber());
}
private UserCount receiveMoneyInput() {
return new UserCount(inputView.inputPrice());
}
private void displayLottoTickets(UserCount count, List<Lotto> lottos){
int autoCount = 0;
int manualCount = 0;
if(lottos == null){
autoCount = count.getCount();
manualCount = 0;
}
else{
autoCount = count.getCount() - lottos.size();
manualCount = lottos.size();
}
outputView.printTicketCount(autoCount, manualCount);
lottoList = generateLottoList(autoCount, lottos);
outputView.printLottoNumbers(lottoList);
}
LottoList generateLottoList(int count, List<Lotto> lottos){
lottoList = new LottoList();
if(lottos != null){
for (Lotto lotto : lottos) {
lottoList.setLottoList(lotto);
}
}
for(int i=0; i<count; i++){
lottoList.setLottoList(generateLotto());
}
return lottoList;
}
private Lotto generateLotto(){
List<Integer> numbers = new ArrayList<>();
AutoLotto autoLotto = new AutoLotto();
numbers = autoLotto.getAutoLotto();
return new Lotto(numbers);
}
private Lotto getWinningNumbers(){
String winnerNumbersStr = inputView.inputWinnerNumber();
Lotto winningLotto = new Lotto();
return winningLotto.convertToList(winnerNumbersStr);
}
private List<Integer> calculateMatches(LottoList ticketList, Lotto winningNumbers, BonusNumber bonusNum) {
List<Integer> matchCounts = new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0));
for (Lotto ticket : ticketList.getLottoList()) {
int countMatches = ticket.calculateMatches(winningNumbers);
updateMatchCounts(matchCounts, countMatches, bonusNum);
}
return matchCounts;
}
private void updateMatchCounts(List<Integer> matchCounts, int countMatches, BonusNumber bonusNum) {
Predicate<Integer> isBonusMatch = num -> num == 5 && bonusNum.getBonusNumber() > 0;
if (countMatches == 3) {
matchCounts.set(0, matchCounts.get(0) + 1);
} else if (countMatches == 4) {
matchCounts.set(1, matchCounts.get(1) + 1);
} else if (countMatches == 5) {
if (isBonusMatch.test(countMatches)) {
matchCounts.set(3, matchCounts.get(3) + 1);
} else {
matchCounts.set(2, matchCounts.get(2) + 1);
}
} else if (countMatches == 6) {
matchCounts.set(4, matchCounts.get(4) + 1);
}
}
double calculateWinningRate(List<Integer> matchCounts, UserCount count) {
int totalWinnings = matchCounts.get(0) * 5000 +
matchCounts.get(1) * 50000 +
matchCounts.get(2) * 1500000 +
matchCounts.get(3) * 30000000 +
matchCounts.get(4) * 2000000000;
double winningRate = (double) totalWinnings / (double) count.getPrice();
return Math.floor(winningRate * 100) / 100;
}
}