Skip to content
Snippets Groups Projects
Commit 3ecdba18 authored by Steven Ha's avatar Steven Ha
Browse files

Created test class HandOfCardsTest, refactored class HandOfCards, implemented...

Created test class HandOfCardsTest, refactored class HandOfCards, implemented methods for calculating sum of values, implemented methods to check for specific cards, implemented JavaDocs for HandOfCards class.
parent fa5d535b
No related branches found
No related tags found
1 merge request!8Created test class HandOfCardsTest, refactored class HandOfCards, implemented...
package edu.ntnu.idatt2003;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
* A class representing a hand of cards.
*/
public class HandOfCards {
private List<PlayingCard> hand;
/**
* Constructor for HandOfCards
* @param hand A list of PlayingCard objects
*/
public HandOfCards(List<PlayingCard> hand) {
this.hand = hand;
}
public boolean isFlush() {
if (hand.size() < 5) {
return false;
/**
* Method for calculating the sum of the face values of the cards in the hand
* @return The sum of the face values
*/
public int sumOfCardValues() {
return hand.stream()
.mapToInt(PlayingCard::getFace)
.sum();
}
return false;
/**
* Method for finding all the hearts in the hand
* @return A string containing the hearts in the hand
*/
public String heartsOnHand() {
String hearts = hand.stream()
.filter(card -> card.getSuit() == 'H')
.map(PlayingCard::getAsString)
.collect(Collectors.joining(" "));
return hearts.isEmpty() ? "No Hearts" : hearts;
}
/**
* Method for checking if the hand contains the queen of spades
* @return True if the hand contains the queen of spades, false otherwise
*/
public boolean hasQueenOfSpades() {
return hand.stream()
.anyMatch(card -> card.getSuit() == 'S' && card.getFace() == 12);
}
/**
* Method for checking if the hand contains a flush
* @return True if the hand contains a flush, false otherwise
*/
public boolean isFiveFlush() {
return hand.stream()
.collect(Collectors.groupingBy(PlayingCard::getSuit, Collectors.counting()))
.values()
.stream()
.anyMatch(count -> count >= 5);
}
/**
* Method for returning the hand as a string
* @return A string representation of the hand
*/
@Override
public String toString() {
return hand.stream()
.map(PlayingCard::getAsString)
.collect(Collectors.joining(" "));
}
}
import static org.junit.Assert.*;
import edu.ntnu.idatt2003.HandOfCards;
import edu.ntnu.idatt2003.PlayingCard;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
public class HandOfCardsTest {
private HandOfCards hand;
@Before
public void setUp() {
hand = new HandOfCards(Arrays.asList(
new PlayingCard('H', 10),
new PlayingCard('H', 11),
new PlayingCard('H', 1),
new PlayingCard('S', 12),
new PlayingCard('D', 5)
));
}
@Test
public void testSumOfCardValues() {
assertEquals(39, hand.sumOfCardValues());
}
@Test
public void testHeartsOnHand() {
String expected = "H10 H11 H1";
assertEquals(expected, hand.heartsOnHand());
}
@Test
public void testHasQueenOfSpades() {
assertTrue(hand.hasQueenOfSpades());
}
@Test
public void testIsFiveFlush() {
HandOfCards flushHand = new HandOfCards(Arrays.asList(
new PlayingCard('D', 2),
new PlayingCard('D', 3),
new PlayingCard('D', 4),
new PlayingCard('D', 5),
new PlayingCard('D', 6)
));
assertTrue(flushHand.isFiveFlush());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment