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