/home/mbilodea/NetBeansProjects/CS1/src/arrayplay/NumberList.java
  1 /*
  2  * Programs featuring an array to store and interactively manipulate a list
  3  * of numbers
  4  */
  5 package arrayplay;
  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  * @author mbilodea
 17  */
 18 public class NumberList {
 19 
 20     // VARIABLES LOCAL TO THE CLASS, AND HENCE GLOBAL TO THE METHODS
 21     private static final int LIMIT = 1000;
 22     private static int[] numbers = new int[LIMIT];
 23     private static int numberOfNumbers = 0;
 24     private static Scanner commandReader = new Scanner(System.in);
 25     
 26     /**
 27      * @param args the command line arguments
 28      */
 29     public static void main(String[] args) {
 30         try {
 31             // ESTABLISH THE ARRAY OF NUMBERS
 32             readNumbers();
 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     
 44     // Assuming that the data file will be found in the public_html/data
 45     // subdirectory of the user's home directory.
 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 readNumbers() throws FileNotFoundException {
 55         Scanner scanner = establishScanner("NumberSet.text");
 56         while ( scanner.hasNext() ) {
 57             numbers[numberOfNumbers] = scanner.nextInt();
 58             numberOfNumbers = numberOfNumbers + 1;
 59         }
 60     }
 61     
 62     private static void displayNumbers() {
 63         for ( int x = 0; x < numberOfNumbers; x = x + 1) {
 64             System.out.println(numbers[x]);
 65         }
 66     }
 67     
 68     private static void interpreter() {
 69         System.out.print(">>> ");
 70         while ( true ) {
 71         String command = commandReader.next();
 72         if ( command.equalsIgnoreCase("DISPLAY") ) {
 73             interpreterDisplayCommand();
 74         } else if ( command.equalsIgnoreCase("PRINT") ) {
 75             interpretPrintCommand();
 76         } else if ( command.equalsIgnoreCase("SWAP") ) {
 77             interpretSwapCommand();
 78         } else if ( command.equalsIgnoreCase("ADD") ) {
 79             interpretAddCommand();
 80         } else if ( command.equalsIgnoreCase("HELP") ) {
 81             interpretHelpCommand();
 82         } else if ( command.equalsIgnoreCase("EXIT") ) {
 83             System.exit(0);
 84         } else {
 85             System.out.println("### Unrecognizable Command: " + command);
 86         } System.out.print(">>> ");
 87         } 
 88     }
 89     
 90     private static void interpreterDisplayCommand() {
 91         displayNumbers();
 92     }
 93     
 94     private static void interpretPrintCommand() {
 95         String operand = commandReader.next();
 96         if ( operand.equalsIgnoreCase("FIRST") ) {
 97             System.out.println(numbers[0]);
 98         } else if ( operand.equalsIgnoreCase("LAST") ) {
 99             System.out.println(numbers[numberOfNumbers-1]);
100         } else {
101             int index = Integer.valueOf(operand);
102             System.out.println(numbers[index-1]);
103         }
104     }
105     
106     private static void interpretHelpCommand() {
107         System.out.println("HELP - display a menu of commands");
108         System.out.println("DISPLAY - display the list of numbers");
109         System.out.println("PRINT - print a number (FIRST;LAST;nth)");
110         System.out.println("SWAP - exchange two elements (nth;mth)");
111         System.out.println("ADD - add a number to the list (FIRST;LAST)");
112         System.out.println("EXIT - terminate execution of the program");
113     }
114     
115     private static void interpretSwapCommand() {
116         int position1 = commandReader.nextInt();
117         int position2 = commandReader.nextInt();
118         int temp = numbers[position1-1];
119         numbers[position1-1] = numbers[position2-1];
120         numbers[position2-1] = temp;
121     }
122     
123     private static void interpretAddCommand() {
124         String position = commandReader.next();
125         if ( position.equalsIgnoreCase("LAST") ) {
126             addLast();
127         } else if ( position.equalsIgnoreCase("FIRST") ) {
128             addFirst();
129         } else {
130             System.out.println("### invalid operand for add command");
131         }
132         numberOfNumbers = numberOfNumbers + 1;
133     }
134     
135     private static void addLast() {
136         numbers[numberOfNumbers] = commandReader.nextInt();
137     }
138     
139     private static void addFirst() {
140         for ( int x = numberOfNumbers; x > 0; x = x - 1 ) {
141             numbers[x] = numbers[x-1];
142         }
143         numbers[0] = commandReader.nextInt();
144     }
145 }