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