KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > csdl > stackmvc > control > command > DoubleCommand


1 package csdl.stackmvc.control.command;
2
3 import csdl.stackmvc.control.Page;
4 import csdl.stackmvc.model.StackModel;
5 import javax.servlet.http.HttpServletRequest JavaDoc;
6 import edu.hawaii.stack.EmptyStackException;
7 import csdl.stackmvc.util.Debug;
8
9 /**
10  * Implements the pop command by popping off the top value from the stack.
11  * @author Jitender Miglani
12  * @author Philip Johnson
13  */

14 public class DoubleCommand implements Command {
15
16   /**
17    * Processes the "Double" command sent by the user.
18    * If an empty stack is popped, then the ErrorMessage attribute is set.
19    * Sets the stackArray attribute with the resulting stack contents.
20    *
21    * @param request The request object.
22    * @return The page to be displayed (Page.INDEX).
23    */

24   public Page process(HttpServletRequest JavaDoc request) {
25     Debug.println(Debug.STACKMVC, "Processing double.");
26     StackModel stackModel = StackModel.getInstance();
27     String JavaDoc top;
28     try {
29       stackModel.doubles();
30       top = stackModel.top();
31     }
32     catch (EmptyStackException e) {
33       request.setAttribute("errorMessage", "Attempt to double empty stack.");
34       top = "The stack is empty.";
35     }
36     Object JavaDoc[] stackArray = stackModel.toArray();
37     request.setAttribute("stackArray", stackArray);
38     request.setAttribute("top", top);
39     return Page.INDEX;
40   }
41 }
42
Popular Tags