Created
March 24, 2014 12:22
-
-
Save edwardbeckett/9739126 to your computer and use it in GitHub Desktop.
Enum Iteration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Card { | |
public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, | |
SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE } | |
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES } | |
private final Rank rank; | |
private final Suit suit; | |
private Card(Rank rank, Suit suit) { | |
this.rank = rank; | |
this.suit = suit; | |
} | |
public Rank rank() { return rank; } | |
public Suit suit() { return suit; } | |
public String toString() { return rank + " of " + suit; } | |
private static final List<Card> protoDeck = new ArrayList<Card>(); | |
// Initialize prototype deck | |
static { | |
for (Suit suit : Suit.values()) | |
for (Rank rank : Rank.values()) | |
protoDeck.add(new Card(rank, suit)); | |
} | |
public static ArrayList<Card> newDeck() { | |
return new ArrayList<Card>(protoDeck); // Return copy of prototype deck | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment