import Stack; import SimpleInput; import SimpleOutput; /** * A class to be used for conducting tests on stacks. * * @author Samuel A. Rebelsky * @version 1.0 of April 1999 */ public class StackTester { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** The stack we're testing. */ protected Stack stack; /** Where to write output. */ protected SimpleOutput output; /** Where to read input. */ protected SimpleInput input; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** * Build a new tester. */ public StackTester(Stack s, SimpleInput in, SimpleOutput out) { this.stack = s; this.input = in; this.output = out; } // StackTester(Stack, SimpleInput, SimpleOutput) // +---------+------------------------------------------------- // | Methods | // +---------+ /** * Run the tester. */ public void run() { boolean done = false; // Are we done yet? String command; // The command. // Keep going until we're done. (Duh.) while (!done) { // Describe the stack. output.println("Stack: " + stack.toString()); output.println("Full: " + stack.isFull()); output.println("Empty: " + stack.isEmpty()); output.println(); // Ask the user what to do output.println("What do you want to do? "); output.println(" [Q]uit."); output.println(" [A]dd an element."); output.println(" [P]eek at the top of the stack."); output.println(" [R]emove an element."); output.print("command: "); // Get a response. command = input.readString(); // Respond to the request. if (command.equals("Q")) { done = true; } else if (command.equals("A")) { output.print("String to add: "); stack.add(input.readString()); } else if (command.equals("P")) { output.println("Top: " + stack.peek().toString()); } else if (command.equals("R")) { output.println("Removed: " + stack.remove().toString()); } else { output.println("Unknown command: '" + command + "'"); } } // while not done } // run() } // class StackTester