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