How to print to console a List of objects that contain fields of Enum type?

0 Deniss Diks · June 1, 2015
We have the class Card:
public class Card {

   private Rank rank;  //ENUM TYPE
   private Suit suit;  //ENUM TYPE

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

   public Rank getRank() {
       return rank;
   }


   public Suit getSuit() {
       return suit;
   }

   @Override
   public String toString() {
       return rank + " " + suit;
   }
}


Enum Rank:
public enum Rank {
   SIX,
   SEVEN,
   EIGHT,
   NINE,
   TEN,
   JACK,
   QUEEN,
   KING,
   ACE;
}


Enum Suit:
public enum Suit {
   SPADES,
   HEARTS,
   DIAMONDS,
   CLUBS;
}


And finally the Deck class:
public class Deck {

   private List deck;

   public Deck(){

       deck = new ArrayList();

       for (Suit suit: Suit.values()){
           for (Rank rank: Rank.values()){
               Card card = new Card(suit, rank);
               deck.add(card);
           }
       }
   }
}

The question is how to make a method that will print to console the deck List?

Post a Reply

Replies

Oldest  Newest  Rating
+1 Will Moffat · June 3, 2015
I hope I'm reading this correctly.

What I would do is I would add a method to the Deck class something like: 

public void printDeck() {
   for(Card c : this.deck) {
    System.out.println(c.toString());
    }
}

Sorry if I misinterpreted what you're asking.
0 Deniss Diks · June 3, 2015
Thanks a lot, it works :D! Sometimes I just think too much - it's easy way to get confused.
0 Will Moffat · June 3, 2015
Haha no problem
  • 1

Java / Android Development

107,215 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator