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