WordList.java
1    package arraylistplay;
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 WordList {
9        private static final int LIMIT = 1000;
10       private static ArrayList<String> words = new ArrayList<>(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 + "IdeaProjects" + separator + "CSC-212" + separator + "src" + 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");
34           while (scanner.hasNext()) {
35               words.add(scanner.next());
36               numberofWords = numberofWords + 1;
37           }
38       }
39   
40       private static void displayNumbers() {
41           for (int x = 0; x < numberofWords; x = x + 1) {
42               System.out.println(words.get(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               interpretPrintCommand();
53           } else if (command.equalsIgnoreCase("SWAP")) {
54               interpretSwapCommand();
55           } else if (command.equalsIgnoreCase("ADD")) {
56               interpretAddCommand();
57           } else if (command.equalsIgnoreCase("HELP")) {
58               interpretHelpCommand();
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 interpreterDisplayCommand() {
68           displayNumbers();
69       }
70   
71       private static void interpretPrintCommand() {
72           String operand = commandReader.next();
73           if (operand.equalsIgnoreCase("FIRST")) {
74               System.out.println(words.get(0));
75           } else if (operand.equalsIgnoreCase("LAST")) {
76               System.out.println(words.get(numberofWords - 1));
77           } else {
78               int index = Integer.valueOf(operand);
79               System.out.println(words.get(index - 1));
80           }
81       }
82   
83       private static void interpretHelpCommand() {
84           System.out.println("HELP - display a menu of commands");
85           System.out.println("DISPLAY - display the list of numbers");
86           System.out.println("PRINT - print a number (FIRST;LAST;nth)");
87           System.out.println("SWAP - exchange two elements (nth;mth)");
88           System.out.println("ADD - add a number to the list (FIRST;LAST)");
89           System.out.println("EXIT - terminate execution of the program");
90       }
91   
92       private static void interpretSwapCommand() {
93           int position1 = commandReader.nextInt() ;
94           int position2 = commandReader.nextInt() ;
95           words.set(position1, words.set(position2, words.get(position1)));
96   
97       }
98   
99       private static void interpretAddCommand() {
100          String position = commandReader.next();
101          if (position.equalsIgnoreCase("LAST")) {
102              addLast();
103          } else if (position.equalsIgnoreCase("FIRST")) {
104              addFirst();
105          } else {
106              System.out.println("### invalid operand for add command");
107          }
108          numberofWords = numberofWords + 1;
109      }
110  
111      private static void addLast() {
112          words.add(commandReader.next());
113      }
114  
115      private static void addFirst() {
116          words.add(0, commandReader.next());
117      }
118  }