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