title: Java's Card Class example - printing enum values
body: <p>I am using the class card example from java website to try to build a game.</p>
  
  <p><a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html" rel="nofollow noreferrer">http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html</a></p>
  
  <p>I am taking in the number of hands and the number of cards per hands as arguments</p>
  
  <p>like: java -jar a02.jar 4 3</p>
  
  <p>I have added values to the ranks and suits. I am printing the DECK then the HAND...</p>
  
  <p>What i am trying to do is print the deck first
  with their values times together.
  for example: Ten of Hearts</p>
  
  <p>Ten = 10
  Hearts = 4</p>
  
  <p>So, i print out Ten of Hearts(40)</p>
  
  <p>When i print the deck, I am having a problem getting them to print on one line by the number of cards per hand.  like:</p>
  
  <p>eight of spades(8), ten of hearts(40), deuce of hearts(8)</p>
  
  <p>What can i do to fix this?</p>
  
  <p>My other issue is that is there a better way of retrieving the values of the cards than what i am doing? could i use some type of compare?</p>
  
  <p>because when i print out the hand after the deck i am having a hard time getting the values and the hand right ( so i commented out the code )</p>
  
  <p>I also commented the part of code in main() where i am retrieving the values.
  how could i do this differently?</p>
  
  <p>Keep in mind i want the values printed also with the Hand like i am doing with deck</p>
  
  <p>I will shorted the output..</p>
  
  <p>here is the output:</p>
  
  <pre><code>C:\Java\a02&gt;java -jar a1.jar 3 4
  THREE of SPADES(3),      // i would like this to print out like hand does below
  FIVE of HEARTS(20),
  DEUCE of DIAMONDS(6),
  KING of CLUBS(20),
  DEUCE of HEARTS(8),
  TEN of HEARTS(40),
  JACK of HEARTS(40),
  ACE of HEARTS(44),
  NINE of CLUBS(18),
  EIGHT of SPADES(8),
  QUEEN of SPADES(10),
  KING of HEARTS(40),
  NINE of SPADES(9),
  SEVEN of SPADES(7),
  SEVEN of HEARTS(28),
  EIGHT of HEARTS(32),
  TEN of SPADES(10),
  DEUCE of CLUBS(4),
  EIGHT of DIAMONDS(24),
  
  // here is the hand - which i also want the values
  
  [SIX of CLUBS, SEVEN of DIAMONDS, TEN of DIAMONDS, TEN of CLUBS]
  [SIX of HEARTS, THREE of HEARTS, SIX of SPADES, DEUCE of SPADES]
  [JACK of SPADES, NINE of DIAMONDS, SEVEN of CLUBS, THREE of DIAMONDS]
  
  C:\Java\a02&gt;
  </code></pre>
  
  <p>here is the code:</p>
  
  <pre><code>import java.util.*;
  
  public class Cards{
  
  public enum Rank { DEUCE(2), THREE(3), FOUR(4), FIVE(5), SIX(6),
  SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11);
  
  private int Rankpoints;
  
  
  private Rank(int points)
  {
  this.Rankpoints = points;  
  }
  
  public int getRankpoints() 
  {
  return Rankpoints;
  }
  
  
  }
  
  public enum Suit { CLUBS(2), DIAMONDS(3), HEARTS(4), SPADES(1);
  
  private int Suitpoints;
  
  Suit(int points)
  {
  
  Suitpoints = points;  
  
  }
  
  
  
  public int getSuitpoints()
  {
  return Suitpoints;
  }
  
  } 
  
  
  
  private final Rank rank;
  private final Suit suit;
  
  private Cards(Rank rank, Suit suit) 
  {
  this.rank = rank;
  this.suit = suit;
  }
  
  
  
  public Rank rank() 
  { 
  return this.rank;
  }
  public Suit suit() 
  {
  
  return this.suit;
  
  }
  
  public String toString() 
  { 
  return rank + " of " + suit; 
  }
  
  private static final List&lt;Cards&gt; protoDeck = new ArrayList&lt;Cards&gt;();
  
  // Initialize prototype deck
  static {
  for (Suit suit : Suit.values())
      for (Rank rank : Rank.values())
          protoDeck.add(new Cards(rank, suit));
  
  }
  
  public static ArrayList&lt;Cards&gt; newDeck()
  {
  
  return new ArrayList&lt;Cards&gt;(protoDeck); // Return copy of prototype deck
  }
  }
  </code></pre>
  
  <p>and here is main()</p>
  
  <pre><code>import java.util.*;
  
  public class Deal {
  public static void main(String args[])
  {
      int numHands = Integer.parseInt(args[0]);
      int cardsPerHand = Integer.parseInt(args[1]);
      List&lt;Cards&gt; deck  = Cards.newDeck();
      Collections.shuffle(deck);
      int total = 0;
      // ArrayList&lt;Cards&gt; hand = new ArrayList&lt;Cards&gt;();
  
  
  
    // this is how i am handling the values.. which is not good.
   for (Cards card : deck)
   { 
  
   for(Cards.Suit a : Cards.Suit.values())
   {
  
  
   for(Cards.Rank b : Cards.Rank.values())
   {
      if(card.rank() == b)
       {
  
  
   if(card.suit() == a)  // as you can see this works. theres got to be a better way??
   {
     total = b.getRankpoints();
    // hand.add(card);
     System.out.println(card.rank() + " of " +  card.suit() + "(" + a.getSuitpoints() * total + ")" + ",");
  
  
   }
  
  }
  }
     }
  }
  
  
      for (int i=0; i &lt; numHands; i++)
          System.out.println(deal(deck, cardsPerHand));   // prints the hand
         //deal(deck,cardsPerHand,numHands);
  }
  
  
  // hand
   public static ArrayList&lt;Cards&gt; deal(List&lt;Cards&gt; deck, int n) {
       int deckSize = deck.size();
       List&lt;Cards&gt; handView = deck.subList(deckSize-n, deckSize); 
       ArrayList&lt;Cards&gt; hand = new ArrayList&lt;Cards&gt;(handView);
       handView.clear();
      // Map&lt;String, Map&lt;String, Integer&gt;&gt; inputWords = new HashMap&lt;String, Map&lt;String, Integer&gt;&gt;();
        int total = 0;
  
        return hand;
      }
  
     }
   //for (Cards card : hand)
   //{ 
  
   // for(Cards.Suit a : Cards.Suit.values())
   // {
   //for(Cards.Rank b : Cards.Rank.values())
   // {
    //  if(card.rank() == b)
     //  {
  
    // if(card.suit() == a)
   //{
    // total = b.getRankpoints();
  
  
      // System.out.println("card.rank() + " of " +  card.suit() + "(" + a.getSuitpoints() * total + ")" + ",");
      // return hand;
    //}
  
   //}
    //}
   //}
  
     //}
    //return hand;
     //}
  
     //}
  </code></pre>
  
  <p>Any help is much appreciated. code snippets or hints are very welcome =)
  Keep in mind i am pretty new to Java..</p>
  
  <p>Thank you</p>