feat: add getters and setters with tests for Joker, Hand, and Score classes

This commit is contained in:
2025-05-12 22:35:16 -04:00
parent cc59d4aaff
commit 7948d0cdbe
16 changed files with 171 additions and 33 deletions

View File

@@ -34,8 +34,18 @@ Each class includes its name, a `Score`, and a `verify(cards: List[Card])` metho
All combinations are tested with valid and invalid cases using MUnit.
The logic uses explicit loops and avoids higher-order functions, as required.
---
## Task 2 - Partial Delivery 3
This delivery improves encapsulation by adding explicit getters and setters to the main classes.
### Changes:
- Marked class fields as `private` where appropriate.
- Added `get` and `set` methods in:
- `Joker`: `getJokerType`, `setJokerType`
- `Hand`: `setCards`, `setJokers`
- `Score`: `getChips`, `setChips`, `getMultiplier`, `setMultiplier`
- Updated tests to cover the new accessors.
---

View File

@@ -99,7 +99,27 @@ class Hand {
* @return An immutable List[Joker] representing the jokers in hand.
*/
def getJokers: List[Joker] = jokers.toList
/**
* Replaces the current list of cards in hand with a new list.
* Intended for controlled updates, e.g., testing or state loading.
*
* @param newCards A list of Card objects to set as the hand.
*/
def setCards(newCards: List[Card]): Unit = {
cards.clear()
cards ++= newCards
}
/**
* Replaces the current list of jokers in hand with a new list.
* Intended for controlled updates, e.g., testing or state loading.
*
* @param newJokers A list of Joker objects to set as the jokers in hand.
*/
def setJokers(newJokers: List[Joker]): Unit = {
jokers.clear()
jokers ++= newJokers
}
/**
* Returns a string representation of the hand, listing cards and jokers.
* Useful for debugging and logging purposes.

View File

@@ -11,12 +11,25 @@ package jokers
*
* @param jokerType the type of this Joker as a String
*/
class Joker(val jokerType: String) {
class Joker(var jokerType: String) {
/**
* Returns a string representation of the Joker.
* Returns the current type of the Joker.
*
* @return the joker type
* @return the joker type as a String
*/
def getJokerType: String = {
jokerType
}
/**
* Updates the type of the Joker.
*
* @param nameJoker the new joker type as a String
*/
def setJokerType(nameJoker: String): Unit = {
this.jokerType = nameJoker
}
override def toString: String = jokerType
}
}

View File

@@ -6,13 +6,44 @@ package score
* @param chips the number of chips a player has
* @param multiplier a factor that multiplies the chips
*/
class Score(var chips:Int, var multiplier: Int) {
class Score(private var chips: Int, private var multiplier: Int) {
/**
* Calculates the total score based on the chips and multiplier.
* Gets the number of chips.
*
* @return the result of chips * multiplier
* @return the chip count
*/
def total:Int =chips *multiplier
def getChips: Int = chips
/**
* Sets the number of chips.
*
* @param value the new chip count
*/
def setChips(value: Int): Unit = {
chips = value
}
/**
* Gets the multiplier value.
*
* @return the current multiplier
*/
def getMultiplier: Int = multiplier
/**
* Sets the multiplier value.
*
* @param value the new multiplier
*/
def setMultiplier(value: Int): Unit = {
multiplier = value
}
/**
* Calculates the total score.
*
* @return chips multiplied by the multiplier
*/
def total: Int = chips * multiplier
}

View File

@@ -16,8 +16,9 @@ class FlushTest extends FunSuite {
)
val combination = Flush()
assert(combination.verify(hand))
assertEquals(combination.score.chips, 35)
assertEquals(combination.score.multiplier, 4)
assertEquals(combination.score.getChips, 35)
assertEquals(combination.score.getMultiplier, 4)
}
test("Flush does not recognize an invalid hand") {

View File

@@ -15,8 +15,9 @@ class FourOfAKindTest extends FunSuite {
)
val combination = FourOfAKind()
assert(combination.verify(hand))
assertEquals(combination.score.chips, 60)
assertEquals(combination.score.multiplier, 7)
assertEquals(combination.score.getChips, 60)
assertEquals(combination.score.getMultiplier, 7)
}
test("Poker does not recognize an invalid hand") {

View File

@@ -15,8 +15,8 @@ class FullHouseTest extends FunSuite {
)
val combination = FullHouse()
assert(combination.verify(hand))
assertEquals(combination.score.chips, 40)
assertEquals(combination.score.multiplier, 4)
assertEquals(combination.score.getChips, 40)
assertEquals(combination.score.getMultiplier, 4)
}
test("Full house does not recognize an invalid hand") {

View File

@@ -15,8 +15,8 @@ class HighCardTest extends FunSuite {
)
val combination = HighCard()
assert(combination.verify(hand)) // No other combination applies
assertEquals(combination.score.chips, 5)
assertEquals(combination.score.multiplier, 1)
assertEquals(combination.score.getChips, 5)
assertEquals(combination.score.getMultiplier, 1)
}
test("HighCard returns false when hand has a pair") {

View File

@@ -15,8 +15,8 @@ class PairTest extends FunSuite {
)
val combination = Pair()
assert(combination.verify(hand))
assertEquals(combination.score.chips, 10)
assertEquals(combination.score.multiplier, 2)
assertEquals(combination.score.getChips, 10)
assertEquals(combination.score.getMultiplier, 2)
}
test("Pair does not recognize an invalid hand") {

View File

@@ -16,8 +16,8 @@ class StraightFlushTest extends FunSuite {
)
val combination = new StraightFlush()
assert(combination.verify(hand))
assertEquals(combination.score.chips, 100)
assertEquals(combination.score.multiplier, 8)
assertEquals(combination.score.getChips, 100)
assertEquals(combination.score.getMultiplier, 8)
}
test("StraightFlush does not recognize an invalid hand") {

View File

@@ -16,8 +16,8 @@ class StraightTest extends FunSuite {
)
val combination = new Straight()
assert(combination.verify(hand))
assertEquals(combination.score.chips, 30)
assertEquals(combination.score.multiplier, 4)
assertEquals(combination.score.getChips, 30)
assertEquals(combination.score.getMultiplier, 4)
}
test("Straight does not recognize an invalid hand") {

View File

@@ -16,8 +16,8 @@ class ThreeOfAKindTest extends FunSuite {
)
val combination = new ThreeOfAKind()
assert(combination.verify(hand))
assertEquals(combination.score.chips, 30)
assertEquals(combination.score.multiplier, 3)
assertEquals(combination.score.getChips, 30)
assertEquals(combination.score.getMultiplier, 3)
}
test("ThreeOfAKind does not recognize an invalid hand") {

View File

@@ -16,8 +16,8 @@ class TwoPairTest extends FunSuite {
)
val combination = new TwoPair()
assert(combination.verify(hand))
assertEquals(combination.score.chips, 20)
assertEquals(combination.score.multiplier, 2)
assertEquals(combination.score.getChips, 20)
assertEquals(combination.score.getMultiplier, 2)
}
test("TwoPair does not recognize an invalid hand") {

View File

@@ -146,4 +146,39 @@ class HandTest extends FunSuite {
assertEquals(playedCards, List(card1, card2), "Should return the two played cards")
assertEquals(hand.getCards, List(card3), "Remaining card should be Ten Diamonds")
}
test("getCards should return the current list of cards") {
val hand = new Hand
hand.addCard(card1)
hand.addCard(card2)
val cards = hand.getCards
assertEquals(cards, List(card1, card2), "getCards should return all added cards in order")
}
test("setCards should replace the current list of cards") {
val hand = new Hand
hand.addCard(card1)
assertEquals(hand.getCards, List(card1), "Initial card should be card1")
hand.setCards(List(card2, card3))
assertEquals(hand.getCards, List(card2, card3), "Cards should be replaced by card2 and card3")
}
test("getJokers should return the current list of jokers") {
val hand = new Hand
hand.addJoker(joker1)
val jokers = hand.getJokers
assertEquals(jokers, List(joker1), "getJokers should return the added joker")
}
test("setJokers should replace the current list of jokers") {
val hand = new Hand
hand.addJoker(joker1)
assertEquals(hand.getJokers, List(joker1), "Initial joker should be joker1")
hand.setJokers(List(joker2))
assertEquals(hand.getJokers, List(joker2), "Jokers should be replaced by joker2")
}
}

View File

@@ -13,5 +13,20 @@ class JokerTest extends FunSuite {
val joker = new Joker("Scary Face")
assertEquals(joker.toString, "Scary Face")
}
}
test("getJokerType should return the current joker type") {
val joker = new Joker("Even Steven")
assertEquals(joker.getJokerType, "Even Steven")
}
test("setJokerType should update the joker type") {
val joker = new Joker("Greedy Joker")
joker.setJokerType("Funny Clown")
assertEquals(joker.getJokerType, "Funny Clown")
}
test("constructor should accept any joker type (even invalid)") {
val joker = new Joker("Unknown Type")
assertEquals(joker.getJokerType, "Unknown Type")
}
}

View File

@@ -9,15 +9,27 @@ class ScoreTest extends FunSuite {
assertEquals(score.total, 20)
}
test("chips can be updated") {
test("getChips should return the current chip value") {
val score = new Score(5, 3)
assertEquals(score.getChips, 5)
}
test("setChips should update the chip value") {
val score = new Score(5, 2)
score.chips = 7
score.setChips(7)
assertEquals(score.getChips, 7)
assertEquals(score.total, 14)
}
test("multiplier can be updated") {
test("getMultiplier should return the current multiplier") {
val score = new Score(4, 3)
assertEquals(score.getMultiplier, 3)
}
test("setMultiplier should update the multiplier value") {
val score = new Score(5, 2)
score.multiplier = 3
assertEquals(score.total, 15)
score.setMultiplier(4)
assertEquals(score.getMultiplier, 4)
assertEquals(score.total, 20)
}
}