/home/ffrigin/NetBeansProjects/CS1/src/arraylistplay/WordList.java
  1 /*
  2     * Program featuring an array to store and interactively manipulate a list
  3     * of words
  4  */
  5 package arraylistplay;
  6 
  7 import java.io.File;
  8 import java.io.FileNotFoundException;
  9 import java.util.Scanner;
 10 import java.util.ArrayList;
 11 
 12 /**
 13  *
 14  * @author ffrigin
 15  */
 16 public class WordList {
 17 
 18     // VARIABLES LOCAL TO THE CLASS, AND HENCE GLOBAL TO THE METHODS
 19     private static ArrayList<String> words = new ArrayList<>();
 20     private static int numberOfWords = 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 WORDS
 29             readWords();
 30             // CHECK THE 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 + "CS1Files" + separator + "data" + separator;
 47         String fullFileName = path + fn;
 48         return new Scanner(new File(fullFileName));
 49     }
 50 
 51     private static void interpreter() {
 52         System.out.print(">>> ");
 53         String command = commandReader.next();
 54         if (command.equalsIgnoreCase("DISPLAY")) {
 55             interpreterDisplayCommand();
 56         } else if (command.equalsIgnoreCase("PRINT")) {
 57             interpretPrintCommand();
 58         } else if (command.equalsIgnoreCase("SWAP")) {
 59             interpretSwapCommand();
 60         } else if (command.equalsIgnoreCase("ADD")) {
 61             interpretAddCommand();
 62         } else if (command.equalsIgnoreCase("HELP")) {
 63             interpretHelpCommand();
 64         } else if (command.equalsIgnoreCase("EXIT")) {
 65             System.exit(0);
 66         } else {
 67             System.out.println("### Unrecognizable command: " + command);
 68         }
 69         interpreter();
 70     }
 71 
 72     private static void interpreterDisplayCommand() {
 73         displayWords();
 74     }
 75 
 76     private static void interpretPrintCommand() {
 77         String operand = commandReader.next();
 78         if (operand.equalsIgnoreCase("FIRST")) {
 79             System.out.println(words.get(0));
 80         } else if (operand.equalsIgnoreCase("LAST")) {
 81             System.out.println(words.get(numberOfWords - 1));
 82         } else {
 83             int index = Integer.valueOf(operand);
 84             System.out.println(words.get(index - 1));
 85         }
 86     }
 87 
 88     private static void interpretHelpCommand() {
 89         System.out.println("HELP - display a menu of commands");
 90         System.out.println("DISPLAY - display the list of words");
 91         System.out.println("PRINT -  print a word (FIRST; LAST; nth)");
 92         System.out.println("SWAP - exchange two elements (nth;mth)");
 93         System.out.println("ADD - add a word to the list(FIRST;LAST)");
 94         System.out.println("EXIT - terminate execution of the program");
 95     }
 96 
 97     private static void interpretSwapCommand() {
 98         int position1 = commandReader.nextInt();
 99         int position2 = commandReader.nextInt();
100         String temp = words.get(position1 - 1);
101         words.set(position1 - 1, words.get(position2 - 1));
102         words.set(position2 - 1, temp);
103     }
104 
105     private static void interpretAddCommand() {
106         String position = commandReader.next();
107         if (position.equalsIgnoreCase("LAST")) {
108             addLast();
109         } else if (position.equalsIgnoreCase("FIRST")) {
110             addFirst();
111         } else {
112             System.out.println("### invalid operand for add command");
113         }
114         numberOfWords = numberOfWords + 1;
115 
116     }
117 
118     private static void addLast() {
119         words.add(numberOfWords, commandReader.next());
120 
121     }
122 
123     private static void addFirst() {
124         words.add(0, commandReader.next());
125     }
126 
127     private static void readWords() throws FileNotFoundException {
128         Scanner scanner = establishScanner("WordSet.text");
129         while (scanner.hasNext()) {
130             words.add(numberOfWords, scanner.next());
131             numberOfWords = numberOfWords + 1;
132 
133         }
134     }
135 
136     private static void displayWords() {
137         for (int x = 0; x < numberOfWords; x = x + 1) {
138             System.out.println(words.get(x));
139         }
140     }
141 }
142