Hi, I have to write a program that plays out a whole game of the card game WAR. I have to create a bunch of classes that go along with some pre written classes so right now im trying to start out small and work my way up. We are given two enum classes of Rank and Suit which look like...

public enum Suit {

    /**
     * The suit CLUBS
     */
    CLUBS('c', "Clubs"),

    /**
     * The suit DIAMONDS
     */
    DIAMONDS('d', "Diamonds"),

    /**
     * The suit HEARTS
     */
    HEARTS('h', "Hearts"),

    /**
     * The suit SPADES
     */
    SPADES('s', "Spades");


public enum Rank {

    /**
     * The rank ACE
     */
    ACE('a', "Ace"),

    /**
     * The rank TWO (2)
     */
    TWO('2', "Two"),

    /**
     * The rank THREE (3)
     */

etc...

So far this is my Card class along with a main inserted just to test and see if my outputs are what I exspect.

    public class Card{

        private Rank rank;
        private Suit suit;
        //constructor for a single card: rank and suit
        Card( Rank rank, Suit suit){
            this.suit = suit;
            this.rank = rank;
        }
        public Rank getRank(){
            return rank;

        }

        public Suit getSuit(){
            return suit;
        }

        public String toString(){
            String temp = "";
            temp += rank;
            temp += " of ";
            temp += suit;
            return temp;

        }

        public static void main(String[] args){

            Card card1 = new Card(Rank.FOUR, Suit.CLUBS); 
            System.out.println(card1);
        }


    }

So far my output in this particular case looks like FOUR of CLUBS. My question is how do I proporly use enum values contained in completely separate classes in my current card class to get the output I want such as four of Clubs useing the values of the enum Suit and Rank.

Dani AI

Generated

As suggested, the reason you saw "FOUR of CLUBS" is that printing an enum uses its name() by default (the declared constant), which is all-caps. Two simple, robust fixes:

  • Give each enum a display string (and, for game logic, a numeric value for Rank) and expose it via a getter.
  • Or override each enum's toString() to return the display string (but be aware this changes every implicit print of the enum).

Example enum pattern (keeps a display name and a numeric rank for comparisons):

public enum Rank {
    FOUR('4', "Four", 4),
    ACE('a', "Ace", 14);

    private final char code;
    private final String display;
    private final int value;

    Rank(char code, String display, int value) {
        this.code = code;
        this.display = display;
        this.value = value;
    }

    public String getDisplayName() { return display; }
    public int getValue() { return value; }

    @Override
    public String toString() { return display; }
}

And a simple Suit pattern:

public enum Suit {
    CLUBS('c', "Clubs"),
    DIAMONDS('d', "Diamonds");

    private final char code;
    private final String display;

    Suit(char code, String display) {
        this.code = code;
        this.display = display;
    }

    public String getDisplayName() { return display; }
    @Override
    public String toString() { return display; }
}

Use the getters when formatting or comparing:

String pretty = card.getRank().getDisplayName().toLowerCase() + " of " + card.getSuit().getDisplayName();
int cmp = Integer.compare(card1.getRank().getValue(), card2.getRank().getValue());

Notes: prefer getters if you want to avoid changing global printing behavior; for WAR decide whether Ace is high (value 14) or low; build the deck with nested loops over Rank.values() and Suit.values() and shuffle with Collections.shuffle. Glad to see got it working.

Recommended Answers

All 2 Replies

Your enums store the text you need (eg "Ace", "Spades"), so all you need is an accessor method in the enums to return that text (or simply override toString in the enums to return it)

I got it working now, thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.