Remove redundant Card record code

Java Records automatically acquire an implementation of equals and
hashCode that accounts for their components. They also have read
accessors for their components (card.suit() to get the suit).
This commit is contained in:
Dave Burke
2022-03-18 17:01:06 -05:00
parent 0b1f809707
commit cdf194f770
5 changed files with 8 additions and 40 deletions

View File

@@ -10,9 +10,9 @@ public final class ScoringUtils {
* @return The numeric value of a hand. A value over 21 indicates a bust.
*/
public static final int scoreHand(List<Card> hand) {
int nAces = (int) hand.stream().filter(c -> c.getValue() == 1).count();
int nAces = (int) hand.stream().filter(c -> c.value() == 1).count();
int value = hand.stream()
.mapToInt(Card::getValue)
.mapToInt(Card::value)
.filter(v -> v != 1) // start without aces
.map(v -> v > 10 ? 10 : v) // all face cards are worth 10. The 'expr ? a : b' syntax is called the
// 'ternary operator'