title: Hand of cards: Total value (Java)
body: <p>I am currently working on a hand of cards class that uses enums that requires me to add methods that:</p>
  
  <p>Add cards  into the hand</p>
  
  <p>Calculate the total value of all the cards currently in the hand (so if there were 3 10's  in the hand then the total value would be 30)</p>
  
  <p>The problem however is that I can't find out how to add up all the total while still implimenting the method within the hand class (as the task asks us to).</p>
  
  <p>Any help will be greatly appreciated</p>
  
  <p>Heres the code for my Suit enum</p>
  
  <pre><code>public enum Suit 
  {    HEARTS, CLUBS, DIAMONDS, SPADES; }
  </code></pre>
  
  <p>Here is the code for my Rank enum.This one  has a few added methods including a getValue method</p>
  
  <pre><code>public enum Rank {   
      ONE(1),TWO(2),THREE(3),FOUR(4),FIVE(5), SIX (6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), KING(10), QUEEN(10),
      JACK(10);
  
      private final int cardValue;
  
      private Rank ( int nDays) {
          cardValue = nDays;
      }
  
      public int getValue() {
          return cardValue;
      }
  }
  </code></pre>
  
  <p>This is the Card class that uses the Rank and Suit enums to print off the details (excuse the mess)</p>
  
  <pre><code> public class Card {
       private Rank rank;
       private Suit suit;
  
       public Card (Rank theRank) {       
          this(theRank,Suit.HEARTS);
       }
  
       public Card (Rank theRank, Suit theSuit) {
          rank = theRank;         
          suit = theSuit;
       }
  
       public Rank getRank( )      { return rank; }
       public Suit getSuit ( )  { return suit; }
  
       public String toString ( ) { return ( rank + " of " + suit +"\n Value of card = " + rank.getValue() ); }
  
    }
  </code></pre>
  
  <p>And this is the Hand class that requires the addCard method and the totalValue method. I have done the addCard method and now need to do totalValue method)</p>
  
  <pre><code> public class Hand {
   private Card theCards[ ];
   private Rank rank;
   private int numCards;    private int totalValue;
   private static final int max = 5;
  
   public Hand ( )
   {
    theCards = new Card [max];
    numCards = 0;
   }
  
   public void addCard( Card aCard )
   {
        if (numCards &lt; max)
                theCards[numCards++] = aCard;
        else
        {
            System.out.println("Cannot add any more cards as hand is full");
        }
   }
  
   public void totalValue ()
       {
       int handValue = 0;
  
  
           for(Card C: theCards)
           {
               //get the rank values to appear then print them off
               //Add up all values of cards and display the total
          totalValue = handValue + C.getValue(); //This was just a test to see if calling up the value of card C would work
  
           }
           System.out.println("The total value of all the cards is: " + totalValue);
   }
  
  
   public String toString ( )
   {
      String s = "";
      for (int i = 0; i &lt; numCards; ++i)  {       s += "\n" + theCards[i];
      }
      return s;
                            }      
  </code></pre>
  
  <p>As far as implimentation goes its just a case of creating card objects and adding them to the Hand object so I don't think I'll need to post that here. Thank you to anyone who can help me with my problem.</p>