/home/jfernan6/NetBeansProjects/CSX/src/arrayplay/WordList.java
  1 /*
  2  * Program featuring an array to store and interactively manipulate a list
  3  * of numbers
  4  */
  5    package arrayplay;
  6    
  7    import java.io.File;
  8    import java.io.FileNotFoundException;
  9   import java.util.Scanner;
 10   
 11   /**
 12    *
 13    * @author jfernan6
 14    */
 15   public class WordList {
 16   
 17       // VARIABLES LOCAL TO THE CLASS, AND HENSE GLOBAL TO THE METHODS
 18       private static final int LIMIT = 1000;
 19       private static String[] words = new String[LIMIT];
 20       private static int numberOfNumbers = 0;
 21       private static Scanner commandReader = new Scanner(System.in);
 22   
 23     /**
 24       * @param args the command line arguments
 25       */
 26       public static void main(String[] args) {
 27           try {
 28               // ESTABLISH THE ARRAY OF NUMBERS
 29               readWords();
 30               // CHECK DATA
 31               System.out.println("\nThe original list of words ...");
 32               displayWords();
 33               // ENTER THE INTERPRETER
 34               interpreter();
 35           } catch (FileNotFoundException ex) {
 36               System.out.println("The file was not found. Please think again");
 37               System.exit(-1);
 38           }
 39       }
 40   
 41       // Assuming that the data file will be found in the public_html/data
 42       // subdirectory of the user's home directory
 43       private static Scanner establishScanner(String fn) throws FileNotFoundException {
 44           String separator = System.getProperty("file.separator");
 45           String homeDirectory = System.getProperty("user.home");
 46           String path = homeDirectory + separator + "data" + separator;
 47           String fullFileName = path + fn;
 48           return new Scanner(new File(fullFileName));
 49       }
 50   
 51       private static void readWords() throws FileNotFoundException {
 52           Scanner scanner = establishScanner("WordSet.text");
 53           while (scanner.hasNext()) {
 54               words[numberOfNumbers] = scanner.next();
 55               numberOfNumbers = numberOfNumbers + 1;
 56           }
 57       }
 58   
 59       private static void displayWords() {
 60           for (int x = 0; x < numberOfNumbers; x = x + 1) {
 61               System.out.println(words[x]);
 62           }
 63       }
 64   
 65       private static void interpreter() {
 66           System.out.print(">>>");
 67           String command = commandReader.next();
 68           if (command.equalsIgnoreCase("DISPLAY")) {
 69              interpretDisplayCommand();
 70           } else if (command.equalsIgnoreCase("PRINT")) {
 71               interpretPrintCommand();
 72           } else if (command.equalsIgnoreCase("SWAP")) {
 73               interpretSwapCommand();
 74           } else if (command.equalsIgnoreCase("ADD")) {
 75               interpretAddCommand();
 76           } else if (command.equalsIgnoreCase("HELP")) {
 77               interpretHelpCommand();
 78           } else if (command.equalsIgnoreCase("EXIT")) {
 79               System.exit(0);
 80           } else {
 81               System.out.println("### Unrecognizible command: " + command);
 82           }
 83           interpreter();
 84       }
 85   
 86       private static void interpretDisplayCommand() {
 87           displayWords();
 88       }
 89   
 90       private static void interpretPrintCommand() {
 91           String operand = commandReader.next();
 92           if (operand.equalsIgnoreCase("FIRST")) {
 93               System.out.println(words[0]);
 94           } else if (operand.equalsIgnoreCase("LAST")) {
 95               System.out.println(words[numberOfNumbers - 1]);
 96           } else {
 97              int index = Integer.valueOf(operand);
 98              System.out.println(words[index - 1]);
 99          }
100      }
101  
102      private static void interpretHelpCommand() {
103          System.out.println("HELP - display a menu of commands");
104          System.out.println("DISPLAY - display the lsit of numbers");
105          System.out.println("PRINT - print a number (FIRST;LAST:nth");
106          System.out.println("SWAP - exchange two elements (nth;mth)");
107          System.out.println("ADD - add a number to the list (FIRST;LAST)");
108          System.out.println("EXIT - terminate execution of the program");
109      }
110  
111      private static void interpretSwapCommand() {
112          int position1 = commandReader.nextInt();
113          int position2 = commandReader.nextInt();
114          String temp = words[position1 - 1];
115          words[position1 - 1] = words[position2 - 1];
116          words[position2 - 1] = temp;
117      }
118  
119      private static void interpretAddCommand() {
120          String position = commandReader.next();
121          if (position.equalsIgnoreCase("LAST")) {
122              addLast();
123         } else if (position.equalsIgnoreCase("FIRST")) {
124              addFirst();
125          } else {
126              System.out.println("### invalid operand for add command");
127          }
128      numberOfNumbers = numberOfNumbers + 1;
129      }
130  
131      private static void addLast() {
132          words[numberOfNumbers] = commandReader.next();
133      }
134  
135      private static void addFirst() {
136          for (int x = numberOfNumbers; x > 0; x = x - 1) {
137              words[x] = words[x - 1];
138          }
139          words[0] = commandReader.next();
140     }
141  }
142