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