-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLottoChecker.java
More file actions
83 lines (64 loc) · 2.35 KB
/
LottoChecker.java
File metadata and controls
83 lines (64 loc) · 2.35 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
package lotto.domain;
import java.util.Arrays;
import java.util.List;
public class LottoChecker {
private final CheckCounter checkCounter = new CheckCounter();
private final List<Integer> winningNumbers;
private final int bonusBall;
public LottoChecker(String winnings, int bonusBall) {
this.winningNumbers = convertToList(winnings);
this.bonusBall = bonusBall;
}
public static LottoChecker newChecker(String winnings, int bonusBall) {
return new LottoChecker(winnings, bonusBall);
}
public int countMatchedNumber(LottoTicket ticket) {
List<Integer> ticketNumbers = ticket.getLottoNumbers();
int count = 0;
for (Integer number : this.winningNumbers) {
count = plusCount(ticketNumbers, count, number);
}
return count;
}
private int plusCount(List<Integer> ticketNumbers, int count, Integer number) {
if (ticketNumbers.contains(number)) {
count += 1;
}
return count;
}
public CheckCounter checkAllTickets(LottoMachine lottoMachine) {
List<LottoTicket> tickets = lottoMachine.getTickets();
for (LottoTicket ticket : tickets) {
int matchCount = this.countMatchedNumber(ticket);
boolean bonusBallMatch = isContainingBonusBall(ticket);
executeCheckCounter(matchCount, bonusBallMatch);
}
return this.checkCounter;
}
private void executeCheckCounter(int matchCount, boolean bonusBallMatch) {
if (matchCount == 5 && bonusBallMatch) {
int secondWinner = 7;
addToCheckCounter(secondWinner);
return;
}
addToCheckCounter(matchCount);
}
private void addToCheckCounter(int matchCount) {
if (this.checkCounter.has(matchCount)) {
this.checkCounter.countUp(matchCount);
return;
}
this.checkCounter.setInitial(matchCount);
}
private List<Integer> convertToList(String string) {
return Arrays.asList(
Arrays.stream(string.split(","))
.mapToInt(Integer::parseInt)
.boxed()
.toArray(Integer[]::new)
);
}
private boolean isContainingBonusBall(LottoTicket ticket) {
return ticket.getLottoNumbers().contains(this.bonusBall);
}
}