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               // Remove the comment from line 23 and 24 to check the list of numbers in your file.
23              // System.out.println("The original list of numbers ...");
24               //displayNumbers();
25               //comment line 23 and 24 so the program would run properly.
26               // ENTER THE INTERPRETER
27               interpreter();
28           } catch (FileNotFoundException ex) {
29               System.out.println("The file was not found. Please think again.");
30               System.exit(-1);
31           }
32       }
33   
34       // Assuming that the data file will be found in the CS1Files/data
35       // subdirectory of the user’s home directory.
36       private static Scanner establishScanner(String fn) throws FileNotFoundException {
37           String separator = File.separator;
38           String homeDirectory = System.getProperty("user.home");
39           String path = homeDirectory + separator + "CS1Files" + separator + "data" + separator;
40           String fullFileName = path + fn;
41           return new Scanner(new File(fullFileName));
42       }
43   
44       private static void readNumbers() throws FileNotFoundException { //code that read the numbers in the file below (that you created)
45           Scanner scanner = establishScanner("NumberSet.txt");
46           while (scanner.hasNext()) {
47               numbers[numberOfNumbers] = scanner.nextInt();
48               numberOfNumbers = numberOfNumbers + 1;
49           }
50       }
51   
52       private static void displayNumbers() { //basic code that displays the items in the arrays, otherwise in this case, the numbers in our file.
53           for (int x = 0; x < numberOfNumbers; x = x + 1) {
54               System.out.println(numbers[x]);
55           }
56       }
57   
58       private static void interpreter() {
59           System.out.print(">>> ");
60           String command = commandReader.next();
61           if (command.equalsIgnoreCase("DISPLAY")) {
62               interpreterDisplayCommand();
63           } else if (command.equalsIgnoreCase("PRINT")) {
64               interpretPrintCommand();
65           } else if (command.equalsIgnoreCase("SWAP")) {
66               interpretSwapCommand();
67           } else if (command.equalsIgnoreCase("ADD")) {
68               interpretAddCommand();
69           } else if (command.equalsIgnoreCase("HELP")) {
70               interpretHelpCommand();
71           } else if (command.equalsIgnoreCase("EXIT")) {
72               System.exit(0);
73           } else {
74               System.out.println("### Unrecognizable command: " + command);
75           }
76          interpreter(); //this keeps the interpreter function running. Except the user types the "EXIT" command.
77       }
78   
79       private static void interpreterDisplayCommand() {
80           displayNumbers(); //find this before the interpreter above.
81       }
82   
83       private static void interpretPrintCommand() {
84           String operand = commandReader.next();
85           if (operand.equalsIgnoreCase("FIRST")) {
86               System.out.println(numbers[0]);
87           } else if (operand.equalsIgnoreCase("LAST")) {
88               System.out.println(numbers[numberOfNumbers - 1]);
89           } else {
90               int index = Integer.valueOf(operand);
91               System.out.println(numbers[index - 1]); //arrays start from zero, so we subtract one to compensate.
92           }
93       }
94   
95       private static void interpretHelpCommand() {
96           //The system would print these when the "HELP" command is typed. It guides the user on how to use this program.
97           System.out.println("HELP - display a menu of commands");
98           System.out.println("DISPLAY - display the list of numbers");
99           System.out.println("PRINT - print a number (FIRST;LAST;nth)");
100          System.out.println("SWAP - exchange two elements (nth;mth)");
101          System.out.println("ADD - add a number to the list (FIRST;LAST)");
102          System.out.println("EXIT - terminate execution of the program");
103      }
104  
105      private static void interpretSwapCommand() { //code to swap the position of two numbers
106          int position1 = commandReader.nextInt();
107          int position2 = commandReader.nextInt();
108          int temp = numbers[position1 - 1]; //this line ensures the positions are switched rather than replaced.
109          numbers[position1 - 1] = numbers[position2 - 1];
110          numbers[position2 - 1] = temp;
111      }
112  
113      private static void interpretAddCommand() {
114          String position = commandReader.next();
115          if (position.equalsIgnoreCase("LAST")) {
116              addLast();
117          } else if (position.equalsIgnoreCase("FIRST")) {
118              addFirst();
119          } else {
120              System.out.println("### invalid operand for add command");
121          }
122          numberOfNumbers = numberOfNumbers + 1;
123      }
124  
125      private static void addLast() {
126          numbers[numberOfNumbers] = commandReader.nextInt();
127      }
128  
129      private static void addFirst() {
130          for (int x = numberOfNumbers; x > 0; x = x - 1) {
131              numbers[x] = numbers[x - 1];
132          }
133          numbers[0] = commandReader.nextInt();
134      }
135  }