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