NumberList.java
1    /* 
2     * Program featuring an array to store and interactively manipulate a list of numbers 
3     */
4    
5    package arrayPlay;
6    
7    import java.io.File;
8    import java.io.FileNotFoundException;
9    import java.util.Scanner;
10   
11   public class NumberList {
12       // VARIABLES LOCAL TO THE CLASS, AND HENCE GLOBAL TO THE METHODS
13       private static final int LIMIT = 1000;
14       // new array called number
15       private static int[] numbers = new int [LIMIT];
16       private static int numberOfNumbers=0;
17       private static Scanner commandReader = new Scanner(System.in);
18   
19       public static void main(String[] args){
20           try{
21               // ESTABLISH THE ARRAY OF NUMBERS
22               readNumbers();
23               // CHECK THE DATA
24               // System.out.println("nThe original list of numbers..")
25               // displayNumber():
26               // ENTER THE INTERPRETER
27               interpreter();
28           } catch (FileNotFoundException ex) {
29               // If the file is not found, the system will catch it to tell the user
30               // to inform. then the system exit after 1 sec.
31               System.out.println("The file was not found. Please think again");
32               System.exit(-1);
33           }
34       }
35   
36       // Assuming the data file will be found in the public.html/data
37       // subdirectory of the user's home directory
38       private static Scanner establishScanner(String fn) throws FileNotFoundException{
39           String separator = File.separator;
40           String homeDirectory = System.getProperty("user.home");
41           // tell the system the direction of the file so its user/home/pubic_html/data
42           String path = homeDirectory + separator + "public_html" + separator + "CS1WorkSite" + separator + "arrayPlay" + separator;
43           String fullFileName = path + fn;
44           return new Scanner(new File(fullFileName));
45       }
46   
47       private static void readNumbers() throws FileNotFoundException {
48           // scanner will scan the new file in the directory given in the establishScanner methods
49           Scanner scanner = establishScanner("NumberSet.txt");
50           // scanner.hasNext() is a boolean that is true if there is another token in the input.
51           while (scanner.hasNext()) {
52               numbers[numberOfNumbers] = scanner.nextInt();
53               numberOfNumbers = numberOfNumbers + 1;
54           }
55       }
56   
57       private static void displayNumbers(){
58           // this will output everything added into the array
59           for (int x = 0; x < numberOfNumbers; x = x +1 ){
60               System.out.println(numbers[x]);
61           }
62       }
63   
64       private static void interpreter() {
65           System.out.print(">>> ");
66           // commandReader(scanner) will return the next string from the system input
67           String command = commandReader.next();
68           if (command.equalsIgnoreCase("DISPLAY")) {
69               interpreterDisplayCommand();
70           } else if (command.equalsIgnoreCase("PRINT")){
71               interpreterPrintCommmand();
72           } else if (command.equalsIgnoreCase("SWAP")){
73               interpreterSwapCommand();
74           } else if (command.equalsIgnoreCase("ADD")) {
75               interpreterAddCommand();
76           } else if (command.equalsIgnoreCase("HELP")) {
77               interpreterHelpCommand();
78           } else if (command.equalsIgnoreCase("EXIT")) {
79               System.exit(0);
80           } else {
81               System.out.println("### Unregonizable Command: " + command);
82           }
83           interpreter();
84       }
85   
86       private static void interpreterDisplayCommand() {
87           // Can't we just write this before since it's only one command????
88           displayNumbers();
89       }
90   
91       private static void interpreterPrintCommmand() {
92           String operand = commandReader.next();
93           if (operand.equalsIgnoreCase("First")){
94               System.out.println(numbers[0]);
95           } else if (operand.equalsIgnoreCase("LAST")){
96               System.out.println(numbers[numberOfNumbers-1]);
97           } else {
98               // "Integer.valueOf(operand)" convert the string operand to an integer. Super useful
99               int index = Integer.valueOf(operand);
100              System.out.println(numbers[index-1]);
101          }
102      }
103  
104      private static void interpreterHelpCommand() {
105          System.out.println("HELP - display a menu of commands");
106          System.out.println("DISPLAY - display the list of numbers");
107          System.out.println("PRINT - print a number (FIRST;LAST;nth)");
108          System.out.println("SWAP - exchange two elements (nth;mth)");
109          System.out.println("ADD - add a number to the list (FIRST;LAST)");
110          System.out.println("EXIT - terminate execution of the program");
111      }
112  
113      private static void interpreterSwapCommand(){
114          int position1 = commandReader.nextInt();
115          int position2 = commandReader.nextInt();
116          int temp = numbers[position1-1];
117          numbers[position1 -1] = numbers[position2-1];
118          numbers[position2 -1] = temp;
119      }
120  
121      private static void interpreterAddCommand() {
122          String position = commandReader.next();
123          if (position.equalsIgnoreCase("LAST")){
124              addlast();
125          } else if (position.equalsIgnoreCase("FIRST")){
126              addFirst();
127          } else {
128              System.out.println("### invalid operand for add command");
129          }
130          numberOfNumbers = numberOfNumbers + 1;
131      }
132  
133      private static void addlast() {
134          numbers[numberOfNumbers] = commandReader.nextInt();
135      }
136  
137      private static void addFirst() {
138          for (int x = numberOfNumbers; x >0; x = x - 1){
139              numbers[x] = numbers [x - 1];
140          }
141          numbers[0] = commandReader.nextInt();
142      }
143  }
144