-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoutes.java
More file actions
56 lines (48 loc) · 1.5 KB
/
Routes.java
File metadata and controls
56 lines (48 loc) · 1.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
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
public class Routes {
public static void main(String args[]) throws IOException
{
ReadFile rfObj = new ReadFile(args[0]);
HashMap<String,Streets> hm = rfObj.read();
Routes rt = new Routes();
String[] result = rt.calculateTimeDistance(hm,rfObj);
String finalTime= result[0];
String finalDistance = result[1];
System.out.println("Distance:"+ finalDistance+"\n"+ "Time: "+finalTime);
}
public String[] calculateTimeDistance(HashMap<String,Streets> hm2,ReadFile rf) throws IOException
{
Iterator<String> iterator = hm2.keySet().iterator();
String[] result=new String[2];
String time=" hrs";
String dist=" mi";
double totalDistance=0;
double totalTime=0;
while(iterator.hasNext())
{
String sname = iterator.next();
Streets st = hm2.get(sname);
totalDistance+=st.getsLength();
double travelTime=(st.getsLength()/st.getSpeedLimit());
if(st.getsType().equals("H")&&travelTime<1/60)
travelTime=1/60;
totalTime+=travelTime;
}
System.out.println("Want the final distance in Miles or KM ? M or K");
BufferedReader buf =rf.getUserInput();
String uip = buf.readLine();
if(uip.equals("K"))
{
dist=" km";
totalDistance*=1.609;
}
double finalDistance = Math.round( totalDistance * 100.0 ) / 100.0;
double finalTime = Math.round( totalTime * 100.0 ) / 100.0;
result[0]=finalTime+time;
result[1]=finalDistance+dist;
return result;
}
}