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