home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / GuessGameApple.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-07-12  |  2.5 KB  |  45 lines

  1. package org.apache.cocoon.components.flow.apples.samples;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Random;
  6. import org.apache.avalon.framework.logger.AbstractLogEnabled;
  7. import org.apache.cocoon.ProcessingException;
  8. import org.apache.cocoon.components.flow.apples.AppleController;
  9. import org.apache.cocoon.components.flow.apples.AppleRequest;
  10. import org.apache.cocoon.components.flow.apples.AppleResponse;
  11.  
  12. public class GuessGameApple extends AbstractLogEnabled implements AppleController {
  13.    private final int random = (new Random()).nextInt(10) + 1;
  14.    private int guesses = 0;
  15.  
  16.    public String toString() {
  17.       return "GuessGameApple[ random=" + this.random + " | guesses=" + this.guesses + "]";
  18.    }
  19.  
  20.    public void process(AppleRequest req, AppleResponse res) throws ProcessingException {
  21.       String hint = "No hints yet.";
  22.       String targetURI = "guess/guess.jx";
  23.       int newGuess = -1;
  24.       String newGuessString = req.getCocoonRequest().getParameter("guess");
  25.       if (newGuessString != null) {
  26.          newGuess = Integer.parseInt(newGuessString);
  27.          ++this.guesses;
  28.          if (this.random == newGuess) {
  29.             targetURI = "guess/success.jx";
  30.          } else if (this.random < newGuess) {
  31.             hint = "Try lower.";
  32.          } else {
  33.             hint = "Try higher.";
  34.          }
  35.       }
  36.  
  37.       this.getLogger().debug(this.toString());
  38.       Map bizdata = new HashMap();
  39.       bizdata.put("random", "" + this.random);
  40.       bizdata.put("guesses", "" + this.guesses);
  41.       bizdata.put("hint", hint);
  42.       res.sendPage(targetURI, bizdata);
  43.    }
  44. }
  45.