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