-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulation1.java
190 lines (128 loc) · 4.18 KB
/
Simulation1.java
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
package fedex_assignment;
import java.sql.*;
//import com.mysql.jdbc.PreparedStatement;
import java.util.ArrayList;
public class Simulation1 implements Runnable{
public void run()
{
int i=0;
int k=0;
while (k==0)
{
for(i=1;i<=2500;i++)
{
Connection conn = null;
Statement stmt = null;
String Source=null, Destination=null, Present=null;
int weight=0;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tracking?autoReconnect=true&useSSL=false","root","3jss7137");
//STEP 4: Execute a query
//System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql = "SELECT Source, Destination, Present FROM PackageDetails WHERE trackid ="+i;
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while(rs.next()){
//Retrieve by column name
Source = rs.getString("Source");
Destination = rs.getString("Destination");
Present = rs.getString("Present");
// weight= rs.getInt("Weight");
Main_Activity.mapper.put(i, new FedexPackage(weight,Source,Destination,Present));
}
rs.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
}
k++;
//FedexPackage a=Main_Activity.mapper.get(1);
//System.out.println("1 done");
}
//************************************SIMULATION**********************************************//
while(true)
{
try {
for(i=1;i<=2500;i++)
{
int srcno = 0,destno = 0,weight=0;
FedexPackage pack = new FedexPackage();
//FedexPackage pack2=new FedexPackage();
pack= Main_Activity.mapper.get(i);
//pack2=Main_Activity.mapper.get(50);
Dijkstra path=new Dijkstra();
// String src = null;
// System.out.println(pack2.getDestination());
// System.out.println("Present "+pack2.getPresent());
//System.out.println("ssssssssssssssss");
//String dest = ;
ArrayList<Integer> shortest=new ArrayList<Integer>();
String[] cityarray ={"Northborough, MA","Edison, NJ","Pittsburgh, PA","Allentown, PA","Martinsburg, WV","Charlotte, NC","Atlanta, GA","Orlando, FL","Memphis, TN","Grove City, OH","Indianapolis, IN","Detroit, MI","New Berlin, WI", "Minneapolis, MN", "St. Louis, MO", "Kansas, KS", "Dallas, TX", "Houston, TX","Denver, CO","Salt Lake City, UT","Phoenix, AZ","Los Angeles, CA","Chino, CA","Sacramento, CA","Seattle, WA"};
k=0;
if(pack!=null){
for(String s:cityarray ){
if(s.equals(pack.getSource()))
srcno=k;
else
k++;
}
k=0;
for(String s:cityarray ){
if(s.equals(pack.getDestination()))
destno=k;
else
k++;
}
}
shortest= path.getShortestPath(srcno, destno);
// for ( int f:shortest)
// {
// System.out.println(path.cityName(f));
// }
FedexPackage packet=new FedexPackage();
packet=Main_Activity.mapper.get(i);
/*if(packet.getPresent()==packet.getDestination())
{
//System.out.println("here");
break;
} */
if (packet!=null)
{
if (Time_Thread.counter<shortest.size())
{
//System.out.println(packet.getPresent()+" "+packet.getDestination()+Time_Thread.counter);
Main_Activity.mapper.put(i, new FedexPackage(weight, packet.getSource(),packet.getDestination(),cityarray[shortest.get(Time_Thread.counter)]));
}
else
{
//System.out.println("here");
Main_Activity.mapper.put(i, new FedexPackage(weight, packet.getSource(),packet.getDestination(),packet.getDestination()));
}
}
}
} catch (Exception e) {
System.out.println("Exception occured");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}}