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