-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphDriver.java
More file actions
289 lines (264 loc) · 9.47 KB
/
Copy pathGraphDriver.java
File metadata and controls
289 lines (264 loc) · 9.47 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
// --== CS400 File Header Information ==--
// Name: Ryan Szymanski
// Email: rpszymanski@wisc.edu
// Team: BA
// Role: Front End Developer 1
// TA: Brianna Cochran
// Lecturer: Florian
// Notes to Grader: None
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class GraphDriver {
public static void main(String[] args) {
System.out.println("Welcome to the GPS Driver!");
CS400Graph<String> graph = new CS400Graph<>();
Scanner sc = new Scanner(System.in);
GraphAddon graphAddon = new GraphAddon();
FileReader fileReader = new FileReader();
char option = 0;
while (option != 'q') {
option = userGuide(sc);
switch (option) {
case 'l':
CS400Graph<String> loadedGPS = loadGPS(sc);
if (loadedGPS == null) {
System.out.println("Unable to load file.");
} else {
graph = loadedGPS;
}
break;
case 'a':
addStudent(sc, graph);
break;
case 'd':
removeAddress(sc, graph);
break;
case 'r':
searchAddress(sc, graph);
break;
case 'e':
deleteRoad(sc, graph);
break;
case 'c':
insertRoad(sc, graph);
break;
case 's':
shortestGPSPath(sc, graph, graphAddon);
break;
case 'h':
helpScreen();
break;
case 'q':
System.out.println("Exiting application.");
break;
default:
System.out.println("Invaild Input! Please try again or type [H] for help.");
break;
}
System.out.println("\n****Press any key to continue****");
sc.nextLine();
}
}
/**
* Method to display the options menu of the GPS
*
* @param sc
* @return the char input
*/
private static char userGuide(Scanner sc) {
System.out.println("Please select what you want to do based on the Characters Below"
+ "\n\t L- Load in a new GPS." + "\n\t A- Updates the GPS and adds a new location to it. "
+ "\n\t D- Updates the GPS and deletes a location."
+ "\n\t R- Search for an address in the GPS."
+ "\n\t E- Updates the GPS and gets rid of a certain road/edge."
+ "\n\t C- Updates the GPS and adds a new road/edge."
+ "\n\t S- Finds the shortest path between two locations." + "\n\t H- Help menu."
+ "\n\t Q- Quit the GPS.");
System.out.print("\nInput: ");
char inputChar = sc.nextLine().toLowerCase().charAt(0);
System.out.print("\n");
return inputChar;
}
/**
* Method that helps load in data from a file to make a new graph
*
* @param sc
* @return the GPS being loaded in
*/
private static CS400Graph<String> loadGPS(Scanner sc) {
System.out.print("File name: ");
String filename = sc.nextLine().trim();
System.out.print("\n");
CS400Graph<String> loadedGPS = null;
try {
loadedGPS = FileReader.importData(filename);
System.out.println("Successfully added GPS data.");
} catch (Exception e) {
System.out.println("Error: \n" + e.getMessage());
}
return loadedGPS;
}
/**
* Method that adds a location to the GPS
*
* @param sc
* @param graph
*/
private static void addStudent(Scanner sc, CS400Graph<String> graph) {
System.out.println("New Address to be added: ");
String name = sc.nextLine().trim();
if (graph.containsVertex(name)) {
System.out.println("This address is already in the GPS!");
return;
}
try {
graph.insertVertex(name);
System.out.println("Successfully added the new address!");
return;
} catch (NullPointerException e) {
System.out.println("Cannot add a null address! Please enter a valid address!");
return;
}
}
/**
* Removes an address from the GPS
*
* @param sc
* @param graph
*/
private static void removeAddress(Scanner sc, CS400Graph<String> graph) {
System.out.println("Address to be removed: ");
String removedAddress = sc.nextLine().trim();
if (!graph.containsVertex(removedAddress)) {
System.out.println("The Address is not in the GPS! Please enter a valid address!");
return;
}
try {
graph.removeVertex(removedAddress);
System.out.println("Successfully removed the address!");
return;
} catch (NullPointerException e) {
System.out.println("Cannot remove a null reference!");
return;
}
}
/**
* Method that Inserts a new road into the GPS connecting two addresses
*
* @param sc
* @param graph
*/
private static void insertRoad(Scanner sc, CS400Graph<String> graph) {
System.out.println("Source Address ");
String startAddress = sc.nextLine().trim();
System.out.println("Target Address ");
String endAddress = sc.nextLine().trim();
if (!graph.containsVertex(startAddress) || !graph.containsVertex(endAddress)) {
System.out.println("One or more of these addresses are not valid! Please try again!");
return;
}
System.out.println("Length of road: ");
int distance;
try {
distance = Integer.parseInt(sc.nextLine().trim());
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please try again and provide a valid integer");
return;
}
if (distance < 0) {
System.out.println("Error: Cannot enter a road with a negative distance!");
}
graph.insertEdge(startAddress, endAddress, distance);
System.out.println("New Road Added!");
return;
}
/**
* Method to delete a road between two addresses
*
* @param sc
* @param graph
*/
private static void deleteRoad(Scanner sc, CS400Graph<String> graph) {
System.out.println("Source Address ");
String startAddress = sc.nextLine().trim();
System.out.println("Target Address ");
String endAddress = sc.nextLine().trim();
if (!graph.containsVertex(startAddress) || !graph.containsVertex(endAddress)) {
System.out.println("One or more of these addresses are not valid! Please try again!");
return;
}
if (graph.removeEdge(startAddress, endAddress)) {
System.out.println("Road successfully removed.");
return;
} else {
System.out.println("Error: There is no road connecting the two address.");
return;
}
}
/**
* Finds and prints out the shortest path between two locations and also tells the user the total
* distance as well.
*
* @param sc
* @param graph
* @param graphAddon
*/
private static void shortestGPSPath(Scanner sc, CS400Graph<String> graph, GraphAddon graphAddon) {
if (graph.isEmpty()) {
System.out.println("The GPS is empty! Please load in valid GPS data!");
}
System.out.println("Start Address ");
String startAddress = sc.nextLine().trim();
System.out.println("End Address ");
String endAddress = sc.nextLine().trim();
if (!graph.containsVertex(startAddress) || !graph.containsVertex(endAddress)) {
System.out.println("One or more of these addresses are not valid! Please try again!");
return;
}
try {
String shortestPath = graphAddon.findShortestPath(graph, startAddress, endAddress);
System.out.println(shortestPath);
return;
} catch (NoSuchElementException e) {
System.out.println("There are no possible routes available!");
return;
}
}
/**
* Method that searches for a specified address
*
* @param sc
* @param graph
*/
private static void searchAddress(Scanner sc, CS400Graph<String> graph) {
System.out.println("Address: ");
String searchedAddress = sc.nextLine().trim();
if (graph.containsVertex(searchedAddress)) {
System.out.println("GPS does contain " + searchedAddress + "!");
} else {
System.out.println("The Address " + searchedAddress + " is not in the GPS.");
}
}
/**
* Method to print help screen for users.
*/
private static void helpScreen() {
System.out.println(
"\n\t[L] Loads in a new GPS by reading a .txt file which contains all locations and roads in the map."
+ "\n"
+ "\n\t[A] Adds a brand new address to the GPS to account for a new building constructed. "
+ "\n\tThis function just takes a new String as the address and inserts it into the GPS as a new vertex. "
+ "\n"
+ "\n\t[D] Updates the GPS by deleting an already existing address from the GPS and also gets rid of all edges/roads"
+ "\n\tconnected to this address. Checks whether or not the address entered is in the GPS, and if so, correctly removes it. "
+ "\n"
+ "\n\t[R] Searches for an address in the GPS by the user typing in a String as the address being serached"
+ "\n\t and checks if it is in the GPS." + "\n"
+ "\n\t[E] Updates the GPS and gets rid of a certain road/edge by the user providing the source vertex and end vertex. "
+ "\n" + "\n\t[C] Updates the GPS and adds a new road/edge "
+ "\n\tby the user providing the source address, the end address, and the distance of the road."
+ "\n"
+ "\n\t[S] Finds the shortest path between two locations that the user specifies.");
}
}