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