⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 finding_way.java

📁 公交信息查询
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//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;//起点站到终点站所经过的车站数
             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)//找到合适的函数来检查是否有有效数据!!!!!!!!!!!!!!!!!
                         {
                              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(" ");

                    //找到各公交车次中从起点车站到终点车站在整条公交线路中所经过的车站 
                    //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();
                          //查找所经过的车站
                          
                          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 )
                {
                   ex.printStackTrace();
                }    
         
                   


         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;

                    //经过起始站点的线路名称
                    resultBegBusNo=stmt.executeQuery( queBegBusNo  );
                    
                    if( !resultBegBusNo.next()  )
                                {
                                   //System.out.println(resBusNo[0]+"dd");
                                   //间接查询中,起点站不存在,为错误三

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -