1 /* 2 * Program featuring an array to store 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.io.IOException; 9 import java.util.Scanner; 10 11 public class NumberList { 12 // VARIABLES LOCAL TO THE CLASS, AND HENCE GLOBAL TO THE METHODS 13 private static final int LIMIT = 1000; 14 private static int[] numbers = new int[LIMIT]; 15 private static int numberOfNumbers = 0; 16 private static Scanner commandReader = new Scanner(System.in); 17 18 public static void main(String[] args) { 19 try { 20 // ESTABLISH THE ARRAY OF NUMBERS 21 readNumbers(); 22 // CHECK THE DATA 23 // System.out.println("\nThe original list of numbers ..."); 24 // displayNumbers(); 25 // ENTER THE INTERPRETER 26 interpreter(); 27 } catch (IOException ex) { 28 System.out.println("The file was not found. Please think again."); 29 System.exit(-1); 30 } 31 } 32 33 //Make Compatible with my windows computer 34 public static String pathBuilder(){ 35 String separator = System.getProperty("file.separator"); 36 String home = null; 37 38 //Make Program Compatible With Windows (I use windows :P ) 39 if(System.getProperty("os.name").startsWith("Windows")) { 40 home = System.getProperty("user.dir"); 41 } 42 else if(System.getProperty("os.name").startsWith("Windows 10")) { 43 home = System.getProperty("user.dir"); 44 } 45 else { 46 home = System.getProperty("user.home"); 47 } 48 String path = home + separator + "CS1Files" + separator + "data" + separator; 49 return path; 50 } 51 52 // Assuming that the data file will be found in the public_html/data 53 // subdirectory of the user’s home directory. 54 private static Scanner establishScanner(String fn, String path) throws FileNotFoundException { 55 String fullFileName = path + fn; 56 return new Scanner(new File(fullFileName)); 57 } 58 59 60 private static void readNumbers() throws FileNotFoundException { 61 Scanner scanner = establishScanner("NumberSet.txt", pathBuilder()); 62 while (scanner.hasNext()) { 63 numbers[numberOfNumbers] = scanner.nextInt(); 64 numberOfNumbers = numberOfNumbers + 1; 65 } 66 } 67 68 private static void displayNumbers() { 69 for (int x = 0; x < numberOfNumbers; x = x + 1) { 70 System.out.println(numbers[x]); 71 } 72 } 73 74 private static void interpreter(){ 75 System.out.print(">>> "); 76 String command = commandReader.next(); 77 if (command.equalsIgnoreCase("DISPLAY")) { 78 interpreterDisplayCommand(); 79 } else if (command.equalsIgnoreCase("PRINT")) { 80 interpretPrintCommand(); 81 } else if (command.equalsIgnoreCase("SWAP")) { 82 interpretSwapCommand(); 83 } else if (command.equalsIgnoreCase("ADD")) { 84 interpretAddCommand(); 85 } else if (command.equalsIgnoreCase("HELP")) { 86 interpretHelpCommand(); 87 } else if(command.equalsIgnoreCase("CLEAR")) { 88 interpretClearCommand(); 89 } else if(command.equalsIgnoreCase("LS")) { 90 interpretListCommand(pathBuilder()); 91 } else if (command.equalsIgnoreCase("EXIT")) { 92 System.exit(0); 93 } else { 94 System.out.println("### Unrecognizable command: " + command); 95 } 96 interpreter(); 97 } 98 99 private static void interpreterDisplayCommand() { 100 displayNumbers(); 101 } 102 103 private static void interpretPrintCommand() { 104 String operand = commandReader.next(); 105 if (operand.equalsIgnoreCase("FIRST")) { 106 System.out.println(numbers[0]); 107 } else if (operand.equalsIgnoreCase("LAST")) { 108 System.out.println(numbers[numberOfNumbers - 1]); 109 } else { 110 int index = Integer.valueOf(operand); 111 System.out.println(numbers[index - 1]); 112 } 113 } 114 115 private static void interpretHelpCommand() { 116 System.out.println("HELP - display a menu of commands"); 117 System.out.println("DISPLAY - display the list of numbers"); 118 System.out.println("PRINT - print a number (FIRST;LAST;nth)"); 119 System.out.println("SWAP - exchange two elements (nth;mth)"); 120 System.out.println("ADD - add a number to the list (FIRST;LAST)"); 121 System.out.println("CLEAR - clears the console"); 122 System.out.println("LS - lists file in data directory"); 123 System.out.println("EXIT - terminate execution of the program"); 124 } 125 126 private static void interpretSwapCommand() { 127 int position1 = commandReader.nextInt(); 128 int position2 = commandReader.nextInt(); 129 int temp = numbers[position1 - 1]; 130 numbers[position1 - 1] = numbers[position2 - 1]; 131 numbers[position2 - 1] = temp; 132 } 133 134 private static void interpretAddCommand() { 135 String position = commandReader.next(); 136 if (position.equalsIgnoreCase("LAST")) { 137 addLast(); 138 } else if (position.equalsIgnoreCase("FIRST")) { 139 addFirst(); 140 } else { 141 System.out.println("### invalid operand for add command"); 142 } 143 numberOfNumbers = numberOfNumbers + 1; 144 } 145 146 147 private static void interpretClearCommand(){ 148 int intelliJConsoleHistorySize = 300; 149 150 for (int i = 0; i < intelliJConsoleHistorySize;++i) { 151 System.out.println(); 152 153 } 154 } 155 156 private static void interpretListCommand(String path){ 157 File dataFolder = new File(path); 158 File[] listOfFiles = dataFolder.listFiles(); 159 for( File element: listOfFiles) { 160 System.out.println(element); 161 } 162 } 163 164 private static void addLast() { 165 numbers[numberOfNumbers] = commandReader.nextInt(); 166 } 167 168 private static void addFirst() { 169 for (int x = numberOfNumbers; x > 0; x = x - 1) { 170 numbers[x] = numbers[x - 1]; 171 } 172 numbers[0] = commandReader.nextInt(); 173 } 174 }