-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestPathToString.java
More file actions
60 lines (50 loc) · 1.77 KB
/
Copy pathShortestPathToString.java
File metadata and controls
60 lines (50 loc) · 1.77 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
import java.util.List;
//--== CS400 File Header Information ==--
//Name: Ryan Szymanski
//Email: rpszymanski@wisc.edu
//Team:BA
//Role: Back End Developer 1
//TA:Brianna
//Lecturer: Florian
//Notes to Grader: <optional extra notes>
public class ShortestPathToString {
interface shortestPath {
String findShortestPath(String flightMap, String prevDes);
}
/**
*
* @param graph Map graph being used
* @param start Starting address
* @param end Destination address
* @return String representation of the shortest path from the start to end addresses, separated
* by lines. Distances for each leg and the total trip are also included.
*/
public static String findShortestPath(CS400Graph<String> graph, String start, String end) {
List<String> flightPath = graph.shortestPath(start, end);
if (flightPath.size() == 2) {
int time = graph.getTime(start, end);
String direct = "This is a direct flight from: " + flightPath.get(0) + " " + flightPath.get(1)
+ " leaving at " + time + ", with a total of " + graph.getWeight(start, end) + " miles.";
return direct;
}
List<String> path = graph.shortestPath(start, end);
//int time = graph.getTime(start, end);
String flightMap = path.get(0);
String prevDes = path.get(0);
path.remove(0);
int[] times=new int[10];
shortestPath returnPath = (String x, String y) -> {
int i=0;
for (String currDes : path) {
times[i]=graph.getTime(y,currDes);
x += " to " + currDes + " (" + graph.getWeight(y, currDes) + " miles) at "+times[i]+ "\n";
y = currDes;
i++;
}
x += "Total distance of your flight: " + graph.getPathCost(start, end)
+ " miles). Leaving at "+times[0];
return x;
};
return returnPath.findShortestPath(flightMap, prevDes);
}
}