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