-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
347 lines (311 loc) · 11.5 KB
/
Copy pathClient.java
File metadata and controls
347 lines (311 loc) · 11.5 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
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* // -------------------------------------------------------------------------
/**
* A class for reading in and executing commands for storing, deleting,
* and searching for records using a Binary Search Tree and PRQuadtree.
*
* @author Joshua Rush
* @author Benjamin Roble
* @version Sep 28, 2011
*/
public class Client
{
//The quadtree the Client uses to manage records using point keys
private static PRQuadTree<CityRecord> qt = new PRQuadTree<CityRecord>();
//The binary Search Tree the Client uses to manage records using name keys
private static BinarySearchTree<String, CityRecord> bst = new BinarySearchTree<String, CityRecord>();
/**
* Create a new Client to handle records and execute a list of commands
* for inserting, removing, and searching those records.
* @param fileName the text file containing the commands.
* @throws FileNotFoundException
*/
public Client(String fileName) throws FileNotFoundException
{
executeCommands(fileName);
}
/**
* Executes the commands for storing and managing records
* specified by an input file.
* @param fileName the text file containing the commands.
* @throws FileNotFoundException
*/
private static void executeCommands(String fileName)
throws FileNotFoundException
{
File file = new File(fileName);
Scanner lineScanner = new Scanner(file);
int x = 0;
//read commands in line by line, parsing and executing them.
try
{
while(lineScanner.hasNext())
{
parseCommand(lineScanner.nextLine());
x++;
}
}
finally
{
lineScanner.close();
}
}
/**
* Strips extra whitespace from a command and determines what it is,
* whether the command be insert, remove, or print, and its arguments.
* @param parseString the single command to be stripped and parsed
*/
private static void parseCommand(String parseString)
{
String trimString = parseString.trim();
StringTokenizer st = new StringTokenizer(trimString, " ");
//using ' ' as a tokenizer, get the first token and determine what the
//command is
if(st.hasMoreTokens()) {
printTokens(trimString);
String command = st.nextToken();
if(command.equalsIgnoreCase("insert"))
parseInsert(st);
else if(command.equalsIgnoreCase("remove"))
parseRemove(st);
else if(command.equalsIgnoreCase("find"))
parseFind(st);
else if(command.equalsIgnoreCase("search"))
parseSearch(st);
else if(command.equalsIgnoreCase("debug"))
debugRequest();
else if(command.equalsIgnoreCase("makenull"))
makeNullRequest();
else
System.out.println("Invalid command '" + command + "'");
}
}
/**
* Print the command given by the string in tokens to avoid extra whitespace.
* @param st the string containing the command to print
*/
public static void printTokens(String st)
{
StringTokenizer printTokenizer = new StringTokenizer(st);
System.out.println();
while(printTokenizer.hasMoreTokens()) {
System.out.print(printTokenizer.nextToken() + " ");
}
System.out.println("\n");
}
/**
* Parses the "insert x y name" command. If it is a valid command
* syntax, the insertion will be processed.
* @param commandToken the command to process separated in a StringTokenizer
*/
private static void parseInsert(StringTokenizer commandToken) {
if(commandToken.countTokens() != 3)
{
System.out.println("Insert commands MUST be in the format " +
"'insert x y name'");
while(commandToken.hasMoreTokens()) {
System.out.print(commandToken.nextToken());
}
return;
}
int x = Integer.parseInt(commandToken.nextToken());
int y = Integer.parseInt(commandToken.nextToken());
String name = commandToken.nextToken();
insertRequest(x, y, name);
}
/**
* insertRequest tries to insert the given parameters into
* the PRQuadTree and BST. Then insertion will fail if the coordinate
* values are negative or above 2^14 - 1, or if a record at those coordinates
* already exists.
* @param x the x coordinate of the Record being inserted
* @param y the y coordinate of the Record being inserted
* @param name the name of this city
*/
private static void insertRequest(int x, int y, String name) {
if (x > qt.MAX_COORD || y > qt.MAX_COORD || x < 0 || y < 0)
{
System.out.println("Insert failed, coordinates are out of bounds");
return;
}
CityRecord rec = new CityRecord(x, y, name);
if(qt.insert(rec, x, y))
{
bst.insert(name, rec);
System.out.println("Insertion successful");
}
else
System.out.println("Insert failed, record with those coordinates" +
" already exists");
}
/**
* Parses the "remove x y name" and "remove name" command. If it is a valid command
* syntax, the removal will be processed.
* @param commandToken the command to process separated in a StringTokenizer
*/
private static void parseRemove(StringTokenizer commandToken) {
int numTokens = commandToken.countTokens();
if(numTokens != 1 && numTokens != 2)
{
System.out.println("Remove commands MUST be in the format " +
"'remove x y' or 'remove name'");
while(commandToken.hasMoreTokens()) {
System.out.print(commandToken.nextToken());
}
return;
}
if(numTokens == 2) {
int x = Integer.parseInt(commandToken.nextToken());
int y = Integer.parseInt(commandToken.nextToken());
removeRequest(x, y);
}
else {
String name = commandToken.nextToken();
removeRequest(name);
}
}
/**
* removeRequest tries to remove a record based on given coordinates.
* Removal fails if the coordinates are negative, or above 2^14 - 1, or
* if the record at that point doesn't exist.
* @param x the x coordinate of the record to remove
* @param y the y coordinate of the record to remove
*/
private static void removeRequest(int x, int y) {
if (x > qt.MAX_COORD || y > qt.MAX_COORD || x < 0 || y < 0)
{
System.out.println("Remove failed, coordinates are out of bounds");
return;
}
CityRecord rec = qt.remove(x, y);
if(rec != null) {
bst.remove(rec.getName());
System.out.println("Removed the city record of "+rec);
}
else
System.out.println("Remove failed, record wasn't found");
}
/**
* removeRequest tries to remove a record given the record's name. The
* removal fails if a record with that name doesn't exist. If multiple
* records with the name exist, only one will be deleted.
* @param name the name of the city to be removed
*/
private static void removeRequest(String name) {
CityRecord rec = bst.remove(name);
if (rec != null)
{
qt.remove(rec.getX(), rec.getY());
System.out.println("Removed the record of "+rec);
}
else
System.out.println("Remove failed, record wasn't found");
}
/**
* Parses the "find name" command. If it is a valid command
* syntax, the find will be processed.
* @param commandToken the command to process separated in a StringTokenizer
*/
private static void parseFind(StringTokenizer commandToken) {
int numTokens = commandToken.countTokens();
if(numTokens != 1)
{
System.out.println("Find commands MUST be in the format " +
"'find name'");
while(commandToken.hasMoreTokens()) {
System.out.print(commandToken.nextToken());
}
return;
}
String name = commandToken.nextToken();
findRequest(name);
}
/**
* findRequest tries to find a record based on the given name
* @param name the name to search by
*/
private static void findRequest(String name) {
List<CityRecord> recList = bst.find(name);
if(recList.size() <= 0) {
System.out.println("Could not find any cities with the name '"+name+"'.");
return;
}
for(CityRecord rec : recList) {
System.out.println("Found the city "+rec.getName() + " located at ("+rec.getX()+", "+rec.getY()+").");
}
}
/**
* Parses the "search x y radius" command. If it is a valid command
* syntax, the search will be processed.
* @param commandToken the command to process separated in a StringTokenizer
*/
private static void parseSearch(StringTokenizer commandToken) {
int numTokens = commandToken.countTokens();
if(numTokens != 3)
{
System.out.println("Search commands MUST be in the format " +
"'search x y radius'");
while(commandToken.hasMoreTokens()) {
System.out.print(commandToken.nextToken());
}
return;
}
int x = Integer.valueOf(commandToken.nextToken());
int y = Integer.valueOf(commandToken.nextToken());
int radius = Integer.valueOf(commandToken.nextToken());
searchRequest(x,y,radius);
}
/**
* searchRequest finds all cities in a given radius from the x,y coordinate
* the absolute value of the x and y must be less than 2^14, and the radius
* must be a positive integer less than 2^14.
* @param x the x coordinate of the search area center
* @param y the y coordinate of the search area center
* @param radius the radius of the search area
*/
private static void searchRequest(int x, int y, int radius) {
if (Math.abs(x) > qt.MAX_COORD || Math.abs(y) > qt.MAX_COORD)
{
System.out.println("Coordinates are out of bounds");
return;
}
if (radius < 0 || radius >= qt.MAX_COORD)
{
System.out.println("Radius value is invalid");
return;
}
ArrayList<CityRecord> recList = qt.search(x, y, radius);
// the QuadTree should print how how many nodes it looked at when searching
if(recList.size() <= 0) {
System.out.println("Could not find any cities within "+radius+" units of ("+x+", "+y+").");
return;
}
for(CityRecord rec : recList) {
System.out.println("Found the city " + rec.getName() +
" located at ("+rec.getX()+", "+rec.getY()+").");
}
}
/**
* Handles the "debug" command.
*/
private static void debugRequest() {
//the QuadTree should print out debug information now
qt.debug();
System.out.println();
}
/**
* Handles the "makenull" command.
*/
private static void makeNullRequest() {
//the QuadTree should print out debug information now
qt.clear();
bst.clear();
System.out.println("Storage is now empty");
}
}