?? finding_way.java
字號:
//Finding_way.java
//該類用來計算乘車方案。
import java.net.URL;
import java.sql.*;
public class Finding_way
{
private Beg_end_sta inputstation;//起始站點
private Path [] bestpath;//20個乘車方案
private boolean isfinding;//是否找到可行的乘車方案
private int pathTotal;
private int errState=0;//1 表示沒有直達方案 2表示起點站與終點站為同一站點
//3 表示間接查詢中,起點站不存在 5該車次無車站
//6 倒一次車的中間站點不存在
public Finding_way()
{
}
public Finding_way( Beg_end_sta inputstation,Path [] bestpath, boolean isfinding )
{
this.inputstation=inputstation;
this.bestpath=bestpath;
this.isfinding=isfinding;
}
public Finding_way ( Beg_end_sta inputstation )
{
this.inputstation=inputstation;
}
public void createFindWay(Beg_end_sta inputstation)
{
this.inputstation=new Beg_end_sta();
this.inputstation=inputstation;
this.bestpath=new Path[20];
}
public void createFindWay(Station begining,Station ending )
{
inputstation=new Beg_end_sta();
inputstation.set_be_sta( begining , ending );
bestpath=new Path[20];
for ( int i=0; i<20; i++)
{
bestpath[i]=new Path();
}
}
public void createFindWay(String begining,String ending )
{
inputstation=new Beg_end_sta();
inputstation.set_be_sta( new Station(begining) , new Station (ending) );
bestpath=new Path[20];
for ( int i=0; i<20; i++)
{
bestpath[i]=new Path();
}
}
public void setinputstation (Beg_end_sta inputstation )
{
this.inputstation=inputstation;
}
public Beg_end_sta getinputstation( )
{
return inputstation;
}
public void setbestpath(Path [] bestpath )
{
this.bestpath=bestpath;
}
public Path[] getbestpath()
{
return bestpath;
}
public void setisfinding( boolean isfinding )
{
this.isfinding=isfinding;
}
//給出不倒車直接到達的方案
public boolean direct_find()
{
int i=0;
int resBegSeq,resEndSeq;
int resBegSeqShadow,resEndSeqShadow;
int distance;//起點站到終點站所經(jīng)過的車站數(shù)
String [] resBusNo=new String [20];
String begining,ending;
String queryBusNo,queryBegSeq,queryEndSeq,queryPassSta;
String queryPath;
//String busNo;
begining=inputstation.getBegName();
ending=inputstation.getEndName();
if(begining==ending)
{
//System.out.println("起點和終點是同一站點");
errState=2; //起點和終點是同一站點
return false;
}
queryBusNo="SELECT bus_no FROM bus_table WHERE station_name='" + begining + "' AND bus_no In ( SELECT bus_no FROM bus_table WHERE station_name='" + ending +"')";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:bus");
Statement stmt;
stmt=con.createStatement();
ResultSet resultBusNo ,resultBegSeq,resultEndSeq,resultPassSta;
resultBusNo=stmt.executeQuery( queryBusNo );
//System.out.println(resultBusNo);
//找到從起點直達終點的一系列公交車次
//System.out.println(resultBusNo);
/* if (resultBusNo)//找到合適的函數(shù)來檢查是否有有效數(shù)據(jù)!!!!!!!!!!!!!!!!!
{
System.out.println("無可行方案 ");
errState=1;
return false;
}
*/
if( !resultBusNo.next() )
{
errState=1;
//System.out.println(resBusNo[0]+"dd");
return false;
}
else
{
resBusNo[i]=resultBusNo.getString(1);
bestpath[i].addSegment( resBusNo[i] );
i++;
}
while( resultBusNo.next() && i<20 )
{
resBusNo[i]=resultBusNo.getString(1);
bestpath[i].addSegment( resBusNo[i] );
//System.out.print("s"+resBusNo[i]+"f ");
i++;
}
pathTotal=i;
resultBusNo.close();
//System.out.println(" ");
//System.out.println(" ");
//找到各公交車次中從起點車站到終點車站在整條公交線路中所經(jīng)過的車站
//System.out.println(" i="+i);
for(int j=0;j<i;j++)
{
//起點車站所處的次序
//System.out.print(" 起始車站的序號 ");
queryBegSeq="SELECT station_sequence FROM bus_table WHERE station_name='"+ begining +" ' AND bus_no='"+resBusNo[j]+"' ";
resultBegSeq=stmt.executeQuery(queryBegSeq);
resultBegSeq.next();
resBegSeq=resultBegSeq.getInt("station_sequence");
//System.out.println(resBegSeq);
resultBegSeq.close();
//終點車站所處的次序
//System.out.print(" 終點車站的序號 ");
queryEndSeq="SELECT station_sequence FROM bus_table WHERE station_name='"+ ending +"' AND bus_no='"+resBusNo[j]+"'";
resultEndSeq=stmt.executeQuery(queryEndSeq);
resultEndSeq.next();
resEndSeq=resultEndSeq.getInt("station_sequence");
// System.out.println(resEndSeq);
resultEndSeq.close();
//查找所經(jīng)過的車站
queryPath="";
resBegSeqShadow=resBegSeq;
resEndSeqShadow=resEndSeq;
distance=resEndSeqShadow-resBegSeqShadow;
if(distance>0)
{
queryPath=" ";
}
else
{
queryPath=" DESC ";
}
//System.out.println(queryPath);
queryPath="SELECT station_name FROM bus_table WHERE bus_no='"+resBusNo[j]+"' AND station_sequence BETWEEN " +resBegSeq+" AND "+resEndSeq+" ORDER BY station_sequence " +queryPath;
//System.out.println(queryPath);
resultPassSta=stmt.executeQuery(queryPath);
//ResultSetMetaData rsmd=resultPassSta.getMetaData();
//int numCols=rsmd.getColumnCount();
//System.out.print(" 乘車路線: "+resBusNo[j]+"路車方案 ");
String busStation;
int q=0;
while(resultPassSta.next())
{
busStation=resultPassSta.getString(1);
// System.out.print( busStation+" ");
bestpath[j].setPassSegment( busStation,q,0);
//System.out.print(" ");
q++;
}
//resEndSeq=resultEndSeq.getInt("station_sequence");
//System.out.println(resEndSeq);
resultPassSta.close();
//System.out.println("");
//System.out.println("");
}
//System.out.println(" ");
// System.out.println("Create successfully!");
//resultBegSeq.close();
//resultEndSeq.close();
stmt.close();
con.close();
}
catch(SQLException ex)
{
System.out.println("\n***SQLException Caught ***\n");
while(ex!=null)
{
System.out.println(" SQLState: " +ex.getSQLState() );
System.out.println(" Message: " +ex.getMessage() );
System.out.println("Vendor: "+ex.getErrorCode());
ex=ex.getNextException();
System.out.println(" ");
}
}
catch(java.lang.Exception ex )
{
}
return true;
}
//給出倒一次車的,間接到達方案
public boolean indirect_find()
{
String begining=inputstation.getBegName();
String ending=inputstation.getEndName();
String queBegBusNo="SELECT bus_no FROM bus_table WHERE station_name='"+begining+"'";
String queEndBusNo="SELECT bus_no FROM bus_table WHERE station_name='"+ending+"'";
String [] begBusNo=new String [20];
String [] endBusNo=new String [20];
String [] midBusSta=new String [20];
int i=0,j=0;
//System.out.println(queBegBusNo);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:bus");
Statement stmt;
stmt=con.createStatement();
ResultSet resultBegBusNo,resultEndBusNo;
//經(jīng)過起始站點的線路名稱
resultBegBusNo=stmt.executeQuery( queBegBusNo );
if( !resultBegBusNo.next() )
{
//System.out.println(resBusNo[0]+"dd");
//間接查詢中,起點站不存在,為錯誤三
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -