package rebelsky.exam3; import java.io.PrintWriter; import java.io.BufferedReader; /** * Something that permits interactive testing of linear * structures. * * @author Samuel A. Rebelsky * @version 1.0 of November 2005 */ public class LinearTester { /** * Run an interfactive test of ls using in and and out for * input and output. */ public static void test(LinearStructure ls, BufferedReader in, PrintWriter out) { String command; // The user's command boolean done = false; instructions(out); while (!done) { out.print("Command? "); out.flush(); try { command = in.readLine().toLowerCase(); } catch (Exception e) { out.println("I/O problems. Stopping. " + e); command = "quit"; } if (command.equals("help")) { instructions(out); } else if (command.equals("get")) { try { out.println("Got '" + ls.get() + "'"); } catch (Exception e) { out.println("Failed to get a value because " + e); } } // get else if (command.equals("status")) { out.println("Size: " + ls.size()); out.println("Empty? " + ls.isEmpty()); out.println("Full? " + ls.isFull()); } else if (command.equals("put")) { out.print("Put what? "); out.flush(); try { String putme = in.readLine(); ls.put(putme); } catch (Exception e) { out.println("Failed to put because " + e); } } // put else if (command.equals("quit")) { done = true; } else { out.println("Do not understand '" + command + "'"); instructions(out); } } // while } // test(LinearStructure, BufferedReader, PrintWriter) /** * Print the instructions. */ static void instructions(PrintWriter out) { out.println("quit - stop the program"); out.println("help - print these instructions"); out.println("status - get the status of the structure"); out.println("get - get an element from the structure"); out.println("put - add an element to the structure"); } // instructions(PrintWriter) } // LinearTester