import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* Java application whose purpose is to test the methods of the ** IndexedSequence class (the generic version of the solution to ** CMPS 144, Spring 2019, Prog. Assg. #1). ** ** Author: R. McCloskey ** Date: February 2019 */ public class IndSeqTester { private static char NEW_CODE = 'N'; private static char GET_CODE = 'G'; private static char PUT_CODE = 'P'; private static char DISPLAY_CODE = 'D'; private static char HELP_CODE = 'H'; private static char QUIT_CODE = 'Q'; private static Scanner input; private static boolean echo; private static IndexedSequence seqOfStr; public static void main(String[] args) throws FileNotFoundException { // Establish the Scanner so that it reads from either the file whose name // was provided as a command-line argument or, absent any such argument, // from the keyboard. if (args.length != 0) { input = new Scanner(new File(args[0])); echo = true; } else { // no command line arg to specify input file name input = new Scanner(System.in); echo = false; } performCommands(); System.out.println("\nGoodbye."); } /* Processes commands provided as input, applying them to the sequence ** referred to by the global variable seqOfStr. ** Aside from the Quit ('Q') and Help ('H') commands, there is 'N' ** (to make a new sequence via the constructor), 'G' (to call get()), ** 'P' (to call put()), and 'D' (to display the sequence). ** Examples: ** "N 15 -4" : creates a sequence of length 14 with starting index -4 ** "G 5" : retrieves (and prints) the item at position 5 ** "P 4 17" : places 17 at position 4 ** "D" : displays the sequence */ private static void performCommands() { boolean keepGoing = true; while (keepGoing) { try { System.out.print("\nEnter command (H for help): "); String command = input.next(); if (echo) { System.out.println(command); } char operation = command.charAt(0); //String rest = command.substring(1).trim(); if (operation == NEW_CODE) { performNew(); } else if (operation == GET_CODE) { performGet(); } else if (operation == PUT_CODE) { performPut(); } else if (operation == DISPLAY_CODE) { performDisplay(); } else if (operation == HELP_CODE) { printHelp(); } else if (operation == QUIT_CODE) { keepGoing = false; } else { System.out.println(operation + " is not a known operation code"); input.nextLine(); } } catch (NullPointerException e) { e.printStackTrace(System.out); if (seqOfStr == null) { System.out.println("First use the " + NEW_CODE + " command to create a new sequence."); } } catch (Exception e) { e.printStackTrace(System.out); } } } private static void performNew() { int len = input.nextInt(); int startIndex = input.nextInt(); input.nextLine(); System.out.printf("Creating new sequence of length %d " + "with index range starting at %d\n", len, startIndex); seqOfStr = new IndexedSequence(len, startIndex); } private static void performGet() { int pos = input.nextInt(); input.nextLine(); System.out.printf("String at position %d is %s\n", pos, seqOfStr.get(pos)); } private static void performPut() { int pos = input.nextInt(); String str = input.next(); // a String input.nextLine(); System.out.printf("Putting %s at position %d\n", str, pos); seqOfStr.put(pos, str); } private static void performDisplay() { System.out.println(seqOfStr.toString()); input.nextLine(); } private static void printHelp() { System.out.println("Examples of commands:"); System.out.println(" N 8 5 (to create a new sequence with range 5..12"); System.out.println(" G 10 (to retrieve element at position 10)"); System.out.println(" P 7 Glork (to put \"Glork\" into position 7)"); System.out.println(" D (to display the sequence)"); System.out.println(" Q (to quit)"); } }