import java.util.Scanner; /* Application program that exercies the methods in the SetOfStrings class. */ public class SetOfStrApp { private static Scanner keyboard; // for reading user input private static boolean keepGoing; // for loop control private static SetOfStrings set; public static void main(String[] args) { System.out.println("Welcome to the Set Of Strings Application."); keyboard = new Scanner(System.in); int cap = getInt("Enter desired set capacity: "); set = new SetOfStrings(cap); keepGoing = true; //printHelp(); while (keepGoing) { try { performCommand(); } catch (Exception e) { e.printStackTrace(System.out); } } System.out.println("Goodbye."); } /* Prompts the user to enter a command and then carries it out. */ private static void performCommand() { boolean validCommand = true; String[] command = getString("\n> ").split("\\s+"); if (command.length == 0) { validCommand = false; } else if (command.length == 1) { String code = command[0]; if (code.equals("q")) { keepGoing = false; } else if (code.equals("h")) { printHelp(); } else if (code.equals("p")) { set.display(); } else if (code.equals("t")) { set.displayTable(); } else if (code.equals("n")) { System.out.printf("set has %d members", set.numberOfMembers()); } else { validCommand = false; } } else if (command.length == 2) { String code = command[0]; String str = command[1]; if (code.equals("m")) { println(set.isMemberOf(str) ? "yes" : "no"); } else if (code.equals("i")) { set.insert(str); set.displayTable(); } else if (code.equals("r")) { set.remove(str); set.displayTable(); } else { validCommand = false; } } else { validCommand = false; } if (!validCommand) { println("Command not recognized; enter h for help"); } } /* Prints the specified prompt and returns the integer value ** entered in response. */ private static int getInt(String prompt) { System.out.print(prompt); int response = keyboard.nextInt(); keyboard.nextLine(); return response; } /* Prints the specified prompt and returns the String ** entered in response. */ private static String getString(String prompt) { System.out.print(prompt); return keyboard.nextLine(); } /* Prints the menu of available commands. */ private static void printHelp() { println("q (quit)"); println("h (print this help menu)"); println("p (print the whole set)"); println("t (print the table)"); println("n (report the # of members)"); println("i glorp (insert \"glorp\")"); println("r glorp (remove \"glorp\")"); println("m glorp (report whether \"glorp\" is a member)"); } /* Surrogate for System.out.println(). */ private static void println(String s) { System.out.println(s); } }