-
Notifications
You must be signed in to change notification settings - Fork 423
Expand file tree
/
Copy pathHitRecords.java
More file actions
72 lines (55 loc) · 1.66 KB
/
HitRecords.java
File metadata and controls
72 lines (55 loc) · 1.66 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
package bowling;
import java.util.ArrayList;
import java.util.List;
public class HitRecords {
private static final int HIT_ONCE = 1;
private final List<HitRecord> hitRecords;
private Score score;
public HitRecords() {
this.hitRecords = new ArrayList<>();
this.score = Score.ofZero();
}
public boolean hitOnce() {
return hitRecords.size() == HIT_ONCE;
}
public void addRecord(HitRecord hitRecord) {
this.hitRecords.add(hitRecord);
}
public void addStrike() {
this.hitRecords.add(HitRecord.of(10, BowilingTerm.STRIKE));
score = Score.ofStrike();
}
public void addSpare() {
this.hitRecords.add(HitRecord.of(10, BowilingTerm.SPARE));
score = Score.ofSpare();
}
public void addGutter() {
this.hitRecords.add(HitRecord.of(0, BowilingTerm.GUTTER));
score = Score.ofMiss(hitSum());
}
public void addMiss(int count) {
this.hitRecords.add(HitRecord.of(count, BowilingTerm.MISS));
score = Score.ofMiss(hitSum());
}
public boolean isRecordAllStrike() {
return hitRecords.stream()
.allMatch(HitRecord::hitAll);
}
public boolean hitTimes(int hitTwice) {
return hitRecords.size() == hitTwice;
}
public List<HitRecord> getHitRecords() {
return hitRecords;
}
private int hitSum() {
return hitRecords.stream().mapToInt(HitRecord::getHitCount).sum();
}
public void calculateBonus(int hitCount) {
if (score.remainBonus()) {
score = score.addBonusScore(hitCount);
}
}
public Score getScore() {
return score;
}
}