/home/ssingh6/NetBeansProjects/CS1/src/arrayplay/NumberList.java
  1 /*
  2  * To change this license header, choose License Headers in Project Properties.
  3  * To change this template file, choose Tools | Templates
  4  * and open the template in the editor.
  5  */
  6 package arrayplay;
  7 
  8 import java.io.File;
  9 import java.io.FileNotFoundException;
 10 import java.util.Scanner;
 11 
 12 /**
 13  *
 14  * @author ssingh6
 15  */
 16 public class NumberList {
 17 
 18     /**
 19      * @param args the command line arguments
 20      */
 21     
 22         // VARIALBES LOCAL TO THE CLASS, AND HENCE GLOBAL TO THE METHODS
 23       private static final int LIMIT = 1000;
 24       private static int[] numbers = new int[LIMIT];
 25       private static int numberOfNumbers = 0;
 26      private static Scanner commandReader = new Scanner(System.in);
 27   
 28      /**
 29        * @param args the command line arguments
 30        */
 31       
 32      public static void main(String[] args) {
 33           try {
 34               //ESTABLIS ARRAY OF NUMBERS
 35               readNumbers();
 36               // CHECK THE DATA
 37               //System.out.println("\nThe original list of numbers ...");
 38               //displayNumbers();
 39               //ENTER THE INTERPRETER
 40               interpreter();
 41           } catch (FileNotFoundException ex) {
 42               System.out.println("The file was not found. Please think again.");
 43               System.exit(-1);
 44           }
 45       }
 46  
 47      // Assuming that the data file will be found in the public_html/data
 48       // subdirectory of the user's home directory
 49       private static Scanner establishScanner(String fn) throws FileNotFoundException {
 50          String separator = System.getProperty("file.separator");
 51           String homeDirectory = System.getProperty("user.home");
 52           String path = homeDirectory + separator + "CS1Files" + separator + "data" + separator;
 53           String fullFileName = path + fn;
 54           return new Scanner(new File(fullFileName));
 55       }
 56       private static void readNumbers() throws FileNotFoundException {
 57           Scanner scanner = establishScanner("NumberSet.txt");
 58         while (scanner.hasNext()) {
 59               numbers[numberOfNumbers] = scanner.nextInt();
 60               numberOfNumbers = numberOfNumbers + 1;
 61           }
 62       }
 63   
 64       private static void displayNumbers() {
 65           for (int x = 0; x < numberOfNumbers; x = x + 1) {
 66               System.out.println(numbers[x]);
 67           }
 68       }
 69   
 70       private static void interpreter() {
 71           System.out.print(">>>");
 72           String command = commandReader.next();
 73           if (command.equalsIgnoreCase("DISPLAY")) {
 74               interpreterDisplayCommand();
 75           } else if (command.equalsIgnoreCase("PRINT")) {
 76               interpreterPrintCommand();
 77          } else if (command.equalsIgnoreCase("SWAP")) {
 78               interpreterSwapCommand();
 79          } else if (command.equalsIgnoreCase("ADD")) {
 80               interpreterAddCommand();
 81           } else if (command.equalsIgnoreCase("HELP")) {
 82              interpreterHelpCommand();
 83          } else if (command.equalsIgnoreCase("EXIT")) {
 84               System.exit(0);
 85           } else {
 86               System.out.println("### Unrecognizable command: " + command);
 87           } 
 88           System.out.print("\n");
 89           interpreter();
 90       }
 91   
 92       private static void interpreterDisplayCommand() {
 93           displayNumbers();
 94       }
 95   
 96       private static void interpreterPrintCommand() {
 97          String operand = commandReader.next();
 98           if (operand.equalsIgnoreCase("FIRST")) {
 99               System.out.println(numbers[0]);
100           } else if (operand.equalsIgnoreCase("LAST")) {
101               System.out.println(numbers[numberOfNumbers-1]);
102           } else {
103              int index = Integer.valueOf(operand);
104             System.out.println(numbers[index-1]);
105          }
106      }
107  
108      private static void interpreterSwapCommand() {
109         int position1 = commandReader.nextInt();
110          int position2 = commandReader.nextInt();
111         int temp = numbers[position1 - 1];         numbers[position1 - 1] = numbers[position2 - 1];
112          numbers[position2 - 1] = temp;
113     }
114  
115      private static void interpreterAddCommand() {
116          String position = commandReader.next();
117          if (position.equalsIgnoreCase("Last")) {
118              addLast();
119          } else if (position.equalsIgnoreCase("FIRST")) {
120              addFirst();
121          } else {
122              System.out.println("### invalid operand for add command");
123          }
124          numberOfNumbers = numberOfNumbers + 1;
125      }
126  
127      private static void interpreterHelpCommand() {
128          System.out.println("HELP    - display menu of commands");
129          System.out.println("DISPLAY - display the list of numbers");
130          System.out.println("PRINT   - print a number (FIRST; LAST; nth)");
131          System.out.println("SWAP    - exchange two elements (nth; mth)");
132         System.out.println("ADD     - add a number to the list (FIRST; LAST)");
133         System.out.println("EXIT    - terminate execution of the program");
134      } 
135      private static void addFirst() {
136          for (int x = numberOfNumbers; x > 0; x = x - 1) {
137              numbers[x] = numbers[x - 1];
138          }
139          numbers[0] = commandReader.nextInt();
140      }
141  
142      private static void addLast() {
143         numbers[numberOfNumbers] = commandReader.nextInt();
144      }
145      
146  }
147  
148 
149