Legger til funksjonalitet for å kikke på det første eller siste kortet i en kortstokk

This commit is contained in:
Kristian Knarvik 2020-03-09 13:21:52 +01:00
parent 1c1bbc6a79
commit 7a5b4fccd2
3 changed files with 36 additions and 2 deletions

View File

@ -77,5 +77,15 @@ public abstract class Deck<T> implements IDeck<T> {
}
return builder.toString();
}
@Override
public T peekTop() {
return cardList.get(0);
}
@Override
public T peekBottom() {
return cardList.get(size()-1);
}
}

View File

@ -4,6 +4,9 @@ import java.util.List;
/**
* Describes a deck
*
* Any card stored in the deck is assumed to be immutable. If it's not, the integrity of the deck cannot be
* guaranteed.
*/
public interface IDeck <T> {
@ -43,11 +46,22 @@ public interface IDeck <T> {
*/
int size();
/**
* Takes a peek at the card currently at the top of the deck
* @return The card at the top of the deck
*/
T peekTop();
/**
* Takes a peek at the card currently at the bottom of the deck
* @return The card at the bottom of the deck
*/
T peekBottom();
/**
* Gets a list of all cards in this deck
*
* The list should have the correct order according to the actual order within the deck. As an ICardWithoutSuit is
* immutable, the object reference can be returned directly.
* The list should have the correct order according to the actual order within the deck.
*
* @return A list of all cards in this deck
*/

View File

@ -113,4 +113,14 @@ public class ProgrammingCardDeckTest {
assertTrue(fullDeck.getCards().contains(card));
}
}
@Test
public void peekTop() {
assertEquals(programmingCard1, testDeck.peekTop());
}
@Test
public void peekBottom() {
assertEquals(programmingCard3, testDeck.peekBottom());
}
}