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