-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathCards.java
More file actions
46 lines (35 loc) · 1.12 KB
/
Cards.java
File metadata and controls
46 lines (35 loc) · 1.12 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
package blackjack.domain.card;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Cards {
private final static int BLACKJACK = 21;
private final List<Card> cards;
public Cards(final List<Card> cards) {
this.cards = cards;
}
private static int getScoreToSum(final int score1, final int score2) {
if ((score2 == CardNumber.ACE.getScore()) && (score1 + CardNumber.ACE.getScore() > 21)) {
return 1;
}
return score2;
}
public void add(final Card card) {
cards.add(card);
}
public boolean sum() {
return sumScore() >= BLACKJACK;
}
public int sumScore() {
Comparator<Card> comparator = (a, b) -> b.getCardNumber().getScore() - a.getCardNumber()
.getScore();
List<Card> newCards = new ArrayList<>(cards);
newCards.sort(comparator);
return newCards.stream()
.mapToInt(card -> card.getCardNumber().getScore())
.reduce(0, (a, b) -> a + getScoreToSum(a, b));
}
public List<Card> getCards() {
return cards;
}
}