-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBusCode.java
More file actions
297 lines (224 loc) · 8.68 KB
/
Copy pathBusCode.java
File metadata and controls
297 lines (224 loc) · 8.68 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
import java.io.Console;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
public class BusCode {
static Graph myGraph;
static int[] stopIDs;
static int[] stopIndices;
public static void main (String[] args) throws FileNotFoundException {
pruneData data = new pruneData();
stopIDs = data.stopIDArray;
stopIndices = data.stopIndicesArray;
// Create Graph;
myGraph = new Graph(stopIDs.length);
data.fillInGraph(myGraph);
String userInput;
System.out.println("\nWelcome user! Please choose what you would like to do: ");
printMainMenu();
while (true) {
userInput = uI();
if (userInput.compareTo("1") == 0) {
searchForStop();
} else if (userInput.compareTo("2") == 0) {
findShortestPath();
} else if (userInput.compareTo("3") == 0) {
arrivalTime();
} else if (isExitCode(userInput)) {
return;
} else {
if (canBeConvertedToInt(userInput)) {
System.out.println(userInput + " is not a valid program.");
continue;
}
System.out.println(userInput + " is not a valid exit code. Enter exit to leave the program.");
continue;
}
printMainMenu();
}
}
// 1.
public static void findShortestPath() throws FileNotFoundException {
int startBusStop = userBusStop("Please enter current bus stop: ");
if (startBusStop == -1) {
return;
}
int endBusStop = userBusStop("Please enter bus stop you wish to arrive at: ");
if (endBusStop == -1) {
return;
}
// Find stop given userInput and using time if it exists:
// Using the TST we have- Dont want to read in files twice, should i read in once as stop objects?
// myGraph.displayGraph();
System.out.println("--------------------");
Bundle<int[], int[], double[]> pair = myGraph.dijkstra(startBusStop, endBusStop, 0, stopIndices);
System.out.println("Path:");
// This might actually need stopIds for hashing back***
myGraph.printPath(endBusStop, pair, startBusStop, stopIndices, stopIDs);
}
// 2.
public static void searchForStop() throws FileNotFoundException {
// Build with stop ID as key
TST<Integer> st = new TST<Integer>();
readInStops(st);
Console cnsl = System.console();
String input = cnsl.readLine("What bus would you like to search for? ").toUpperCase().trim();
if (isExitCode(input)) {
return;
}
if (st.keysWithPrefix(input).isEmpty()) System.out.println("No stops matching your input.");
System.out.println();
}
private static void readInStops(TST<Integer> st) throws FileNotFoundException {
// Read in stops into TST
File file = new File("input files/stops.txt");
Scanner scan = new Scanner(file);
scan.nextLine();
String[] line;
String busName;
while (scan.hasNextLine()) {
line = scan.nextLine().split(",");
busName = pushBackKeywords(line[2].trim());
// add busName to TST
st.put(busName, Integer.valueOf(line[0]));
}
scan.close();
}
private static String pushBackKeywords(String busName) {
String[] line = busName.split("\\s+");
String word;
String busNameEdit = "";
String edits = "";
for (int i = 0; i < line.length; i++) {
word = line[i];
if (word.compareTo("FLAGSTOP") == 0 || word.compareTo("WB") == 0 || word.compareTo("SB") == 0 || word.compareTo("NB") == 0 || word.compareTo("EB") == 0) {
edits += word + " ";
continue;
}
busNameEdit += word + " ";
}
return busNameEdit + edits.trim();
}
// 3. -- Not Complete -- (Trip IDs not sorted)
// Need to add error checking to this mehtod****
// is there a way to use the graph to search for the bus stops because loading them in again is slow?
public static void arrivalTime() throws FileNotFoundException {
Console cnsl = System.console();
String userInput = cnsl.readLine("Please enter arrival time: ").trim();
while (!canBeConvertedToTime(userInput)) {
if (isExitCode(userInput)) {
return;
}
userInput = cnsl.readLine("Sorry, " + userInput + " is not a valid time, please try again: ").trim();
}
Stop givenStop = new Stop(0, 0, userInput);
File file = new File("input files/stop_times.txt");
Scanner scan = new Scanner(file);
// Save them in a linked list- copy over to array and use merge/quick/insertion sort?
LinkedList<Stop> trips = new LinkedList<Stop>();
// Use stop object? ****
String[] line1;
scan.nextLine();
// could create stop objects only for the ones who need to get added to linkedlist to save memory
while (scan.hasNextLine()) {
line1 = scan.nextLine().split(",");
Stop thisStop = new Stop(Integer.valueOf(line1[3].trim()), Integer.valueOf(line1[0].trim()), line1[1].trim());
// is time equal
if (givenStop.isTimeEqual(thisStop)) {
// Add to trips
trips.add(thisStop);
}
}
scan.close();
// copy to an array
Stop[] myStops = new Stop[trips.size()];
int counter = 0;
for (Stop stop : trips) {
myStops[counter] = stop;
counter++;
}
// sort array- quick sort because of small array size?
Arrays.sort(myStops);
for (int i = 0; i < myStops.length; i++) {
myStops[i].displayStopInfo();
}
System.out.println("");
}
// how to integrate this method with error checking method, inputAgrees?
public static int userBusStop(String message) {
String userInput;
Console cnsl = System.console();
userInput = cnsl.readLine("\n" + message);
while (!inputAgrees(userInput)) {
userInput = userInput.trim();
if (isExitCode(userInput)) {
return -1;
}
// Need APPROPRIATE mesasge here
if (canBeConvertedToInt(userInput)) {
userInput = cnsl.readLine("Sorry, the bus stop: " + userInput + " does not exist, please try again. ");
continue;
}
userInput = cnsl.readLine("Sorry, " + userInput + " is not a bus stop, please try again. ");
}
return Integer.valueOf(userInput);
}
// 4. Error Checking
private static boolean isExitCode(String input) {
if (input.toLowerCase().compareTo("exit") == 0) {
System.out.println();
return true;
}
return false;
}
private static boolean canBeConvertedToInt(String input) {
try {
Integer.parseInt( input );
return true;
}
catch( Exception e ) {
return false;
}
}
private static boolean canBeConvertedToTime(String input) {
try {
Stop stop = new Stop(0, 0, input);
if (stop.time.isValid()) {
return true;
}
return false;
}
catch (Exception e) {
return false;
}
}
// method to check that imput is correct format-wise
private static boolean inputAgrees(String input) {
if (!canBeConvertedToInt(input)) {
return false;
}
// Change this to look in the hash table?
try {
if (stopIndices[Integer.valueOf(input)] != 0) {
return true;
}
} catch (IndexOutOfBoundsException i) {
return false; // it's not a valid input
}
return false;
}
private static void printMainMenu() {
// User Interface:
System.out.println("Main Menu:");
System.out.println("Search for bus stop (1)");
System.out.println("Find Shortest Bus route between two stops. (2)");
System.out.println("Find all stops by arrival time (3)");
System.out.println("To exit, type \"exit\"");
}
public static String uI() {
Console cnsl = System.console();
return cnsl.readLine("").trim();
}
}