合作人:张鑫华
已实现功能:能够实现寻找最短路径,同时到页面上,以及图片放大。
未实现的功能:页面不美观,需要美化页面
实现功能重要代码:
1 public List<User>Num5(String a,String b) 2 { 3 int NN=0; 4 User start=new User(); 5 User end=new User(); 6 start=loadUser(a); 7 end=loadUser(b); 8 conn=ut.getConn(); 9 ps=null; 10 ResultSet rs=null; 11 String id; 12 sql="select * from station where Id between ? and ? order by Id"; 13 List<User> users=new ArrayList<User>(); 14 List<User> Mid1s=new ArrayList<User>();//做出一个始发站中转站点集 15 List<User> Mid2s=new ArrayList<User>();//做出一个终点站中转站点集 16 List<User> Mid3s=new ArrayList<User>();//1在转战线上的位置 17 List<User> Mid4s=new ArrayList<User>();//2在转战上的位置 18 User user=null; 19 User Mid1=null; 20 User Mid2=null; 21 User Mid3=null; 22 User Mid4=null; 23 24 int Zhuan;//最近的转战是第几条线 25 if(start.getLine()==end.getLine())//一条线 26 { 27 if(start.getId()<=end.getId()) 28 { 29 try { 30 conn=ut.getConn(); 31 ps=conn.prepareStatement(sql); 32 ps.setInt(1, start.getId()); 33 ps.setInt(2, end.getId()); 34 35 rs=ps.executeQuery(); 36 while(rs.next()) { 37 user=new User(); 38 user.setName(rs.getString("Name")); 39 user.setId(rs.getInt("Id")); 40 user.setLine(rs.getInt("Line")) ; 41 user.setNum(rs.getInt("Num")); 42 user.setExchange(rs.getInt("Exchange")); 43 users.add(user); 44 } 45 }catch(SQLException e) { 46 e.printStackTrace(); 47 } 48 } 49 else 50 { 51 52 try { 53 sql="select * from station where Id between ? and ? order by Id desc"; 54 conn=ut.getConn(); 55 ps=conn.prepareStatement(sql); 56 ps.setInt(2, start.getId()); 57 ps.setInt(1, end.getId()); 58 rs=ps.executeQuery(); 59 while(rs.next()) { 60 user=new User(); 61 user.setName(rs.getString("Name")); 62 user.setId(rs.getInt("Id")); 63 user.setLine(rs.getInt("Line")) ; 64 user.setNum(rs.getInt("Num")); 65 user.setExchange(rs.getInt("Exchange")); 66 users.add(user); 67 } 68 }catch(SQLException e) { 69 e.printStackTrace(); 70 } 71 } 72 73 74 } 75 if(start.getLine()!=end.getLine())//不在一条线 76 { 77 int mid=0; 78 try { 79 80 sql="select * from station where Line=? and Exchange !=?";//始发站一条线 81 conn=ut.getConn(); 82 ps=conn.prepareStatement(sql); 83 ps.setInt(1, start.getLine()); 84 ps.setInt(2, 0); 85 rs=ps.executeQuery(); 86 while(rs.next()) 87 { 88 Mid1=new User(); 89 Mid1.setName(rs.getString("Name")); 90 Mid1.setId(rs.getInt("Id")); 91 Mid1.setLine(rs.getInt("Line")) ; 92 Mid1.setNum(rs.getInt("Num")); 93 Mid1.setExchange(rs.getInt("Exchange")); 94 Mid1s.add(Mid1); 95 } 96 for(User mid1:Mid1s) 97 { 98 sql="select * from station where Name=?";//同名 但是Id 不一样 99 conn=ut.getConn(); 100 ps=conn.prepareStatement(sql); 101 ps.setString(1, mid1.getName()); 102 rs=ps.executeQuery(); 103 while(rs.next()) 104 { 105 Mid3=new User(); 106 Mid3.setName(rs.getString("Name")); 107 Mid3.setId(rs.getInt("Id")); 108 Mid3.setLine(rs.getInt("Line")) ; 109 Mid3.setNum(rs.getInt("Num")); 110 Mid3.setExchange(rs.getInt("Exchange")); 111 if(Mid3.getId()!=mid1.getId()) 112 { 113 Mid3s.add(Mid3); 114 } 115 } 116 } 117 sql="select * from station where Line=? and Exchange !=?"; //找到在终点站那条线路上 118 conn=ut.getConn(); 119 ps=conn.prepareStatement(sql); 120 ps.setInt(2, start.getLine()); 121 ps.setInt(1, end.getLine()); 122 rs=ps.executeQuery(); 123 while(rs.next()) 124 { 125 Mid2=new User(); 126 Mid2.setName(rs.getString("Name")); 127 Mid2.setId(rs.getInt("Id")); 128 Mid2.setLine(rs.getInt("Line")) ; 129 Mid2.setNum(rs.getInt("Num")); 130 Mid2.setExchange(rs.getInt("Exchange")); 131 Mid2s.add(Mid2); 132 } 133 for(User mid2:Mid2s) 134 { 135 sql="select * from station where Name=?";//同名 但是Id 不一样 136 conn=ut.getConn(); 137 ps=conn.prepareStatement(sql); 138 ps.setString(1, mid2.getName()); 139 rs=ps.executeQuery(); 140 while(rs.next()) 141 { 142 Mid4=new User(); 143 Mid4.setName(rs.getString("Name")); 144 Mid4.setId(rs.getInt("Id")); 145 Mid4.setLine(rs.getInt("Line")) ; 146 Mid4.setNum(rs.getInt("Num")); 147 Mid4.setExchange(rs.getInt("Exchange")); 148 if(Mid4.getId()!=mid2.getId()) 149 { 150 Mid4s.add(Mid4); 151 } 152 } 153 } 154 155 156 157 int min=100;//选出最近转战口 158 int Min1=Mid1.getId();//最近转战口ID 与始发站相同 159 int Min2=Mid2.getId();//与终点站相同 160 int Min3=Mid3.getId(); 161 int Min4=Mid4.getId(); 162 for(User mid1:Mid1s) 163 { 164 for(User mid3:Mid3s) 165 { 166 for(User mid2:Mid2s) 167 { 168 for(User mid4:Mid4s) 169 { 170 if((mid1.getExchange()==mid2.getExchange())&&(mid3.getLine()==mid4.getLine())&&(mid1.getExchange()==mid3.getLine())) 171 { 172 173 int n=Math.abs(mid1.getId()-start.getId())+Math.abs(mid2.getId()-end.getId())+Math.abs(mid3.getId()-mid4.getId()); 174 if(min>n) 175 { 176 min=n; 177 Min1=mid1.getId(); 178 Min2=mid2.getId(); 179 Min3=mid3.getId(); 180 Min4=mid4.getId(); 181 } 182 } 183 } 184 185 } 186 } 187 } 188 System.out.println(Min1); 189 User mid1=Select_Id(Min1); 190 User mid2=Select_Id(Min2); 191 User mid3=Select_Id(Min3); 192 User mid4=Select_Id(Min4); 193 System.out.println(mid2.getName()+"asdfasdfasdgasdgasdf"+mid1.getName());//////////// 194 if(mid1.getId()>start.getId())//始发站->中转站 195 { 196 sql="select * from station where Id between ? and ? order by Id"; 197 conn=ut.getConn(); 198 ps=conn.prepareStatement(sql); 199 ps.setInt(1, start.getId()); 200 ps.setInt(2, mid1.getId()); 201 rs=ps.executeQuery(); 202 while(rs.next()) { 203 NN=NN+1; 204 user=new User(); 205 user.setName(rs.getString("Name")); 206 user.setId(rs.getInt("Id")); 207 user.setLine(rs.getInt("Line")) ; 208 user.setNum(rs.getInt("Num")); 209 user.setExchange(rs.getInt("Exchange")); 210 users.add(user); 211 } 212 if(mid3.getId()>mid4.getId())//3->4 213 { 214 sql="select * from station where Id between ? and ? order by Id desc"; 215 conn=ut.getConn(); 216 ps=conn.prepareStatement(sql); 217 ps.setInt(1,mid4.getId()); 218 ps.setInt(2, mid3.getId()-1);///////////////////// 219 rs=ps.executeQuery(); 220 while(rs.next()) { 221 NN=NN+1; 222 user=new User(); 223 user.setName(rs.getString("Name")); 224 user.setId(rs.getInt("Id")); 225 user.setLine(rs.getInt("Line")) ; 226 user.setNum(rs.getInt("Num")); 227 user.setExchange(rs.getInt("Exchange")); 228 users.add(user); 229 } 230 } 231 if(mid3.getId()<mid4.getId())//3->4 232 { 233 sql="select * from station where Id between ? and ? order by Id "; 234 conn=ut.getConn(); 235 ps=conn.prepareStatement(sql); 236 ps.setInt(1,mid3.getId()+1); 237 ps.setInt(2, mid4.getId()); 238 rs=ps.executeQuery(); 239 while(rs.next()) { 240 NN=NN+1; 241 user=new User(); 242 user.setName(rs.getString("Name")); 243 user.setId(rs.getInt("Id")); 244 user.setLine(rs.getInt("Line")) ; 245 user.setNum(rs.getInt("Num")); 246 user.setExchange(rs.getInt("Exchange")); 247 users.add(user); 248 } 249 } 250 if(mid2.getId()>end.getId())//始发站->中转站 终点站->中转站 251 { 252 System.out.println(""); 253 sql="select * from station where Id between ? and ? order by Id desc"; 254 conn=ut.getConn(); 255 ps=conn.prepareStatement(sql); 256 ps.setInt(1, end.getId()); 257 ps.setInt(2, mid2.getId()-1); 258 rs=ps.executeQuery(); 259 while(rs.next()) { 260 NN=NN+1; 261 user=new User(); 262 user.setName(rs.getString("Name")); 263 user.setId(rs.getInt("Id")); 264 user.setLine(rs.getInt("Line")) ; 265 user.setNum(rs.getInt("Num")); 266 user.setExchange(rs.getInt("Exchange")); 267 users.add(user); 268 } 269 } 270 else//始发站->中转站->终点站 271 { 272 sql="select * from station where Id between ? and ? order by Id "; 273 conn=ut.getConn(); 274 ps=conn.prepareStatement(sql); 275 ps.setInt(2, end.getId()); 276 ps.setInt(1, mid2.getId()+1); 277 rs=ps.executeQuery(); 278 while(rs.next()) { 279 NN=NN+1; 280 user=new User(); 281 user.setName(rs.getString("Name")); 282 user.setId(rs.getInt("Id")); 283 user.setLine(rs.getInt("Line")) ; 284 user.setNum(rs.getInt("Num")); 285 user.setExchange(rs.getInt("Exchange")); 286 users.add(user); 287 } 288 } 289 290 } 291 else//中转站<-始发站 292 { 293 294 sql="select * from station where Id between ? and ? order by Id desc"; 295 conn=ut.getConn(); 296 ps=conn.prepareStatement(sql); 297 ps.setInt(2, start.getId()); 298 ps.setInt(1, mid1.getId()); 299 rs=ps.executeQuery(); 300 while(rs.next()) { 301 NN=NN+1; 302 user=new User(); 303 user.setName(rs.getString("Name")); 304 user.setId(rs.getInt("Id")); 305 user.setLine(rs.getInt("Line")) ; 306 user.setNum(rs.getInt("Num")); 307 user.setExchange(rs.getInt("Exchange")); 308 users.add(user); 309 } 310 if(mid3.getId()>mid4.getId())//3->4 311 { 312 sql="select * from station where Id between ? and ? order by Id desc"; 313 conn=ut.getConn(); 314 ps=conn.prepareStatement(sql); 315 ps.setInt(1,mid4.getId()); 316 ps.setInt(2, mid3.getId()-1); 317 rs=ps.executeQuery(); 318 while(rs.next()) { 319 NN=NN+1; 320 user=new User(); 321 user.setName(rs.getString("Name")); 322 user.setId(rs.getInt("Id")); 323 user.setLine(rs.getInt("Line")) ; 324 user.setNum(rs.getInt("Num")); 325 user.setExchange(rs.getInt("Exchange")); 326 users.add(user); 327 } 328 } 329 if(mid3.getId()<mid4.getId())//3->4 330 { 331 sql="select * from station where Id between ? and ? order by Id "; 332 conn=ut.getConn(); 333 ps=conn.prepareStatement(sql); 334 ps.setInt(1,mid3.getId()+1); 335 ps.setInt(2, mid4.getId()); 336 rs=ps.executeQuery(); 337 while(rs.next()) { 338 NN=NN+1; 339 user=new User(); 340 user.setName(rs.getString("Name")); 341 user.setId(rs.getInt("Id")); 342 user.setLine(rs.getInt("Line")) ; 343 user.setNum(rs.getInt("Num")); 344 user.setExchange(rs.getInt("Exchange")); 345 users.add(user); 346 } 347 } 348 349 if(mid2.getId()>end.getId())//中转站<-始发站 终点站->中转站 350 { 351 sql="select * from station where Id between ? and ? order by Id desc"; 352 conn=ut.getConn(); 353 ps=conn.prepareStatement(sql); 354 ps.setInt(1, end.getId()); 355 ps.setInt(2, mid2.getId()-1); 356 rs=ps.executeQuery(); 357 while(rs.next()) { 358 NN=NN+1; 359 user=new User(); 360 user.setName(rs.getString("Name")); 361 user.setId(rs.getInt("Id")); 362 user.setLine(rs.getInt("Line")) ; 363 user.setNum(rs.getInt("Num")); 364 user.setExchange(rs.getInt("Exchange")); 365 users.add(user); 366 } 367 } 368 else//中转站<-始发站 369 { 370 sql="select * from station where Id between ? and ? order by Id "; 371 conn=ut.getConn(); 372 ps=conn.prepareStatement(sql); 373 ps.setInt(2, end.getId()); 374 ps.setInt(1, mid2.getId()+1); 375 rs=ps.executeQuery(); 376 while(rs.next()) { 377 NN=NN+1; 378 user=new User(); 379 user.setName(rs.getString("Name")); 380 user.setId(rs.getInt("Id")); 381 user.setLine(rs.getInt("Line")) ; 382 user.setNum(rs.getInt("Num")); 383 user.setExchange(rs.getInt("Exchange")); 384 users.add(user); 385 } 386 } 387 388 } 389 390 391 392 393 394 395 396 397 }catch(SQLException e) { 398 e.printStackTrace(); 399 } 400 401 402 403 404 405 406 } 407 System.out.println("aadfasdfsdaf"+NN+"adsfads"+Num1(a,b)); 408 if(NN>Num1(a,b)) 409 { 410 return Num3(a,b); 411 } 412 else 413 { 414 return users; 415 } 416 }
太过于冗杂,有时间一定要优化