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