-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathCards.java
More file actions
44 lines (34 loc) · 1012 Bytes
/
Cards.java
File metadata and controls
44 lines (34 loc) · 1012 Bytes
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
package blackjack.domain;
import java.util.List;
import java.util.stream.Collectors;
public class Cards {
private final List<Card> cards;
public Cards(List<Card> cards) {
this.cards = cards;
}
public void addCard(Card card) {
cards.add(card);
}
public int getScore() {
int total = cards.stream().mapToInt(card -> card.getDenomination().getScore()).sum();
int aceCount = (int) cards.stream().filter(card -> card.getDenomination().isAce()).count();
for (int i = 0; i < aceCount; i++) {
total = checkAceOneOrEleven(total);
}
return total;
}
private int checkAceOneOrEleven(int total) {
if (total + 10 <= 21) {
return total + 10;
}
return total;
}
public List<Card> getCards() {
return cards;
}
public List<String> getCardNames() {
return cards.stream()
.map(Card::toString)
.collect(Collectors.toList());
}
}