Skip to content

Commit a9bda5e

Browse files
authored
feat: adds Danielson's Feature/lesson16/objects (#583)
* feat: added tests * feat: initial deck code; untested * feat: all test but shuffle passing * feat: all tests passing * fix: spotlessApply * fix: warning resolved(i think) * feat: added DeckException * fix: danielsonobject made
1 parent 3bdc813 commit a9bda5e

File tree

7 files changed

+332
-0
lines changed

7 files changed

+332
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.codedifferently.lesson16.danielsonobject;
2+
3+
import java.util.Objects;
4+
import lombok.Getter;
5+
6+
@Getter
7+
public class Card {
8+
private final Suit suit;
9+
private final int rank;
10+
11+
public Card(Suit suit, int rank) {
12+
if (rank < 1 || rank > 15) {
13+
throw new IllegalArgumentException("Rank must be between 1 and 15");
14+
}
15+
this.suit = suit;
16+
this.rank = rank;
17+
}
18+
19+
public Suit getSuit() {
20+
return suit;
21+
}
22+
23+
public int getRank() {
24+
return rank;
25+
}
26+
27+
public Card compareTo(Card other) {
28+
if (this.rank > other.rank) {
29+
return this;
30+
} else if (this.rank < other.rank) {
31+
return other;
32+
} else {
33+
return null; // They are equal
34+
}
35+
}
36+
37+
@Override
38+
public String toString() {
39+
if (suit == Suit.NONE) {
40+
return "Joker";
41+
} else if (2 <= rank && rank <= 10) {
42+
return rank + " of " + suit.toString();
43+
} else {
44+
String face;
45+
switch (this.rank) {
46+
case 11 -> face = "Jack";
47+
case 12 -> face = "Queen";
48+
case 13 -> face = "King";
49+
default -> face = "Ace";
50+
}
51+
return face + " of " + suit.toString();
52+
}
53+
}
54+
55+
@Override
56+
public boolean equals(Object o) {
57+
if (this == o) return true;
58+
if (!(o instanceof Card card)) return false;
59+
return rank == card.rank && suit == card.suit;
60+
}
61+
62+
@Override
63+
public int hashCode() {
64+
return Objects.hash(suit, rank);
65+
}
66+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.codedifferently.lesson16.danielsonobject;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Random;
6+
7+
public class Deck {
8+
private final Random rand = new Random();
9+
private final String brand;
10+
private final ArrayList<Card> cards;
11+
private int max_size = 54;
12+
13+
public Deck(String brand) {
14+
this.brand = brand;
15+
this.cards = new ArrayList<>();
16+
for (Suit suit : Suit.values()) {
17+
if (suit == Suit.NONE) {
18+
cards.add(new Card(Suit.NONE, 14)); // This will represent the Jokers.
19+
cards.add(new Card(Suit.NONE, 15));
20+
} else {
21+
for (int rank = 1; rank <= 13; rank++) {
22+
cards.add(new Card(suit, rank));
23+
}
24+
}
25+
}
26+
}
27+
28+
public String getBrand() {
29+
return brand;
30+
}
31+
32+
public void shuffle() {
33+
Collections.shuffle(cards);
34+
}
35+
36+
public Card draw() {
37+
if (cards.isEmpty()) {
38+
throw new DeckException("There are no Cards to draw");
39+
}
40+
return cards.removeFirst();
41+
}
42+
43+
public int getSize() {
44+
return cards.size();
45+
}
46+
47+
public ArrayList<Card> getCards() {
48+
return cards;
49+
}
50+
51+
public void addToDeck(Card card) throws DeckException {
52+
if (cards.size() + 1 > max_size) {
53+
throw new DeckException("Deck limit reached");
54+
}
55+
cards.addFirst(card);
56+
}
57+
58+
public void shuffleIntoDeck(Card card) throws DeckException {
59+
if (cards.size() + 1 > max_size) {
60+
throw new DeckException("Deck limit reached");
61+
}
62+
int insert = rand.nextInt(0, max_size);
63+
64+
cards.add(insert, card);
65+
}
66+
67+
public void removeJokers() throws JokerException {
68+
if (max_size == 52) {
69+
throw new JokerException("There are already no Jokers");
70+
}
71+
cards.removeIf(card -> card.getSuit() == Suit.NONE);
72+
max_size = 52;
73+
}
74+
75+
public void addJokers() throws JokerException {
76+
if (max_size == 52) {
77+
max_size = 54;
78+
shuffleIntoDeck(new Card(Suit.NONE, 14));
79+
shuffleIntoDeck(new Card(Suit.NONE, 15));
80+
} else if (max_size == 54) {
81+
throw new JokerException("Jokers are already accounted for");
82+
}
83+
}
84+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.danielsonobject;
2+
3+
public class DeckException extends IllegalStateException {
4+
DeckException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.danielsonobject;
2+
3+
public class JokerException extends Exception {
4+
public JokerException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.codedifferently.lesson16.danielsonobject;
2+
3+
public enum Suit {
4+
NONE,
5+
HEARTS,
6+
DIAMONDS,
7+
CLUBS,
8+
SPADES
9+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.codedifferently.lesson16.danielsonobject;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class CardTest {
9+
// Test cases for Card class
10+
@Test
11+
public void testCardCreation() {
12+
Card card = new Card(Suit.HEARTS, 10);
13+
assertThat(card.getSuit()).isEqualTo(Suit.HEARTS);
14+
assertThat(card.getRank()).isEqualTo(10);
15+
16+
card = new Card(Suit.SPADES, 3);
17+
assertThat(card.getSuit()).isEqualTo(Suit.SPADES);
18+
assertThat(card.getRank()).isEqualTo(3);
19+
20+
assertThatThrownBy(() -> new Card(Suit.HEARTS, 0))
21+
.isInstanceOf(IllegalArgumentException.class)
22+
.hasMessage("Rank must be between 1 and 15");
23+
}
24+
25+
@Test
26+
public void JokerCreation() {
27+
Card card = new Card(Suit.NONE, 14);
28+
assertThat(card.getSuit()).isEqualTo(Suit.NONE);
29+
assertThat(card.getRank()).isEqualTo(14);
30+
}
31+
32+
@Test
33+
public void testCompareTo() {
34+
Card card1 = new Card(Suit.HEARTS, 10);
35+
Card card2 = new Card(Suit.SPADES, 3);
36+
Card card3 = new Card(Suit.DIAMONDS, 10);
37+
Card card4 = new Card(Suit.NONE, 14); // Joker
38+
39+
assertThat(card1.compareTo(card2)).isEqualTo(card1); // 10 > 3
40+
assertThat(card2.compareTo(card1)).isEqualTo(card1); // 3 < 10
41+
assertThat(card1.compareTo(card4)).isEqualTo(card4); // 10 < Joker
42+
assertThat(card4.compareTo(card2)).isEqualTo(card4); // Joker >
43+
assertThat(card1.compareTo(card3)).isNull(); // 10
44+
}
45+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.codedifferently.lesson16.danielsonobject;
2+
3+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
4+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
7+
import static org.junit.jupiter.api.Assertions.fail;
8+
9+
import java.util.ArrayList;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
13+
public class DeckTest {
14+
Deck deck;
15+
16+
@BeforeEach
17+
public void setUp() {
18+
deck = new Deck("Bicycle");
19+
}
20+
21+
@Test
22+
public void Deckexists() {
23+
Deck deck = new Deck("Bicycle");
24+
assertThat(deck).isNotNull();
25+
}
26+
27+
@Test
28+
public void getCards() {
29+
ArrayList<Card> cards = deck.getCards();
30+
assertThat(cards).isNotNull();
31+
}
32+
33+
@Test
34+
public void removeJokerTest() throws JokerException {
35+
deck.removeJokers();
36+
assertEquals(deck.getSize(), 52);
37+
for (Card card : deck.getCards()) {
38+
if (card.getRank() == 14) {
39+
fail("There is still a Joker in the deck");
40+
}
41+
}
42+
}
43+
44+
@Test
45+
public void addJokerTest() throws JokerException {
46+
assertThatThrownBy(() -> deck.addJokers())
47+
.isInstanceOf(JokerException.class)
48+
.hasMessage("Jokers are already accounted for");
49+
50+
deck.removeJokers();
51+
deck.addJokers();
52+
assertEquals(deck.getSize(), 54);
53+
int count = 0;
54+
for (Card card : deck.getCards()) {
55+
if (card.getSuit() == Suit.NONE) {
56+
count++;
57+
}
58+
}
59+
assertEquals(2, count);
60+
}
61+
62+
@Test
63+
public void brandTest() {
64+
assertEquals("Bicycle", deck.getBrand());
65+
assertEquals("Copag", new Deck("Copag").getBrand());
66+
}
67+
68+
@Test
69+
public void drawTest() {
70+
Card card1 = deck.draw();
71+
Card card2 = deck.draw();
72+
73+
assertNotEquals(card1, card2);
74+
75+
assertEquals(52, deck.getSize());
76+
}
77+
78+
@Test
79+
public void sizeTest() {
80+
assertEquals(deck.getSize(), 54);
81+
}
82+
83+
@Test
84+
public void addToDeckTest() {
85+
int initialSize = deck.getSize();
86+
Card card1 = deck.draw();
87+
deck.addToDeck(card1);
88+
assertEquals(card1, deck.getCards().getFirst());
89+
assertEquals(initialSize, deck.getSize());
90+
}
91+
92+
@Test
93+
public void DeckExceptionTest() {
94+
assertThatThrownBy(() -> deck.addToDeck(new Card(Suit.HEARTS, 1)))
95+
.isInstanceOf(DeckException.class)
96+
.hasMessage("Deck limit reached");
97+
98+
for (int x = 0; x < 54; x++) {
99+
deck.draw();
100+
}
101+
assertThatThrownBy(() -> deck.draw())
102+
.isInstanceOf(DeckException.class)
103+
.hasMessage("There are no Cards to draw");
104+
}
105+
106+
@Test
107+
public void shuffleTest() {
108+
Deck copy = new Deck("Bicyle");
109+
assertEquals(deck.getCards(), copy.getCards());
110+
deck.shuffle();
111+
assertNotEquals(deck.getCards(), copy.getCards());
112+
assertEquals(deck.getSize(), copy.getSize());
113+
}
114+
}

0 commit comments

Comments
 (0)