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