Browse Source

Card and Deck class complete

Nick Satinover 6 years ago
parent
commit
fab1cf12f4

+ 93
- 2
src/main/java/io/zipcoder/casino/Card.java View File

@@ -1,13 +1,104 @@
1 1
 package io.zipcoder.casino;
2 2
 
3 3
 public class Card {
4
-    private String suit;
4
+    private Suit suit;
5
+    private CardValue cardValue;
5 6
     private boolean isVisible;
6
-    private int value;
7 7
     private String name;
8 8
 
9
+    public Card (CardValue cardValue, Suit suit)
10
+    {
11
+        this.cardValue = cardValue;
12
+        this.suit = suit;
13
+        this.isVisible = false;
14
+        this.name = cardValue + " of " + suit;
15
+    }
16
+
17
+    public String getName() {
18
+        return name;
19
+    }
20
+
21
+    public void setName(String name) {
22
+        this.name = name;
23
+    }
24
+
25
+    public boolean isVisible() {
26
+        return isVisible;
27
+    }
28
+
29
+    public void setVisible(boolean visible) {
30
+        isVisible = visible;
31
+    }
32
+
9 33
     public void setVisibility(boolean visibility){
10 34
         isVisible = visibility;
11 35
     }
12 36
 
37
+    public Suit getSuit()
38
+    {
39
+        return suit;
40
+    }
41
+
42
+    public void setSuit(Suit suit)
43
+    {
44
+        this.suit = suit;
45
+    }
46
+
47
+    public CardValue getCardValue()
48
+    {
49
+        return cardValue;
50
+    }
51
+
52
+    public void setCardValue(CardValue cardValue)
53
+    {
54
+        this.cardValue = cardValue;
55
+    }
56
+
57
+    public enum CardValue
58
+    {
59
+        TWO(2),
60
+        THREE(3),
61
+        FOUR(4),
62
+        FIVE(5),
63
+        SIX(6),
64
+        SEVEN(7),
65
+        EIGHT(8),
66
+        NINE(9),
67
+        TEN(10),
68
+        JACK(11),
69
+        QUEEN(12),
70
+        KING(13),
71
+        ACE(14);
72
+
73
+        private int cardValue;
74
+
75
+        CardValue (int cardValue)
76
+        {
77
+            this.cardValue = cardValue;
78
+        }
79
+
80
+        public int getCardValue() {
81
+            return cardValue;
82
+        }
83
+    }
84
+
85
+    public enum Suit
86
+    {
87
+        HEARTS("HEARTS"),
88
+        SPADES("SPADES"),
89
+        CLUBS("CLUBS"),
90
+        DIAMONDS("DIAMONDS");
91
+
92
+        private String suitValue;
93
+
94
+        Suit (String suitValue)
95
+        {
96
+            this.suitValue = suitValue;
97
+        }
98
+
99
+        public String getSuitValue(){
100
+            return suitValue;
101
+        }
102
+
103
+    }
13 104
 }

+ 48
- 1
src/main/java/io/zipcoder/casino/Deck.java View File

@@ -4,5 +4,52 @@ import java.util.ArrayList;
4 4
 import java.util.Collections;
5 5
 
6 6
 public class Deck {
7
-    ArrayList<Card> cards = new ArrayList<Card>();
7
+    ArrayList<Card> deck;
8
+
9
+    /**
10
+     * Deck constructor will create an array of all 52 @Card 's
11
+     * Then, they are shuffled
12
+     */
13
+    public Deck(){
14
+        this.deck = new ArrayList<>();
15
+
16
+        for (int i = 0; i < 13; i++){
17
+            Card.CardValue value = Card.CardValue.values()[i];
18
+
19
+            for (int j = 0; j < 4; j++){
20
+                Card card = new Card(value, Card.Suit.values()[j]);
21
+                this.deck.add(card);
22
+            }
23
+        }
24
+
25
+        Collections.shuffle(deck);
26
+    }
27
+
28
+    /**
29
+     * Will retrieve a card at requested index, but will NOT REMOVE
30
+     * Likely just for testing
31
+     * @param index
32
+     * @return
33
+     */
34
+    public Card getCard(int index){
35
+        return this.deck.get(index);
36
+    }
37
+
38
+    /**
39
+     * Will retrieve AND REMOVE card from ZERO INDEX
40
+     * @return
41
+     */
42
+    public Card pullCard(){
43
+        Card tempCard = this.deck.remove(0);
44
+        return tempCard;
45
+    }
46
+
47
+    /**
48
+     * Used to retrieve the array with instantiated deck shuffled
49
+     * Will RETURN DECK
50
+     * @return
51
+     */
52
+    public ArrayList<Card> getDeck() {
53
+        return this.deck;
54
+    }
8 55
 }

+ 20
- 0
src/test/java/io/zipcoder/casino/CardTest.java View File

@@ -0,0 +1,20 @@
1
+package io.zipcoder.casino;
2
+
3
+import org.junit.Test;
4
+
5
+public class CardTest {
6
+
7
+    @Test
8
+    public void createCard(){
9
+        Card card = new Card(Card.CardValue.TWO, Card.Suit.CLUBS);
10
+        System.out.println(card.getName());
11
+    }
12
+
13
+    @Test
14
+    public void createCard2(){
15
+        Card card = new Card(Card.CardValue.TWO, Card.Suit.CLUBS);
16
+        Card card1 = new Card(Card.CardValue.QUEEN, Card.Suit.HEARTS);
17
+        System.out.println(card.getName());
18
+        System.out.println(card1.getName());
19
+    }
20
+}

+ 32
- 0
src/test/java/io/zipcoder/casino/DeckTest.java View File

@@ -0,0 +1,32 @@
1
+package io.zipcoder.casino;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class DeckTest {
7
+
8
+    @Test
9
+    public void testDeck(){
10
+        //GIVEN
11
+        Deck deck = new Deck();
12
+        //WHEN
13
+        for (int i = 0; i < 52; i++){
14
+            System.out.println(deck.getCard(i).getName());
15
+        }
16
+
17
+        //THEN
18
+    }
19
+
20
+    @Test
21
+    public void pullCard(){
22
+        //GIVEN
23
+        Deck deck = new Deck();
24
+        //WHEN
25
+        String expected = deck.getCard(0).getName();
26
+        //THEN
27
+        Card testCard = deck.pullCard();
28
+        String actual = testCard.getName();
29
+
30
+        Assert.assertEquals(expected, actual);
31
+    }
32
+}