import java.util.Scanner; /* Java application having as its purpose to test the features of the ** IndexedListViaLink1 class. ** ** Author: R. McCloskey ** Date: January 2024 */ public class IndexedListTester { // User commands: private static char HELP = 'h'; private static char QUIT = 'q'; private static char CREATE = 'c'; private static char LENGTH = 'L'; private static char ELEM_AT = 'e'; private static char DELETE = 'd'; private static char REPLACE = 'r'; private static char INSERT = 'i'; private static char APPEND = 'a'; private static char INDEX_OF = 'x'; private static char TSIL = 't'; private static char PRINT = 'p'; private static Scanner keyboard = new Scanner(System.in); private static boolean keepGoing = true;; private static IndexedListViaLink1 aList = new IndexedListViaLink1(); public static void main(String[] args) { System.out.println("Welcome to the IndexedListTester program."); System.out.println("Enter 'h' for a list of commands."); 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); try { performCommand(command); } catch (Exception e) { e.printStackTrace(System.out); print("\n Something went wrong; try again\n"); } } System.out.println("Goodbye."); } /* Performs the specified command upon aList. */ private static void performCommand(String command) { Scanner comScan = new Scanner(command); String commandCode = comScan.next(); if (commandCode.length() != 1) { print("Command code must be a single letter; try again.\n"); } else { char cc = commandCode.charAt(0); if (cc == HELP) { printHelp(); } else if (cc == QUIT) { keepGoing = false; } else if (cc == CREATE) { // create new list if (comScan.hasNext()) { String[] elems = comScan.nextLine().trim().split(" "); aList = new IndexedListViaLink1(elems); } else { // create empty list aList = new IndexedListViaLink1(); } print("New list created...\n"); displayList(); } else if (cc == LENGTH) { // display Length System.out.printf("%d\n", aList.lengthOf()); } else if (cc == ELEM_AT) { int k = comScan.nextInt(); print(aList.elemAt(k) + "\n"); } else if (cc == INDEX_OF) { String str = comScan.nextLine().trim(); int k = aList.indexOf(str); print(k + "\n"); } else if (cc == DELETE) { // delete/remove int k = comScan.nextInt(); String deletedStr = aList.remove(k); print("Deleted item is " + deletedStr + "\n"); displayList(); } else if (cc == REPLACE) { // replace int k = comScan.nextInt(); String str = comScan.nextLine().trim(); aList.replace(k, str); displayList(); } else if (cc == INSERT) { // insert int k = comScan.nextInt(); String newElem = comScan.next(); aList.insert(k, newElem); displayList(); } else if (cc == APPEND) { // append String newElem = comScan.next(); aList.append(newElem); displayList(); } else if (cc == TSIL) { // tsil (reverse) aList.tsil(); displayList(); } else if (cc == PRINT) { // print/display if (!comScan.hasNext()) { // display whole list aList.display(); } else { int start = comScan.nextInt(); int stop = comScan.nextInt(); aList.display(start, stop); } print("\n"); } else { print("Unrecognized command; try again."); } } } /* Displays aList. */ private static void displayList() { //print("List is now: "); aList.display(); print("\n"); } private static void printHelp() { print("Examples of commands:\n"); print("---------------------\n"); print(QUIT + " : to quit\n"); print(HELP + " : to display this list of example commands!\n"); print(LENGTH + " : to display length of the list\n"); print(ELEM_AT + " 5 : to display the element at position 5 of the list\n"); print(INDEX_OF + " cow : to find the position at which \"cow\" occurs in the list\n"); print(DELETE + " 3 : to delete/remove the element at position 3\n"); print(REPLACE + " 2 cow : to replace the element at position 2 with \"cow\"\n"); print(INSERT + " 6 cow : to insert \"cow\" at position 6\n"); print(APPEND + " moose : to append \"moose\" to the end of the list\n"); print(PRINT + " : to print/display all list elements\n"); print(PRINT + " 2 7: to print/display the list elements in positions 2 through 6\n"); print(CREATE + " dog cat pig : populates new list with three animals\n"); print(CREATE + " : makes a new empty list\n"); print(TSIL + " : to apply tsil() (to reverse the list)\n"); } /* Surrogate for System.out.print() */ private static void print(String s) { System.out.print(s); } }