title: Keep track of games played JSF
body: <p>I was wondering if anyone can help me?</p>
  
  <p>I am creating a simple game using JSF. I have managed to complete the main functionality but I would like to tell the user how many games they have played. </p>
  
  <p>For some reason, the code I have written for it does not work. </p>
  
  <p>Bean:</p>
  
  <pre><code>import java.util.Random;
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.SessionScoped;
  
  @ManagedBean
  @SessionScoped
  public class GameBeans {
      private int randomNumber;
      private int userGuess;
      private int gamesPlayed;
  
      public String getWin() {
           if(this.userGuess == this.randomNumber)
          {
  
              return "Congratulations! You've Won!";
          }
          else
          {
              return "You Lose!";
          }        
      } 
      /**
       * 
       * @return randomNumber
       */
      public int getRandomNumber() {
          return randomNumber;
      }
  
      /**
       * sets the generated random number
       * @param randomNumber 
       */
      private void setRandomNumber(int randomNumber) {
          this.randomNumber = randomNumber;
      }
  
      /**
       * 
       * @return the guess of the user
       */
      public int getUserGuess() {
          return userGuess;
      }
  
      /**
       * Sets the guess of the user into userGuess
       * @param userGuess 
       */
      public void setUserGuess(int userGuess) {
          this.userGuess = userGuess;
  
      }
      /**
       * 
       * @return number of games played by the user
       */
  
  
      public int getGamesPlayed() 
      {
          return gamesPlayed;
      }
  
      private void setGamesPlayed(int played)
      {
          this.gamesPlayed=played;
      }
  
      /**
       * Creates a new instance of GameBeans
       * Generates a new random number
       * 
       * Compares random number to user's
       * choice
       * 
       * Keeps total of games played
       */
     public GameBeans() {
          Random number = new Random();
          int rNumber = number.nextInt(1000);
          setRandomNumber(rNumber);
          int played = this.gamesPlayed++;
          setGamesPlayed(played);
      }
  
  }
  </code></pre>
  
  <p>First page (play_game.xhtml):</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://xmlns.jcp.org/jsf/html"&gt;
      &lt;h:head&gt;
          &lt;title&gt;Guess Numbers Page&lt;/title&gt;
      &lt;/h:head&gt;
      &lt;h:body&gt;
          &lt;h:form&gt;
              &lt;h1&gt;Welcome to Your Game Session&lt;/h1&gt;
              &lt;p&gt;Number of games played this session: #{gameBeans.gamesPlayed}&lt;/p&gt;
              &lt;p&gt;Enter your lucky number guess and then click play&lt;/p&gt;
              &lt;p&gt;Your guess: &lt;h:inputText id="iptxt1" value="#{gameBeans.userGuess}" /&gt;&lt;/p&gt;
              &lt;h:commandButton id="cmdBtn1" value="Play" action="game_result" /&gt;            
          &lt;/h:form&gt;
      &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>game_result.xhtml:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://xmlns.jcp.org/jsf/html"&gt;
      &lt;h:head&gt;
          &lt;title&gt;Game Results&lt;/title&gt;
      &lt;/h:head&gt;
      &lt;h:body&gt;
          &lt;h:form&gt;
              &lt;p&gt;Your Guess: &lt;h:outputText id="outText1" value="#{gameBeans.userGuess}"&gt;&lt;/h:outputText&gt;&lt;/p&gt;
              &lt;p&gt;Random Number: &lt;h:outputText id="outText2" value="#{gameBeans.randomNumber}"&gt;&lt;/h:outputText&gt;&lt;/p&gt;
              &lt;p&gt;&lt;h:outputText id="outText4" value="#{gameBeans.win}"&gt;&lt;/h:outputText&gt;&lt;/p&gt;
              &lt;p&gt;Number of Games Played: #{gameBeans.gamesPlayed}&lt;/p&gt;
              &lt;h:commandButton id="cmdBtn1" value="Play Again" action="play_game" /&gt;     
          &lt;/h:form&gt;
      &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I would like to allow the user to play again even if they win or lose, the count (game played) should be kept track of. This is not working currently!</p>
  
  <p>Can anyone help please??</p>
  
  <p>Thanks</p>