01: package csdl.stackmvc.control.command;
02:
03: import csdl.stackmvc.control.Page;
04: import csdl.stackmvc.model.StackModel;
05: import javax.servlet.http.HttpServletRequest;
06: import edu.hawaii.stack.EmptyStackException;
07: import csdl.stackmvc.util.Debug;
08:
09: /**
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 request) {
25: Debug.println(Debug.STACKMVC, "Processing double.");
26: StackModel stackModel = StackModel.getInstance();
27: String top;
28: try {
29: stackModel.doubles();
30: top = stackModel.top();
31: } catch (EmptyStackException e) {
32: request.setAttribute("errorMessage",
33: "Attempt to double empty stack.");
34: top = "The stack is empty.";
35: }
36: Object[] stackArray = stackModel.toArray();
37: request.setAttribute("stackArray", stackArray);
38: request.setAttribute("top", top);
39: return Page.INDEX;
40: }
41: }
|