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