DB204
DB204

Reputation: 23

Getting an enum's index from an object? (Java)

I have an object Card that is created using two enums, those being Suit and Rank. These enums read as such (apologies for formatting, not quite sure how to get it right):

public enum Suit
{
    HEARTS(),
    DIAMONDS(),
    SPADES(),
    CLUBS();
}

public enum Rank
{
    TWO(),
    THREE(),
    FOUR(),
    FIVE(),
    SIX(),
    SEVEN(),
    EIGHT(),
    NINE(),
    TEN(),
    JACK(),
    QUEEN(),
    KING(),
    ACE();
}

The Card constructor is as follows:

public Card(Suit s, Rank r) //s and r are fields in class Card
    {
        suit = s;
        rank = r;
    }

So let's say a Card object is made with Suit DIAMONDS and Rank NINE, which I know to be indexes 1 and 7, respectively.

A method called Hand is dealing with an ArrayList of 5 Card objects and is going to sort them by value to make them easier to arrange in a certain manner. Is it possible to obtain these indexes from the Card object for this purpose, or is there some other way to do it?

Added note, this is for an assignment, so I am under constraint of needing to use the enums and ArrayLists.

Upvotes: 2

Views: 235

Answers (2)

Dropout
Dropout

Reputation: 13866

A better solution would be to define values for the particular elements in the enumeration

public enum CardSuits {

    HEARTS(1),
    CLUBS(2),
    DIAMONDS(3),
    SPADES(4);

    public final int rank;

    private CardSuits(int rank){
        this.rank = rank;
    }

    public int getRank() {
        return rank;
    }
}

You can then call CardSuits.SPADES.getRank() in order to get it's value when needed. This way the values are completely defined by you precisely and there will be no confusion if someone swaps the order.

Upvotes: 2

Rohan Kumar
Rohan Kumar

Reputation: 5882

You can create a member field called index in your Suit and Rank enums like this:

public enum Suit {
    HEARTS(0),
    DIAMONDS(1),
    SPADES(2),
    CLUBS(3);

    private final int index;

    Suit(int i) {
        this.index = i;
    }

    public int getIndex() { return this.index; }
}

public enum Rank {
    TWO(2),
    THREE(3),
    FOUR(4),
    FIVE(5),
    SIX(6),
    SEVEN(7),
    EIGHT(8),
    NINE(9),
    TEN(10),
    JACK(11),
    QUEEN(12),
    KING(13),
    ACE(1);

    private final int index;

    Rank(int i) {
        this.index = i;
    }

    public int getIndex() { return this.index; }
}

You can then use it your Card class like this:

public class Card {
    private final Suit suit;
    private final Rank rank;

    public Card(Suit s, Rank r) {
        this.suit = s;
        this.rank = r;
    }

    public Suit getSuit() { return this.suit; }

    public Rank getRank() { return this.rank; }

    public static void main(String[] args) {
        Card card = new Card(Suit.DIAMONDS, Rank.NINE);

        System.out.println(card.getSuit().getIndex()); // Prints 1
        System.out.println(card.getRank().getIndex()); // Prints 9
    }
}

Upvotes: 1

Related Questions