1 16 package org.apache.cocoon.components.flow.apples.samples; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 import java.util.Random ; 21 22 import org.apache.avalon.framework.logger.AbstractLogEnabled; 23 import org.apache.cocoon.ProcessingException; 24 import org.apache.cocoon.components.flow.apples.AppleController; 25 import org.apache.cocoon.components.flow.apples.AppleRequest; 26 import org.apache.cocoon.components.flow.apples.AppleResponse; 27 28 31 public class GuessGameApple extends AbstractLogEnabled implements AppleController { 32 33 private final int random = (new Random ()).nextInt(10) + 1; 34 private int guesses = 0; 35 36 public String toString() { 37 return "GuessGameApple[ random=" + this.random + " | guesses=" + this.guesses + "]"; 38 } 39 40 public void process(AppleRequest req, AppleResponse res) throws ProcessingException { 41 42 String hint = "No hints yet."; 43 String targetURI = "guess/guess.jx"; 44 45 int newGuess = -1; 46 String newGuessString = req.getCocoonRequest().getParameter("guess"); 47 48 if (newGuessString != null) { 49 newGuess = Integer.parseInt(newGuessString); 50 this.guesses++; 51 52 if (this.random == newGuess) { 53 targetURI = "guess/success.jx"; 54 } else { 55 if (this.random < newGuess) { 56 hint = "Try lower."; 57 } else { 58 hint = "Try higher."; 59 } 60 } 61 } 62 63 getLogger().debug(toString()); 64 65 Map bizdata = new HashMap (); 66 bizdata.put("random" , "" + this.random); 67 bizdata.put("guesses", "" + this.guesses); 68 bizdata.put("hint" , hint); 69 70 res.sendPage(targetURI, bizdata); 71 } 72 73 } 74 | Popular Tags |