import java.util.Scanner; /* DequeTester.java ** This Java application is for the purpose of testing a class that ** implements the Deque interface. ** ** Author: R. McCloskey ** Date: March 2019 */ public class DequeTester { private static boolean keepGoing; private static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { System.out.println("Welcome to the Deque Tester!"); Deque deque = null; int k = getInt("Enter 1 to test an array-based deque;\n" + "Enter 2 to test a Link2-based deque\n> "); if (k == 1) { int cap = getInt("Choose capacity of deque (suggested is 8): "); deque = new DequeViaArray(cap); } else if (k == 2) { deque = new DequeViaLink2(); } else { System.out.println("Invalid response; program aborting"); System.exit(0); } testDeque(deque); } public static void testDeque(Deque deque) { keepGoing = true; while (keepGoing) { String command; // Prompt user until a non-empty string is entered do { System.out.print("\n>"); command = keyboard.nextLine().trim(); } while (command.length() == 0); // Having received a non-empty command, try to perform it. try { performCommand(deque, command); } catch (Exception e) { e.printStackTrace(System.out); System.out.println("\n Something went wrong; try again"); } } System.out.println("Goodbye."); } /* Applies the command described by the second parameter to the deque ** given by the first. */ private static void performCommand(Deque deque, String command) { Scanner comScanner = new Scanner(command); String commandCode = comScanner.next(); String insertWarning = "Need to specify a string to insert!"; if (commandCode.equals("q")) { keepGoing = false; } else if (commandCode.equals("pr")) { System.out.printf("Rear item is %s\n", deque.rearOf()); } else if (commandCode.equals("pf")) { System.out.printf("Front item is %s\n", deque.frontOf()); } else if (commandCode.equals("ps")) { System.out.printf("Size of deque is %s\n", deque.sizeOf()); } else if (commandCode.equals("pd")) { printContents(deque); //System.out.printf("Contents of deque: %s\n", deque.toString()); } else if (commandCode.equals("ir")) { // insert at rear if (!comScanner.hasNext()) { System.out.println(insertWarning); } else { String item = comScanner.next(); System.out.printf("Inserting \"%s\" at rear...\n", item); deque.insertAtRear(item); printContents(deque); //System.out.printf("Contents of deque: %s\n", deque.toString()); } } else if (commandCode.equals("if")) { // insert at front if (!comScanner.hasNext()) { System.out.println(insertWarning); } else { String item = comScanner.next(); System.out.printf("Inserting \"%s\" at front...\n", item); deque.insertAtFront(item); printContents(deque); } } else if (commandCode.equals("rr")) { // remove from rear System.out.println("Removing item at rear..."); deque.removeRear(); printContents(deque); } else if (commandCode.equals("rf")) { // remove from frong System.out.println("Removing item at front..."); deque.removeFront(); printContents(deque); } else if (commandCode.equals("h")) { printHelp(); } else { System.out.println("Unrecognized command."); } } private static void printHelp() { System.out.println("q : to quit"); System.out.println("h : for help"); System.out.println("ps : to print size of deque"); System.out.println("pr : to print item at rear of deque"); System.out.println("pf : to print item at front of deque"); System.out.println("pd : to print all items in deque"); System.out.println("rr : to remove item from rear"); System.out.println("rf : to remove item from front"); System.out.println("ir x : to insert string x at rear"); System.out.println("if x : to insert string x at front"); } private static void printContents(Deque d) { System.out.println("Contents of deque: " + d); } private static int getInt(String prompt) { System.out.print(prompt); return Integer.parseInt(keyboard.nextLine().trim()); } }