enum
Class Suit

java.lang.Object
  extended byenum.Suit
All Implemented Interfaces:
java.lang.Comparable

public final class Suit
extends java.lang.Object
implements java.lang.Comparable

Simple class demonstrating how to create an ordinal based typesafe enum based around the suits in a deck of cards. There are four possible values in the enum's range:
HEARTS
CLUBS
SPADES
DIAMONDS

The big idea here is that you use this algorithm in place of using constant declarations.
For examples use this:

Suit mySuit = getSuit();
if(mySuit == Suit.DIAMONDS) {
// Do Something
}

and DON'T use
// Some Where in your code...
private static final String DIAMONDS = "diamonds";
private static final String HEARTS = "hearts";
private static final String CLUBS = "clubs";
private static final String SPADES = "spades";

:
:
String mySuit = getSuit();
if(mySuit.equals(DIAMONDS)) {
// Do something...
}


Note the use of the private constructor limiting you to the four suit types. you can't do:

Suit mySuit = new Suit("Wobbles");


... and the use of the final keyword so it's no to:

public class mySuit extends Suit {


Field Summary
static Suit CLUBS
          Four values for Suit are created at construction time and allocated to public member vars.
static Suit DIAMONDS
          Four values for Suit are created at construction time and allocated to public member vars.
static Suit HEARTS
          Four values for Suit are created at construction time and allocated to public member vars.
private  java.lang.String name
          Somewhere to hold the name of the suit - "diamonds", "clubs" etc.
private static int nextOrdinal
          Ordinal of the next suit to be created
private  int ordinal
          Assign the next ordinal to this suit
static Suit SPADES
          Four values for Suit are created at construction time and allocated to public member vars.
 
Constructor Summary
private Suit(java.lang.String name)
          Constructor - Pass in the name of the Suit.
 
Method Summary
 int compareTo(java.lang.Object o)
          Compare the input parameter to this Suit class.
static void main(java.lang.String[] args)
          main Holds sample code that could be used in any client of an enum class The whole point of this is that you can used a == symbol to compare objects.
 java.lang.String toString()
          Override toString() returning the Suit's name
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

name

private final java.lang.String name
Somewhere to hold the name of the suit - "diamonds", "clubs" etc.


nextOrdinal

private static int nextOrdinal
Ordinal of the next suit to be created


ordinal

private final int ordinal
Assign the next ordinal to this suit


CLUBS

public static final Suit CLUBS
Four values for Suit are created at construction time and allocated to public member vars. Possible values are clubs, hearts, spades and diamonds.


HEARTS

public static final Suit HEARTS
Four values for Suit are created at construction time and allocated to public member vars. Possible values are clubs, hearts, spades and diamonds.


SPADES

public static final Suit SPADES
Four values for Suit are created at construction time and allocated to public member vars. Possible values are clubs, hearts, spades and diamonds.


DIAMONDS

public static final Suit DIAMONDS
Four values for Suit are created at construction time and allocated to public member vars. Possible values are clubs, hearts, spades and diamonds.

Constructor Detail

Suit

private Suit(java.lang.String name)
Constructor - Pass in the name of the Suit. I guess that a more complex app should test the inputs for validity

Method Detail

toString

public java.lang.String toString()
Override toString() returning the Suit's name

Returns:
the name of the suit.

compareTo

public int compareTo(java.lang.Object o)
Compare the input parameter to this Suit class. Return the difference.

Specified by:
compareTo in interface java.lang.Comparable
Returns:
int the difference between the two suits, zero if the same

main

public static void main(java.lang.String[] args)
main Holds sample code that could be used in any client of an enum class The whole point of this is that you can used a == symbol to compare objects. Of course, this example is very Mickey Mouse.