/* This class is quite sophisticated for the introductory module, but it is here more to address how could English Playing Card rules can be expressed as a class. */ import java.util.*; class PlayingCard { public enum Value { ACE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13); private int value; private Value(int value) { this.value = value; } public int toInt() { return value; } public boolean lessThan(Value v2) { return value < v2.toInt(); } public boolean lessThanEq(Value v2) { return value <= v2.toInt(); } public boolean greaterThan(Value v2) { return value > v2.toInt(); } public boolean greaterThanEq(Value v2) { return value >= v2.toInt(); } public boolean equalTo(Value v2) { return value == v2.toInt(); } } public enum Suit { SPADE(1), HEART(2), DIAMOND(3), CLUB(4); private int value; private Suit(int value) { this.value = value; } public int toInt() { return value; } public boolean lessThan(Suit v2) { return value > v2.toInt(); } public boolean lessThanEq(Suit v2) { return value >= v2.toInt(); } public boolean greaterThan(Suit v2) { return value < v2.toInt(); } public boolean greaterThanEq(Suit v2) { return value <= v2.toInt(); } public boolean equalTo(Suit v2) { return value == v2.toInt(); } } public enum Color { BLACK, RED } private Value value; private Suit suit; private Color color; public PlayingCard(Value value, Suit suit) { this.value = value; this.suit = suit; // Here we compute color based on suit. Since suit cannot change // by the initial design of the class, this is the only time color // is computed and stored. If the class is changed, the assumption // that a suit can't change may break down and this logic would // need to be addressed elsewhere. if( suit == Suit.HEART || suit == Suit.DIAMOND ) { color = Color.RED; } else { color = Color.BLACK; } } public String toString() { return value + "," + suit + "," + color; } public Value getValue() { return value; } public Suit getSuit() { return suit; } public Color getColor() { return color; } }