WordList.java
1    package assign_5;
2    
3    import java.io.File;
4    import java.io.FileNotFoundException;
5    import java.util.Scanner;
6    
7    public class WordList {
8        //VARIABLES LOCAL TO THE CLASS, AND HENCE GLOBAL TO THE METHODS
9        private static final int LIMIT = 1000;
10       private static final 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               //ESTABLISH THE ARRAY OF NUMBERS
17               readWords();
18               //CHECK THE DATA
19   //            System.out.println("NThe original list of numbers...");
20   //            displaceNumbers();
21               //ENTER THE INTERPRETER
22               interpreter();
23           } catch(FileNotFoundException ex){
24               System.out.println("The file was not found. Please think again");
25               System.exit(-1);
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 + "Desktop" + separator + "public_html" + separator + "CS1WorkSite" + 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("WordSet.txt");
40           while(scanner.hasNext()){
41               words[numberOfWords] = scanner.next();
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[x]);
49           }
50       }
51   
52       private static void interpreter(){
53           System.out.println(">>>");
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       private static void interpreterDisplayCommand(){
73           displayNumbers();
74       }
75       private static void interpretPrintCommand(){
76           String operand = commandReader.next();
77           if(operand.equalsIgnoreCase("FIRST")){
78               System.out.println(words[0]);
79           }else if (operand.equalsIgnoreCase("LAST")){
80               System.out.println(words[numberOfWords - 1]);
81           }else{
82               int index = Integer.valueOf(operand);
83               System.out.println(words[index - 1]);
84           }
85       }
86       private static void interpretHelpCommand(){
87           System.out.println("Help - Display a menu of commands");
88           System.out.println("DISPLAY - display the list of words");
89           System.out.println("PRINT - print a word (FIRST;LAST;nth");
90           System.out.println("SWAP - exchange two words (nth;mth");
91           System.out.println("ADD - add a word to the list (FIRST;LAST");
92           System.out.println("EXIT - terminate execution of the program");
93       }
94       private static void interpretSwapCommand(){
95           int position1 = commandReader.nextInt();
96           int  position2 = commandReader.nextInt();
97           String temp = words[position1 - 1];
98           words[position1 - 1] = words[position2 - 1];
99           words[position2 - 1] = temp;
100      }
101      private static void interpretAddCommand(){
102          String position = commandReader.next();
103          if(position.equalsIgnoreCase("LAST")){
104              addLast();
105          }else if (position.equalsIgnoreCase("FIRST")){
106              addFirst();
107          }else{
108              System.out.println("### invalid operand for add command");
109          }
110          numberOfWords = numberOfWords + 1;
111      }
112      private static void addLast(){
113          words[numberOfWords] = commandReader.next();
114      }
115      private static void addFirst(){
116          for(int x = numberOfWords; x>0; x = x - 1){
117              words[x] = words[x - 1];
118          }
119          words[0] = commandReader.next();
120      }
121  }
122