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