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