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