-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterface.java
More file actions
422 lines (395 loc) · 16.2 KB
/
Interface.java
File metadata and controls
422 lines (395 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import java.io.*;
import java.util.*;
public class Interface {
private static BSTDictionary dictionary = new BSTDictionary();
/*
* Main method - serves as the entry point for the application.
* It checks for the required input file argument, processes the input file,
* and then enters a loop to read and process user commands until the "exit" command is issued.
* @param args Command line arguments, expecting a single argument for the input file name.
*/
public static void main(String[] args) {
// Check if an input file is provided
if (args.length < 1) {
System.out.println("Usage: java Interface <inputFile>");
return;
}
String inputFile = args[0];
try {
// Read and process the input file
readInputFile(inputFile);
} catch (IOException | DictionaryException e) {
// Handle errors during file reading
System.err.println("Error reading input file: " + e.getMessage());
return;
}
// Set up for reading user commands
StringReader keyboard = new StringReader();
String command;
while (true) {
// Read user command from keyboard
command = keyboard.read("Enter next command: ");
// Check for exit command
if (command.equals("exit")) {
System.out.println("Exiting program.");
break;
}
// Process the command
processCommand(command);
}
}
/* Reads the input file and adds the records to the dictionary.
* Each line of the file is expected to have a label and data,
* with the type determined based on the data format.
* @param inputFile The name of the file to be read.
* @throws IOException if there's an error reading the file.
* @throws DictionaryException if there's an error processing the dictionary.
*/
private static void readInputFile(String inputFile) throws IOException, DictionaryException {
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
String label;
while ((label = reader.readLine()) != null) {
String line = reader.readLine();
int type = determineType(line);
String data = line.substring(1).trim();
Record record = new Record(new Key(label.toLowerCase(), type), data);
dictionary.put(record);
}
reader.close();
}
/* Determines the type of a record based on its line format.
* Types are determined by specific prefixes or file extensions in the line.
* @param line The line from the input file to determine the type of.
* @return int representing the type of the record.
*/
private static int determineType(String line) {
if (line.startsWith("-")) {
return 3; // Sound file
} else if (line.startsWith("+")) {
return 4; // Music file
} else if (line.startsWith("*")) {
return 5; // Voice file
} else if (line.startsWith("/")) {
return 2; // Translation
} else if (line.endsWith(".gif")) {
return 7; // Animated image file
} else if (line.endsWith(".jpg")) {
return 6; // Image file
} else if (line.endsWith(".html")) {
return 8; // Webpage URL
}
return 1; // Definition
}
/* Processes a user command by splitting it into parts and handling
* it based on the command type. Supports various commands like "define", "translate",
* "sound", "play", "say", "show", "animate", "browse", "add", "delete", "list",
* "first", "last", and "exit".
*
* @param command The command string entered by the user.
*/
private static void processCommand(String command) {
String[] parts = command.split(" ");
if (parts.length < 2) {
System.out.println("Invalid command");
return;
}
String w = parts[1].toLowerCase();
try {
switch (parts[0]) {
case "define":
handleDefine(w);
break;
case "translate":
handleTranslate(w);
break;
case "sound":
handleSound(w);
break;
case "play":
handlePlay(w);
break;
case "say":
handleSay(w);
break;
case "show":
handleShow(w);
break;
case "animate":
handleAnimate(w);
break;
case "browse":
handleBrowse(w);
break;
case "add":
int t = Integer.parseInt(parts[2]);
String c = combineParts(parts, 3); // Combines all parts starting from index 3
handleAdd(w, t, c);
break;
case "delete":
if (parts.length < 3) {
System.out.println("Invalid command format for delete");
break;
}
w = parts[1];
int k = Integer.parseInt(parts[2]);
handleDelete(w, k);
break;
case "list":
if (parts.length < 2) {
System.out.println("Invalid command format for list");
break;
}
String prefix = parts[1];
handleList(prefix);
break;
case "first":
handleFirst();
break;
case "last":
handleLast();
break;
case "exit":
handleExit();
break;
default:
System.out.println("Invalid command");
}
} catch (Exception e) {
System.err.println("Error processing command: " + e.getMessage());
}
}
/*
* Retrieves and displays the definition of the specified word.
* The word is looked up in the dictionary, and its definition is printed if found.
* @param word The word to define.
*/
private static void handleDefine(String word) {
Record record = dictionary.get(new Key(word, 1));
if (record != null) {
System.out.println(record.getDataItem());
} else {
System.out.println("The word " + word + " is not in the ordered dictionary");
}
}
/*
* Retrieves and displays the translation of the specified word.
* The word is looked up in the dictionary, and its translation is printed if found.
* @param word The word to translate.
*/
private static void handleTranslate(String word) {
Record record = dictionary.get(new Key(word, 2));
if (record != null) {
System.out.println(record.getDataItem());
} else {
System.out.println("There is no definition for the word " + word);
}
}
/*
* Plays the sound file associated with the specified word.
* The word is looked up in the dictionary, and if a sound file is associated,
* it is played using a SoundPlayer.
* @param word The word whose sound file is to be played.
*/
private static void handleSound(String word) {
Record record = dictionary.get(new Key(word, 3));
if (record != null) {
SoundPlayer soundPlayer = new SoundPlayer();
try {
soundPlayer.play(record.getDataItem());
} catch (MultimediaException e) {
System.out.println("There is no sound file for " + word);
} catch (Exception e) {
System.out.println("Error playing sound file: " + e.getMessage());
}
} else {
System.out.println("There is no sound file for " + word);
}
}
/*
* Plays the music file associated with the specified word.
* The word is looked up in the dictionary, and if a music file is associated,
* it is played using a SoundPlayer.
* @param word The word whose music file is to be played.
*/
private static void handlePlay(String word) {
Record record = dictionary.get(new Key(word, 4));
if (record != null) {
SoundPlayer soundPlayer = new SoundPlayer();
try {
soundPlayer.play(record.getDataItem());
} catch (MultimediaException e) {
System.out.println("There is no music file for " + word);
} catch (Exception e) {
System.out.println("Error playing music file: " + e.getMessage());
}
} else {
System.out.println("There is no music file for " + word);
}
}
/*
* Plays the voice file associated with the specified word.
* The word is looked up in the dictionary, and if a voice file is associated,
* it is played using a SoundPlayer.
* @param word The word whose voice file is to be played.
*/
private static void handleSay(String word) {
Record record = dictionary.get(new Key(word, 5));
if (record != null) {
SoundPlayer soundPlayer = new SoundPlayer();
try {
soundPlayer.play(record.getDataItem());
} catch (MultimediaException e) {
System.out.println("There is no voice file for " + word);
} catch (Exception e) {
System.out.println("Error playing voice file: " + e.getMessage());
}
} else {
System.out.println("There is no voice file for " + word);
}
}
/*
* Displays the image file associated with the specified word.
* The word is looked up in the dictionary, and if an image file is associated,
* it is displayed using a PictureViewer.
* @param word The word whose image file is to be shown.
* @throws MultimediaException If there is an issue displaying the image.
*/
private static void handleShow(String word) throws MultimediaException {
Record record = dictionary.get(new Key(word, 6));
if (record != null) {
PictureViewer pictureViewer = new PictureViewer();
pictureViewer.show(record.getDataItem());
}
else {
System.out.println("There is no image file for " + word);
}
}
/*
* Displays the animated image file associated with the specified word.
* The word is looked up in the dictionary, and if an animated image file is associated,
* it is displayed using a PictureViewer.
* @param word The word whose animated image file is to be shown.
* @throws MultimediaException If there is an issue displaying the animated image.
*/
private static void handleAnimate(String word) throws MultimediaException {
Record record = dictionary.get(new Key(word, 7));
if (record != null) {
PictureViewer pictureViewer = new PictureViewer();
pictureViewer.show(record.getDataItem());
}
else {
System.out.println("There is no animated image file for " + word);
}
}
/*
* Opens a browser window to display the webpage associated with the specified word.
* The word is looked up in the dictionary, and if a webpage URL is associated,
* it is opened in a web browser.
* @param word The word whose associated webpage is to be browsed.
* @throws MultimediaException If there is an issue opening the webpage.
*/
private static void handleBrowse(String word) throws MultimediaException {
Record record = dictionary.get(new Key(word, 8));
if (record != null) {
ShowHTML showHTML = new ShowHTML();
showHTML.show(record.getDataItem());
} else {
System.out.println("There is no webpage called " + word);
}
}
/*
* Deletes a record from the dictionary.
* The record is identified using the provided label and type,
* and then removed from the dictionary.
* @param label The label of the record to delete.
* @param type The type of the record to delete.
*/
private static void handleDelete(String label, int type) {
try {
dictionary.remove(new Key(label.toLowerCase(), type));
System.out.println("Record removed successfully");
} catch (DictionaryException e) {
System.out.println("No record in the ordered dictionary has key (" + label + ", " + type + ")");
}
}
/*
* Adds a new record to the dictionary.
* The record is created using the provided label, type, and data,
* and then added to the dictionary.
* @param label The label of the new record.
* @param type The type of the new record.
* @param data The data associated with the new record.
*/
private static void handleAdd(String label, int type, String data) {
try {
dictionary.put(new Record(new Key(label.toLowerCase(), type), data));
System.out.println("A record with the given key (" + label + ", " + type + ") is already in the ordered dictionary");
} catch (DictionaryException e) {
System.out.println("A record with the given key (" + label + ", " + type + ") is already in the ordered dictionary");
}
}
/*
* Lists all records starting with a specific prefix.
* Records in the dictionary that start with the given prefix are listed.
* @param prefix The prefix to search for in the dictionary.
*/
private static void handleList(String prefix) {
List<Record> records = dictionary.getRecordsStartingWith(prefix);
if (records.isEmpty()) {
System.out.println("No label attributes in the ordered dictionary start with prefix " + prefix);
} else {
for (Record record : records) {
System.out.println(record.getKey().getLabel());
}
}
}
/*
* Displays the first record in the dictionary.
* The first record is determined based on some ordering in the dictionary.
*/
private static void handleFirst() {
Record record = dictionary.smallest();
if (record != null) {
System.out.println(record.getKey().getLabel() + "," + record.getKey().getType() + "," + record.getDataItem());
} else {
System.out.println("Dictionary is empty.");
}
}
/*
* Displays the last record in the dictionary.
* The last record is determined based on some ordering in the dictionary.
*/
private static void handleLast() {
Record record = dictionary.largest();
if (record != null) {
System.out.println(record.getKey().getLabel() + "," + record.getKey().getType() + "," + record.getDataItem());
} else {
System.out.println("Dictionary is empty.");
}
}
/*
* Exits the program.
* Terminates the execution of the application.
*/
private static void handleExit() {
System.out.println("Exiting program.");
System.exit(0);
}
/*
* Combines parts of a string array into a single string.
* Useful for reconstructing command arguments split into parts.
* @param parts The array of string parts to combine.
* @param startIndex The starting index in the array to begin combining.
* @return The combined string.
*/
private static String combineParts(String[] parts, int startIndex) {
StringBuilder builder = new StringBuilder();
for (int i = startIndex; i < parts.length; i++) {
if (i > startIndex) {
builder.append(" ");
}
builder.append(parts[i]);
}
return builder.toString();
}
}