zoukankan      html  css  js  c++  java
  • 个人作业4 结对开发地铁

    1.开发一套石家庄地铁线路查询系统。

    2.功能设计

    (1)数据库设计:将石家庄地铁线路图的各个线路,各个站点,换乘信息等用数据库的形式保存起来,应该保存的信息有 {线路号,线路的各个站名,车站的换乘信息}。

    (2)站点查询:用户可以输入任一一条线路或输入出发地和目的地信息,可以查询到相关内容。

    例如输入出发地:石家庄铁道大学   目的地 博物院

    返回经历的站名的个数,和路径,如果有换乘,请列出换乘的线路

     

    设计思想:

      将所有的站点可分为两类:一种是只在一条线路上(普通点),一种是可在两条线路上,即为两条线路交点(换乘点)。

            所以可以分为3种情况:

      ①:起始点:普通点   终点:普通点

      ②:起始点:普通点   终点:换乘点      或     起始点:换乘点   终点:普通点

      ③:起始点:换乘点    终点:换乘点

            以上每种情况都可以分为以下几种:直达,一次换乘,二次换乘,多次换乘(此种情况很少,所以此次试验中并未考虑)。直达:通过得到的起始站和终点站的两个站名,获取它们的id,

    然后查询到两站之间的站名存入数组输出即可。一次换乘:找到起始站和终点站所在线路的交点,然后转化成求两段直达线路。二次换乘,找到起始站两侧最近的中转站A和B(若两侧都存在都要考虑),

    以A为例,计算起始站与A的直达线路,再计算A与终点站的一次换乘。

    LoginerDaolmpl.java

    package com.demo;
    import util.util;
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    import javax.swing.plaf.synth.SynthSpinnerUI;
    
    public class LoginerDaolmpl {
        static Connection conn;
        static PreparedStatement ps = null;
        static ResultSet rs;
        static String sql = "select * from station";
        static util ut= new util();
        static Scanner in = new Scanner(System.in);
        
        public User loadUser(String a) {
            conn=ut.getConn();
            ps=null;
            ResultSet rs=null;
            User user=null;
            sql="select * from station where Name=?";
            try {
                ps=conn.prepareStatement(sql);
                ps.setString(1, a);
                rs=ps.executeQuery();
                if(rs.next()) {
                    user=new User();
                    user.setName(rs.getString("Name"));
                    user.setId(rs.getInt("Id"));
                    user.setLine(rs.getInt("Line")) ;
                    user.setNum(rs.getInt("Num"));
                    user.setExchange(rs.getInt("Exchange"));
                }
            }catch(SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                     if(ps!=null)ps.close();
                     if(conn!=null)conn.close();
                 }catch(Exception e2) {
                     e2.printStackTrace();
                 }
            }
            return user;
        }
        public User Select_Id(int a) {
            conn=ut.getConn();
            ps=null;
            ResultSet rs=null;
            User user=null;
            sql="select * from station where Id=?";
            try {
                ps=conn.prepareStatement(sql);
                ps.setInt(1, a);
                rs=ps.executeQuery();
                if(rs.next()) {
                    user=new User();
                    user.setName(rs.getString("Name"));
                    user.setId(rs.getInt("Id"));
                    user.setLine(rs.getInt("Line")) ;
                    user.setNum(rs.getInt("Num"));
                    user.setExchange(rs.getInt("Exchange"));
                }
            }catch(SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                     if(ps!=null)ps.close();
                     if(conn!=null)conn.close();
                 }catch(Exception e2) {
                     e2.printStackTrace();
                 }
            }
            return user;
        }
        
        public List<User> load(){
            conn=ut.getConn();
            ps=null;
            ResultSet rs=null;
            String id;
            sql="select * from station order by Id ";
    //        sql="select * from station where Id between ? and ? order by Id";
            List<User> users=new ArrayList<User>();
            User user=null;
            try {
                ps=conn.prepareStatement(sql);
                rs=ps.executeQuery();
                while(rs.next()) {
                    user=new User();
                    user.setName(rs.getString("Name"));
                    user.setId(rs.getInt("Id"));
                    user.setLine(rs.getInt("Line")) ;
                    user.setNum(rs.getInt("Num"));
                    user.setExchange(rs.getInt("Exchange"));
                    users.add(user);
                }
            }catch(SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                     if(ps!=null)ps.close();
                     if(conn!=null)conn.close();
                 }catch(Exception e2) {
                     e2.printStackTrace();
                 }
            }
            return users;
        }
        //只能返回站数,能判断一条线上,有两个转战口,但不能判断 三条最近 
        public int Num1(String a,String b)
        {
            User start=new User();
            start=loadUser(a);
            User end=new User();
            end=loadUser(b);
            User Mid=new User();
            int num=0;
            if(start.getLine()==end.getLine())
            {
            num=Math.abs(start.getNum()-end.getNum());
            
            }
            if(start.getLine()==1&&end.getLine()==2)
            {
                int N=0,M=0;
                N=Math.abs(start.getNum()-4)+Math.abs(end.getNum()-6);
                M=Math.abs(start.getNum()-20)+Math.abs(end.getNum()-20);
                if(N>M)num=M;
                else num=N;
            }
            if(start.getLine()==1&&end.getLine()==3)
            {
                num=Math.abs(start.getNum()-23)+Math.abs(end.getNum()-13);
            }
            if(start.getLine()==1&&end.getLine()==4)
            {
                num=Math.abs(start.getNum()-17)+Math.abs(end.getNum()-13);
            }
            if(start.getLine()==1&&end.getLine()==5)
            {
                int N=0,M=0;
                N=Math.abs(start.getNum()-10)+Math.abs(end.getNum()-19);
                M=Math.abs(start.getNum()-25)+Math.abs(end.getNum()-7);
                if(N>M)num=M;
                else num=N;
            }
            if(start.getLine()==1&&end.getLine()==6)
            {
                num=Math.abs(start.getNum()-13)+Math.abs(end.getNum()-14);
            }
            if(start.getLine()==2&&end.getLine()==1)
            {
                int N=0,M=0;
                N=Math.abs(start.getNum()-6)+Math.abs(end.getNum()-4);
                M=Math.abs(start.getNum()-20)+Math.abs(end.getNum()-20);
                if(N>M)num=M;
                else num=N;
            }
            if(start.getLine()==2&&end.getLine()==3)
            {
                num=Math.abs(start.getNum()-25)+Math.abs(end.getNum()-17);
            }
            if(start.getLine()==2&&end.getLine()==4)
            {
                num=Math.abs(start.getNum()-27)+Math.abs(end.getNum()-5);
            }
            if(start.getLine()==2&&end.getLine()==5)
            {
                num=Math.abs(start.getNum()-18)+Math.abs(end.getNum()-13);
            }
            if(start.getLine()==2&&end.getLine()==6)
            {
                num=Math.abs(start.getNum()-22)+Math.abs(end.getNum()-8);
            }
            if(start.getLine()==3&&end.getLine()==1)
            {
                num=Math.abs(start.getNum()-13)+Math.abs(end.getNum()-23);
            }
            if(start.getLine()==3&&end.getLine()==2)
            {
                num=Math.abs(start.getNum()-17)+Math.abs(end.getNum()-25);
            }
            if(start.getLine()==3&&end.getLine()==4)
            {
                num=Math.abs(start.getNum()-22)+Math.abs(end.getNum()-9);
            }
            if(start.getLine()==3&&end.getLine()==5)
            {
                num=Math.abs(start.getNum()-11)+Math.abs(end.getNum()-11);
            }
            if(start.getLine()==3&&end.getLine()==6)
            {
                int N=0,M=0;
                N=Math.abs(start.getNum()-26)+Math.abs(end.getNum()-16);
                M=Math.abs(start.getNum()-15)+Math.abs(end.getNum()-5);
                if(N>M)num=M;
                else num=N;
            }
            if(start.getLine()==4&&end.getLine()==1)
            {
                num=Math.abs(start.getNum()-13)+Math.abs(end.getNum()-17);
            }
            if(start.getLine()==4&&end.getLine()==2)
            {
                num=Math.abs(start.getNum()-5)+Math.abs(end.getNum()-27);
            }
            if(start.getLine()==4&&end.getLine()==3)
            {
                num=Math.abs(start.getNum()-9)+Math.abs(end.getNum()-22);
            }
            if(start.getLine()==4&&end.getLine()==5)
            {
                int N=0,M=0;
                N=Math.abs(start.getNum()-15)+Math.abs(end.getNum()-15);
                M=Math.abs(start.getNum()-3)+Math.abs(end.getNum()-2);
                if(N>M)num=M;
                else num=N;
            }
            if(start.getLine()==4&&end.getLine()==6)
            {
                num=Math.abs(start.getNum()-11)+Math.abs(end.getNum()-11);
            }
            if(start.getLine()==5&&end.getLine()==1)
            {
                int N=0,M=0;
                N=Math.abs(start.getNum()-19)+Math.abs(end.getNum()-10);
                M=Math.abs(start.getNum()-25)+Math.abs(end.getNum()-7);
                if(N>M)num=M;
                else num=N;
            }
            if(start.getLine()==5&&end.getLine()==2)
            {
                num=Math.abs(start.getNum()-13)+Math.abs(end.getNum()-18);
            }
            if(start.getLine()==5&&end.getLine()==3)
            {
                num=Math.abs(start.getNum()-11)+Math.abs(end.getNum()-11);
            }
            if(start.getLine()==5&&end.getLine()==4  )
            {
                int N=0,M=0;
                N=Math.abs(start.getNum()-19)+Math.abs(end.getNum()-10);
                M=Math.abs(start.getNum()-25)+Math.abs(end.getNum()-7);
                if(N>M)num=M;
                else num=N;
            }
            if(start.getLine()==5&&end.getLine()==6)
            {
                num=Math.abs(start.getNum()-4)+Math.abs(end.getNum()-3);
            }
            if(start.getLine()==6&&end.getLine()==1)
            {
                num=Math.abs(start.getNum()-14)+Math.abs(end.getNum()-13);
            }
            if(start.getLine()==6&&end.getLine()==2)
            {
                num=Math.abs(start.getNum()-8)+Math.abs(end.getNum()-22);
            }
            if(start.getLine()==6&&end.getLine()==3  )
            {
                int N=0,M=0;
                N=Math.abs(start.getNum()-16)+Math.abs(end.getNum()-26);
                M=Math.abs(start.getNum()-5)+Math.abs(end.getNum()-15);
                if(N>M)num=M;
                else num=N;
            }
            if(start.getLine()==6&&end.getLine()==4)
            {
                num=Math.abs(start.getNum()-11)+Math.abs(end.getNum()-11);
            }
            if(start.getLine()==6&&end.getLine()==5)
            {
                num=Math.abs(start.getNum()-3)+Math.abs(end.getNum()-4);
            }
            System.out.println(num);
            return num;
        }
    
        //只能返回站名,不能判断两个转战口,同时不能判断三条最近
        
        public List<User> Num2(String a,String b)
        {
        User start=new User();
        User end=new User();
        start=loadUser(a);
        end=loadUser(b);
        conn=ut.getConn();
        ps=null;
        ResultSet rs=null;
        String id;
        sql="select * from station where Id between ? and ? order by Id";
        List<User> users=new ArrayList<User>();
        User user=null;
        
        if(start.getLine()==end.getLine())//一条线
        {
            if(start.getId()<=end.getId())
            {
                try {
            ps=conn.prepareStatement(sql);
            ps.setInt(1, start.getId());
            ps.setInt(2, end.getId());
            
            rs=ps.executeQuery();
            while(rs.next()) {
                user=new User();
                user.setName(rs.getString("Name"));
                user.setId(rs.getInt("Id"));
                user.setLine(rs.getInt("Line")) ;
                user.setNum(rs.getInt("Num"));
                user.setExchange(rs.getInt("Exchange"));
                users.add(user);
            }
        }catch(SQLException e) {
            e.printStackTrace();
        }
        }
            else
            {
                
                try {
                    sql="select * from station where Id between ? and ? order by Id desc";
                    ps=conn.prepareStatement(sql);
                    ps.setInt(2, start.getId());
                    ps.setInt(1, end.getId());
                    rs=ps.executeQuery();
                    while(rs.next()) {
                        user=new User();
                        user.setName(rs.getString("Name"));
                        user.setId(rs.getInt("Id"));
                        user.setLine(rs.getInt("Line")) ;
                        user.setNum(rs.getInt("Num"));
                        user.setExchange(rs.getInt("Exchange"));
                        users.add(user);
                    }
                }catch(SQLException e) {
                    e.printStackTrace();
                }
            }
            
            
        }
        if(start.getLine()!=end.getLine())//不在一条线
        {
            System.out.println(start.getLine()+end.getLine());
            int mid=0;
            try {
                
                sql="select * from station where Line=? and Exchange=?";
                ps=conn.prepareStatement(sql);
                ps.setInt(1, start.getLine());
                ps.setInt(2, end.getLine());
                rs=ps.executeQuery();
                User Mid=new User();//找到中间值(在始发站那条线路上)
                if(rs.next())
                {
                Mid.setName(rs.getString("Name"));
                Mid.setId(rs.getInt("Id"));
                Mid.setLine(rs.getInt("Line")) ;
                Mid.setNum(rs.getInt("Num"));
                Mid.setExchange(rs.getInt("Exchange"));
                }
                sql="select * from station where Line=? and Exchange=?";  //找到在终点站那条线路上
                ps=conn.prepareStatement(sql);
                ps.setInt(2, start.getLine());
                ps.setInt(1, end.getLine());
                rs=ps.executeQuery();
                User Mid2=new User();
                if(rs.next())
                {
                Mid2.setName(rs.getString("Name"));
                Mid2.setId(rs.getInt("Id"));
                Mid2.setLine(rs.getInt("Line")) ;
                Mid2.setNum(rs.getInt("Num"));
                Mid2.setExchange(rs.getInt("Exchange"));
                }
                if(Mid.getId()>start.getId())//始发站->中转站
                {    
                    sql="select * from station where Id between ? and ? order by Id";
                    ps=conn.prepareStatement(sql);
                    ps.setInt(1, start.getId());
                    ps.setInt(2, Mid.getId());
                    rs=ps.executeQuery();
                    while(rs.next()) {
                    user=new User();
                    user.setName(rs.getString("Name"));
                    user.setId(rs.getInt("Id"));
                    user.setLine(rs.getInt("Line")) ;
                    user.setNum(rs.getInt("Num"));
                    user.setExchange(rs.getInt("Exchange"));
                    users.add(user);       
                }
                     if(Mid2.getId()>end.getId())//始发站->中转站    终点站->中转站
                        {
                         System.out.println("");
                         sql="select * from station where Id between ? and ? order by Id desc";
                            ps=conn.prepareStatement(sql);
                            ps.setInt(1, end.getId());
                            ps.setInt(2, Mid2.getId()-1);
                            rs=ps.executeQuery();
                            while(rs.next()) {
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);       
                        }
                        }
                     else//始发站->中转站->终点站
                     {
                         sql="select * from station where Id between ? and ? order by Id ";
                            ps=conn.prepareStatement(sql);
                            ps.setInt(2, end.getId());
                            ps.setInt(1, Mid2.getId()+1);
                            rs=ps.executeQuery();
                            while(rs.next()) {
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);       
                        }
                     }
                     
                }
                else//中转站<-始发站
                {    
                    
                    sql="select * from station where Id between ? and ? order by Id desc";
                    ps=conn.prepareStatement(sql);
                    ps.setInt(2, start.getId());
                    ps.setInt(1, Mid.getId());
                    rs=ps.executeQuery();
                    while(rs.next()) {
                    user=new User();
                    user.setName(rs.getString("Name"));
                    user.setId(rs.getInt("Id"));
                    user.setLine(rs.getInt("Line")) ;
                    user.setNum(rs.getInt("Num"));
                    user.setExchange(rs.getInt("Exchange"));
                    users.add(user);       
                }
                     if(Mid2.getId()>end.getId())//中转站<-始发站    终点站->中转站    
                        {
                         sql="select * from station where Id between ? and ? order by Id desc";
                            ps=conn.prepareStatement(sql);
                            ps.setInt(1, end.getId());
                            ps.setInt(2, Mid2.getId()-1);
                            rs=ps.executeQuery();
                            while(rs.next()) {
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);       
                        }
                        }
                     else//中转站<-始发站    
                     {
                         sql="select * from station where Id between ? and ? order by Id ";
                            ps=conn.prepareStatement(sql);
                            ps.setInt(2, end.getId());
                            ps.setInt(1, Mid2.getId()+1);
                            rs=ps.executeQuery();
                            while(rs.next()) {
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);       
                        }
                     }
                     
                }
                
                
                
                
                
                
                
                
                
                
            }catch(SQLException e) {
                e.printStackTrace();
            }
            
            
            
            
            
            
        }
        return users;
    }
    
        //能返回站名,同时实现两个转战口,但是不能三条最近
        
        public List<User> Num3(String a,String b)
        {
        User start=new User();
        User end=new User();
        start=loadUser(a);
        end=loadUser(b);
        conn=ut.getConn();
        ps=null;
        ResultSet rs=null;
        String id;
        sql="select * from station where Id between ? and ? order by Id";
        List<User> users=new ArrayList<User>();
        List<User>    Mid1s=new ArrayList<User>();//做出一个始发站中转站点集
        List<User>    Mid2s=new ArrayList<User>();//做出一个终点站中转站点集
        User user=null;
        User Mid1=null;
        User Mid2=null;
        if(start.getLine()==end.getLine())//一条线
        {
            if(start.getId()<=end.getId())
            {
                try {
                    conn=ut.getConn();
            ps=conn.prepareStatement(sql);
            ps.setInt(1, start.getId());
            ps.setInt(2, end.getId());
            
            rs=ps.executeQuery();
            while(rs.next()) {
                user=new User();
                user.setName(rs.getString("Name"));
                user.setId(rs.getInt("Id"));
                user.setLine(rs.getInt("Line")) ;
                user.setNum(rs.getInt("Num"));
                user.setExchange(rs.getInt("Exchange"));
                users.add(user);
            }
        }catch(SQLException e) {
            e.printStackTrace();
        }
        }
            else
            {
                
                try {
                    sql="select * from station where Id between ? and ? order by Id desc";
                    conn=ut.getConn();
                    ps=conn.prepareStatement(sql);
                    ps.setInt(2, start.getId());
                    ps.setInt(1, end.getId());
                    rs=ps.executeQuery();
                    while(rs.next()) {
                        user=new User();
                        user.setName(rs.getString("Name"));
                        user.setId(rs.getInt("Id"));
                        user.setLine(rs.getInt("Line")) ;
                        user.setNum(rs.getInt("Num"));
                        user.setExchange(rs.getInt("Exchange"));
                        users.add(user);
                    }
                }catch(SQLException e) {
                    e.printStackTrace();
                }
            }
            
            
        }
        if(start.getLine()!=end.getLine())//不在一条线
        {
            int mid=0;
            try {
                
                sql="select * from station where Line=? and Exchange=?";//始发站一条线
                conn=ut.getConn();
                ps=conn.prepareStatement(sql);
                ps.setInt(1, start.getLine());
                ps.setInt(2, end.getLine());
                rs=ps.executeQuery();
                while(rs.next())
                {
                Mid1=new User();
                Mid1.setName(rs.getString("Name"));
                Mid1.setId(rs.getInt("Id"));
                Mid1.setLine(rs.getInt("Line")) ;
                Mid1.setNum(rs.getInt("Num"));
                Mid1.setExchange(rs.getInt("Exchange"));
                Mid1s.add(Mid1);
                }
                sql="select * from station where Line=? and Exchange=?";  //找到在终点站那条线路上
                conn=ut.getConn();
                ps=conn.prepareStatement(sql);
                ps.setInt(2, start.getLine());
                ps.setInt(1, end.getLine());
                rs=ps.executeQuery();
                while(rs.next())
                {
                    Mid2=new User();
                Mid2.setName(rs.getString("Name"));
                Mid2.setId(rs.getInt("Id"));
                Mid2.setLine(rs.getInt("Line")) ;
                Mid2.setNum(rs.getInt("Num"));
                Mid2.setExchange(rs.getInt("Exchange"));
                Mid2s.add(Mid2);
                }
                int min=100;//选出最近转战口
                int Min1=Mid1.getId();//最近转战口ID 与始发站相同
                int Min2=Mid2.getId();//与终点站相同
                for(User mid1:Mid1s)
                {
                    for(User mid2:Mid2s)
                    {
                        if(mid1.getName().equals(mid2.getName()))
                        {
                            int n=Math.abs(mid1.getId()-start.getId())+Math.abs(mid2.getId()-end.getId());
                            if(min>n)
                            {
                                min=n;
                                Min1=mid1.getId();
                                Min2=mid2.getId();
                            }
                        }
                    }
                    
                }
                System.out.println(Min1);
                User mid1=Select_Id(Min1);
                User mid2=Select_Id(Min2);
                System.out.println(mid2.getName()+"asdfasdfasdgasdgasdf"+mid1.getName());////////////
                if(mid1.getId()>start.getId())//始发站->中转站
                {    
                    sql="select * from station where Id between ? and ? order by Id";
                    conn=ut.getConn();
                    ps=conn.prepareStatement(sql);
                    ps.setInt(1, start.getId());
                    ps.setInt(2, mid1.getId());
                    rs=ps.executeQuery();
                    while(rs.next()) {
                    user=new User();
                    user.setName(rs.getString("Name"));
                    user.setId(rs.getInt("Id"));
                    user.setLine(rs.getInt("Line")) ;
                    user.setNum(rs.getInt("Num"));
                    user.setExchange(rs.getInt("Exchange"));
                    users.add(user);       
                }
                     if(mid2.getId()>end.getId())//始发站->中转站    终点站->中转站
                        {
                         System.out.println("");
                         sql="select * from station where Id between ? and ? order by Id desc";
                            conn=ut.getConn();
                            ps=conn.prepareStatement(sql);
                            ps.setInt(1, end.getId());
                            ps.setInt(2, mid2.getId()-1);
                            rs=ps.executeQuery();
                            while(rs.next()) {
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);       
                        }
                        }
                     else//始发站->中转站->终点站
                     {
                         sql="select * from station where Id between ? and ? order by Id ";
                            conn=ut.getConn();
                            ps=conn.prepareStatement(sql);
                            ps.setInt(2, end.getId());
                            ps.setInt(1, mid2.getId()+1);
                            rs=ps.executeQuery();
                            while(rs.next()) {
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);       
                        }
                     }
                     
                }
                else//中转站<-始发站
                {    
                    
                    sql="select * from station where Id between ? and ? order by Id desc";
                    conn=ut.getConn();
                    ps=conn.prepareStatement(sql);
                    ps.setInt(2, start.getId());
                    ps.setInt(1, mid1.getId());
                    rs=ps.executeQuery();
                    while(rs.next()) {
                    user=new User();
                    user.setName(rs.getString("Name"));
                    user.setId(rs.getInt("Id"));
                    user.setLine(rs.getInt("Line")) ;
                    user.setNum(rs.getInt("Num"));
                    user.setExchange(rs.getInt("Exchange"));
                    users.add(user);       
                }
                     if(mid2.getId()>end.getId())//中转站<-始发站    终点站->中转站    
                        {
                         sql="select * from station where Id between ? and ? order by Id desc";
                            conn=ut.getConn();
                            ps=conn.prepareStatement(sql);
                            ps.setInt(1, end.getId());
                            ps.setInt(2, mid2.getId()-1);
                            rs=ps.executeQuery();
                            while(rs.next()) {
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);       
                        }
                        }
                     else//中转站<-始发站    
                     {
                         sql="select * from station where Id between ? and ? order by Id ";
                            conn=ut.getConn();
                            ps=conn.prepareStatement(sql);
                            ps.setInt(2, end.getId());
                            ps.setInt(1, mid2.getId()+1);
                            rs=ps.executeQuery();
                            while(rs.next()) {
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);       
                        }
                     }
                     
                }
                
       
                
                
                
                
                
                
            }catch(SQLException e) {
                e.printStackTrace();
            }
            
            
            
            
            
            
        }
        return users;
    }
    
        public List<User>Num4(String a,String b)
        {
            User start=new User();
            User end=new User();
            start=loadUser(a);
            end=loadUser(b);
            conn=ut.getConn();
            ps=null;
            ResultSet rs=null;
            String id;
            sql="select * from station where Id between ? and ? order by Id";
            List<User> users=new ArrayList<User>();
            List<User>    Mid1s=new ArrayList<User>();//做出一个始发站中转站点集
            List<User>    Mid2s=new ArrayList<User>();//做出一个终点站中转站点集
            List<User>    Mid3s=new ArrayList<User>();//1在转战线上的位置
            List<User>    Mid4s=new ArrayList<User>();//2在转战上的位置
            User user=null;
            User Mid1=null;
            User Mid2=null;
            User Mid3=null;
            User Mid4=null;
            
            int Zhuan;//最近的转战是第几条线
            if(start.getLine()==end.getLine())//一条线
            {
                if(start.getId()<=end.getId())
                {
                    try {
                        conn=ut.getConn();
                ps=conn.prepareStatement(sql);
                ps.setInt(1, start.getId());
                ps.setInt(2, end.getId());
                
                rs=ps.executeQuery();
                while(rs.next()) {
                    user=new User();
                    user.setName(rs.getString("Name"));
                    user.setId(rs.getInt("Id"));
                    user.setLine(rs.getInt("Line")) ;
                    user.setNum(rs.getInt("Num"));
                    user.setExchange(rs.getInt("Exchange"));
                    users.add(user);
                }
            }catch(SQLException e) {
                e.printStackTrace();
            }
            }
                else
                {
                    
                    try {
                        sql="select * from station where Id between ? and ? order by Id desc";
                        conn=ut.getConn();
                        ps=conn.prepareStatement(sql);
                        ps.setInt(2, start.getId());
                        ps.setInt(1, end.getId());
                        rs=ps.executeQuery();
                        while(rs.next()) {
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);
                        }
                    }catch(SQLException e) {
                        e.printStackTrace();
                    }
                }
                
                
            }
            if(start.getLine()!=end.getLine())//不在一条线
            {
                int mid=0;
                try {
                    
                    sql="select * from station where Line=? and Exchange !=?";//始发站一条线
                    conn=ut.getConn();
                    ps=conn.prepareStatement(sql);
                    ps.setInt(1, start.getLine());
                    ps.setInt(2, 0);
                    rs=ps.executeQuery();
                    while(rs.next())
                    {
                    Mid1=new User();
                    Mid1.setName(rs.getString("Name"));
                    Mid1.setId(rs.getInt("Id"));
                    Mid1.setLine(rs.getInt("Line")) ;
                    Mid1.setNum(rs.getInt("Num"));
                    Mid1.setExchange(rs.getInt("Exchange"));
                    Mid1s.add(Mid1);
                    }
                    for(User mid1:Mid1s)
                    {
                        sql="select * from station where Name=?";//同名 但是Id 不一样
                        conn=ut.getConn();
                        ps=conn.prepareStatement(sql);
                        ps.setString(1, mid1.getName());
                        rs=ps.executeQuery();
                        while(rs.next())
                        {
                        Mid3=new User();
                        Mid3.setName(rs.getString("Name"));
                        Mid3.setId(rs.getInt("Id"));
                        Mid3.setLine(rs.getInt("Line")) ;
                        Mid3.setNum(rs.getInt("Num"));
                        Mid3.setExchange(rs.getInt("Exchange"));
                        if(Mid3.getId()!=mid1.getId())
                        {
                            Mid3s.add(Mid3);
                        }
                        }
                    }
                    sql="select * from station where Line=? and Exchange !=?";  //找到在终点站那条线路上
                    conn=ut.getConn();
                    ps=conn.prepareStatement(sql);
                    ps.setInt(2, start.getLine());
                    ps.setInt(1, end.getLine());
                    rs=ps.executeQuery();
                    while(rs.next())
                    {
                    Mid2=new User();
                    Mid2.setName(rs.getString("Name"));
                    Mid2.setId(rs.getInt("Id"));
                    Mid2.setLine(rs.getInt("Line")) ;
                    Mid2.setNum(rs.getInt("Num"));
                    Mid2.setExchange(rs.getInt("Exchange"));
                    Mid2s.add(Mid2);
                    }
                    for(User mid2:Mid2s)
                    {
                        sql="select * from station where Name=?";//同名 但是Id 不一样
                        conn=ut.getConn();
                        ps=conn.prepareStatement(sql);
                        ps.setString(1, mid2.getName());
                        rs=ps.executeQuery();
                        while(rs.next())
                        {
                        Mid4=new User();
                        Mid4.setName(rs.getString("Name"));
                        Mid4.setId(rs.getInt("Id"));
                        Mid4.setLine(rs.getInt("Line")) ;
                        Mid4.setNum(rs.getInt("Num"));
                        Mid4.setExchange(rs.getInt("Exchange"));
                        if(Mid4.getId()!=mid2.getId())
                        {
                            Mid4s.add(Mid4);
                        }
                        }
                    }
                    
                    
                    
                    int min=100;//选出最近转战口
                    int Min1=Mid1.getId();//最近转战口ID 与始发站相同
                    int Min2=Mid2.getId();//与终点站相同
                    int Min3=Mid3.getId();
                    int Min4=Mid4.getId();
                    for(User mid1:Mid1s)
                    {
                        for(User mid3:Mid3s)
                        {
                        for(User mid2:Mid2s)
                        {
                            for(User mid4:Mid4s)
                            {
                            if((mid1.getExchange()==mid2.getExchange())&&(mid3.getLine()==mid4.getLine())&&(mid1.getExchange()==mid3.getLine()))
                            {
                                
                                int n=Math.abs(mid1.getId()-start.getId())+Math.abs(mid2.getId()-end.getId())+Math.abs(mid3.getId()-mid4.getId());
                                if(min>n)
                                {
                                    min=n;
                                    Min1=mid1.getId();
                                    Min2=mid2.getId();
                                    Min3=mid3.getId();
                                    Min4=mid4.getId();
                                }
                            }
                            }
                            
                        }
                        }
                    }
                    System.out.println(Min1);
                    User mid1=Select_Id(Min1);
                    User mid2=Select_Id(Min2);
                    User mid3=Select_Id(Min3);
                    User mid4=Select_Id(Min4);
                    System.out.println(mid2.getName()+"asdfasdfasdgasdgasdf"+mid1.getName());////////////
                    if(mid1.getId()>start.getId())//始发站->中转站
                    {    
                        sql="select * from station where Id between ? and ? order by Id";
                        conn=ut.getConn();
                        ps=conn.prepareStatement(sql);
                        ps.setInt(1, start.getId());
                        ps.setInt(2, mid1.getId());
                        rs=ps.executeQuery();
                        while(rs.next()) {
                        user=new User();
                        user.setName(rs.getString("Name"));
                        user.setId(rs.getInt("Id"));
                        user.setLine(rs.getInt("Line")) ;
                        user.setNum(rs.getInt("Num"));
                        user.setExchange(rs.getInt("Exchange"));
                        users.add(user);       
                    }
                        if(mid3.getId()>mid4.getId())//3->4
                        {
                            sql="select * from station where Id between ? and ? order by Id desc";
                            conn=ut.getConn();
                            ps=conn.prepareStatement(sql);
                            ps.setInt(1,mid4.getId());
                            ps.setInt(2, mid3.getId());
                            rs=ps.executeQuery();
                            while(rs.next()) {
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);       
                        }
                        }
                        if(mid3.getId()<mid4.getId())//3->4
                        {
                            sql="select * from station where Id between ? and ? order by Id ";
                            conn=ut.getConn();
                            ps=conn.prepareStatement(sql);
                            ps.setInt(1,mid3.getId());
                            ps.setInt(2, mid4.getId());
                            rs=ps.executeQuery();
                            while(rs.next()) {
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);       
                        }
                        }
                         if(mid2.getId()>end.getId())//始发站->中转站    终点站->中转站
                            {
                             System.out.println("");
                             sql="select * from station where Id between ? and ? order by Id desc";
                                conn=ut.getConn();
                                ps=conn.prepareStatement(sql);
                                ps.setInt(1, end.getId());
                                ps.setInt(2, mid2.getId()-1);
                                rs=ps.executeQuery();
                                while(rs.next()) {
                                user=new User();
                                user.setName(rs.getString("Name"));
                                user.setId(rs.getInt("Id"));
                                user.setLine(rs.getInt("Line")) ;
                                user.setNum(rs.getInt("Num"));
                                user.setExchange(rs.getInt("Exchange"));
                                users.add(user);       
                            }
                            }
                         else//始发站->中转站->终点站
                         {
                             sql="select * from station where Id between ? and ? order by Id ";
                                conn=ut.getConn();
                                ps=conn.prepareStatement(sql);
                                ps.setInt(2, end.getId());
                                ps.setInt(1, mid2.getId()+1);
                                rs=ps.executeQuery();
                                while(rs.next()) {
                                user=new User();
                                user.setName(rs.getString("Name"));
                                user.setId(rs.getInt("Id"));
                                user.setLine(rs.getInt("Line")) ;
                                user.setNum(rs.getInt("Num"));
                                user.setExchange(rs.getInt("Exchange"));
                                users.add(user);       
                            }
                         }
                         
                    }
                    else//中转站<-始发站
                    {    
                        
                        sql="select * from station where Id between ? and ? order by Id desc";
                        conn=ut.getConn();
                        ps=conn.prepareStatement(sql);
                        ps.setInt(2, start.getId());
                        ps.setInt(1, mid1.getId());
                        rs=ps.executeQuery();
                        while(rs.next()) {
                        user=new User();
                        user.setName(rs.getString("Name"));
                        user.setId(rs.getInt("Id"));
                        user.setLine(rs.getInt("Line")) ;
                        user.setNum(rs.getInt("Num"));
                        user.setExchange(rs.getInt("Exchange"));
                        users.add(user);       
                    }
                        if(mid3.getId()>mid4.getId())//3->4
                        {
                            sql="select * from station where Id between ? and ? order by Id desc";
                            conn=ut.getConn();
                            ps=conn.prepareStatement(sql);
                            ps.setInt(1,mid4.getId());
                            ps.setInt(2, mid3.getId());
                            rs=ps.executeQuery();
                            while(rs.next()) {
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);       
                        }
                        }
                        if(mid3.getId()<mid4.getId())//3->4
                        {
                            sql="select * from station where Id between ? and ? order by Id ";
                            conn=ut.getConn();
                            ps=conn.prepareStatement(sql);
                            ps.setInt(1,mid3.getId());
                            ps.setInt(2, mid4.getId());
                            rs=ps.executeQuery();
                            while(rs.next()) {
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);       
                        }
                        }
                        
                         if(mid2.getId()>end.getId())//中转站<-始发站    终点站->中转站    
                            {
                             sql="select * from station where Id between ? and ? order by Id desc";
                                conn=ut.getConn();
                                ps=conn.prepareStatement(sql);
                                ps.setInt(1, end.getId());
                                ps.setInt(2, mid2.getId()-1);
                                rs=ps.executeQuery();
                                while(rs.next()) {
                                user=new User();
                                user.setName(rs.getString("Name"));
                                user.setId(rs.getInt("Id"));
                                user.setLine(rs.getInt("Line")) ;
                                user.setNum(rs.getInt("Num"));
                                user.setExchange(rs.getInt("Exchange"));
                                users.add(user);       
                            }
                            }
                         else//中转站<-始发站    
                         {
                             sql="select * from station where Id between ? and ? order by Id ";
                                conn=ut.getConn();
                                ps=conn.prepareStatement(sql);
                                ps.setInt(2, end.getId());
                                ps.setInt(1, mid2.getId()+1);
                                rs=ps.executeQuery();
                                while(rs.next()) {
                                user=new User();
                                user.setName(rs.getString("Name"));
                                user.setId(rs.getInt("Id"));
                                user.setLine(rs.getInt("Line")) ;
                                user.setNum(rs.getInt("Num"));
                                user.setExchange(rs.getInt("Exchange"));
                                users.add(user);       
                            }
                         }
                         
                    }
                    
           
                    
                    
                    
                    
                    
                    
                }catch(SQLException e) {
                    e.printStackTrace();
                }
                
                
                
                
                
                
            }
            return users;
        }
        
        public List<User>Num5(String a,String b)
        {
            int NN=0;
            User start=new User();
            User end=new User();
            start=loadUser(a);
            end=loadUser(b);
            conn=ut.getConn();
            ps=null;
            ResultSet rs=null;
            String id;
            sql="select * from station where Id between ? and ? order by Id";
            List<User> users=new ArrayList<User>();
            List<User>    Mid1s=new ArrayList<User>();//做出一个始发站中转站点集
            List<User>    Mid2s=new ArrayList<User>();//做出一个终点站中转站点集
            List<User>    Mid3s=new ArrayList<User>();//1在转战线上的位置
            List<User>    Mid4s=new ArrayList<User>();//2在转战上的位置
            User user=null;
            User Mid1=null;
            User Mid2=null;
            User Mid3=null;
            User Mid4=null;
            
            int Zhuan;//最近的转战是第几条线
            if(start.getLine()==end.getLine())//一条线
            {
                if(start.getId()<=end.getId())
                {
                    try {
                        conn=ut.getConn();
                ps=conn.prepareStatement(sql);
                ps.setInt(1, start.getId());
                ps.setInt(2, end.getId());
                
                rs=ps.executeQuery();
                while(rs.next()) {
                    user=new User();
                    user.setName(rs.getString("Name"));
                    user.setId(rs.getInt("Id"));
                    user.setLine(rs.getInt("Line")) ;
                    user.setNum(rs.getInt("Num"));
                    user.setExchange(rs.getInt("Exchange"));
                    users.add(user);
                }
            }catch(SQLException e) {
                e.printStackTrace();
            }
            }
                else
                {
                    
                    try {
                        sql="select * from station where Id between ? and ? order by Id desc";
                        conn=ut.getConn();
                        ps=conn.prepareStatement(sql);
                        ps.setInt(2, start.getId());
                        ps.setInt(1, end.getId());
                        rs=ps.executeQuery();
                        while(rs.next()) {
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);
                        }
                    }catch(SQLException e) {
                        e.printStackTrace();
                    }
                }
                
                
            }
            if(start.getLine()!=end.getLine())//不在一条线
            {
                int mid=0;
                try {
                    
                    sql="select * from station where Line=? and Exchange !=?";//始发站一条线
                    conn=ut.getConn();
                    ps=conn.prepareStatement(sql);
                    ps.setInt(1, start.getLine());
                    ps.setInt(2, 0);
                    rs=ps.executeQuery();
                    while(rs.next())
                    {
                    Mid1=new User();
                    Mid1.setName(rs.getString("Name"));
                    Mid1.setId(rs.getInt("Id"));
                    Mid1.setLine(rs.getInt("Line")) ;
                    Mid1.setNum(rs.getInt("Num"));
                    Mid1.setExchange(rs.getInt("Exchange"));
                    Mid1s.add(Mid1);
                    }
                    for(User mid1:Mid1s)
                    {
                        sql="select * from station where Name=?";//同名 但是Id 不一样
                        conn=ut.getConn();
                        ps=conn.prepareStatement(sql);
                        ps.setString(1, mid1.getName());
                        rs=ps.executeQuery();
                        while(rs.next())
                        {
                        Mid3=new User();
                        Mid3.setName(rs.getString("Name"));
                        Mid3.setId(rs.getInt("Id"));
                        Mid3.setLine(rs.getInt("Line")) ;
                        Mid3.setNum(rs.getInt("Num"));
                        Mid3.setExchange(rs.getInt("Exchange"));
                        if(Mid3.getId()!=mid1.getId())
                        {
                            Mid3s.add(Mid3);
                        }
                        }
                    }
                    sql="select * from station where Line=? and Exchange !=?";  //找到在终点站那条线路上
                    conn=ut.getConn();
                    ps=conn.prepareStatement(sql);
                    ps.setInt(2, start.getLine());
                    ps.setInt(1, end.getLine());
                    rs=ps.executeQuery();
                    while(rs.next())
                    {
                    Mid2=new User();
                    Mid2.setName(rs.getString("Name"));
                    Mid2.setId(rs.getInt("Id"));
                    Mid2.setLine(rs.getInt("Line")) ;
                    Mid2.setNum(rs.getInt("Num"));
                    Mid2.setExchange(rs.getInt("Exchange"));
                    Mid2s.add(Mid2);
                    }
                    for(User mid2:Mid2s)
                    {
                        sql="select * from station where Name=?";//同名 但是Id 不一样
                        conn=ut.getConn();
                        ps=conn.prepareStatement(sql);
                        ps.setString(1, mid2.getName());
                        rs=ps.executeQuery();
                        while(rs.next())
                        {
                        Mid4=new User();
                        Mid4.setName(rs.getString("Name"));
                        Mid4.setId(rs.getInt("Id"));
                        Mid4.setLine(rs.getInt("Line")) ;
                        Mid4.setNum(rs.getInt("Num"));
                        Mid4.setExchange(rs.getInt("Exchange"));
                        if(Mid4.getId()!=mid2.getId())
                        {
                            Mid4s.add(Mid4);
                        }
                        }
                    }
                    
                    
                    
                    int min=100;//选出最近转战口
                    int Min1=Mid1.getId();//最近转战口ID 与始发站相同
                    int Min2=Mid2.getId();//与终点站相同
                    int Min3=Mid3.getId();
                    int Min4=Mid4.getId();
                    for(User mid1:Mid1s)
                    {
                        for(User mid3:Mid3s)
                        {
                        for(User mid2:Mid2s)
                        {
                            for(User mid4:Mid4s)
                            {
                            if((mid1.getExchange()==mid2.getExchange())&&(mid3.getLine()==mid4.getLine())&&(mid1.getExchange()==mid3.getLine()))
                            {
                                
                                int n=Math.abs(mid1.getId()-start.getId())+Math.abs(mid2.getId()-end.getId())+Math.abs(mid3.getId()-mid4.getId());
                                if(min>n)
                                {
                                    min=n;
                                    Min1=mid1.getId();
                                    Min2=mid2.getId();
                                    Min3=mid3.getId();
                                    Min4=mid4.getId();
                                }
                            }
                            }
                            
                        }
                        }
                    }
                    System.out.println(Min1);
                    User mid1=Select_Id(Min1);
                    User mid2=Select_Id(Min2);
                    User mid3=Select_Id(Min3);
                    User mid4=Select_Id(Min4);
                    System.out.println(mid2.getName()+"asdfasdfasdgasdgasdf"+mid1.getName());////////////
                    if(mid1.getId()>start.getId())//始发站->中转站
                    {    
                        sql="select * from station where Id between ? and ? order by Id";
                        conn=ut.getConn();
                        ps=conn.prepareStatement(sql);
                        ps.setInt(1, start.getId());
                        ps.setInt(2, mid1.getId());
                        rs=ps.executeQuery();
                        while(rs.next()) {
                        NN=NN+1;
                        user=new User();
                        user.setName(rs.getString("Name"));
                        user.setId(rs.getInt("Id"));
                        user.setLine(rs.getInt("Line")) ;
                        user.setNum(rs.getInt("Num"));
                        user.setExchange(rs.getInt("Exchange"));
                        users.add(user);       
                    }
                        if(mid3.getId()>mid4.getId())//3->4
                        {
                            sql="select * from station where Id between ? and ? order by Id desc";
                            conn=ut.getConn();
                            ps=conn.prepareStatement(sql);
                            ps.setInt(1,mid4.getId());
                            ps.setInt(2, mid3.getId()-1);/////////////////////
                            rs=ps.executeQuery();
                            while(rs.next()) {
                                NN=NN+1;
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);       
                        }
                        }
                        if(mid3.getId()<mid4.getId())//3->4
                        {
                            sql="select * from station where Id between ? and ? order by Id ";
                            conn=ut.getConn();
                            ps=conn.prepareStatement(sql);
                            ps.setInt(1,mid3.getId()+1);
                            ps.setInt(2, mid4.getId());
                            rs=ps.executeQuery();
                            while(rs.next()) {
                                NN=NN+1;
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);       
                        }
                        }
                         if(mid2.getId()>end.getId())//始发站->中转站    终点站->中转站
                            {
                             System.out.println("");
                             sql="select * from station where Id between ? and ? order by Id desc";
                                conn=ut.getConn();
                                ps=conn.prepareStatement(sql);
                                ps.setInt(1, end.getId());
                                ps.setInt(2, mid2.getId()-1);
                                rs=ps.executeQuery();
                                while(rs.next()) {
                                    NN=NN+1;
                                user=new User();
                                user.setName(rs.getString("Name"));
                                user.setId(rs.getInt("Id"));
                                user.setLine(rs.getInt("Line")) ;
                                user.setNum(rs.getInt("Num"));
                                user.setExchange(rs.getInt("Exchange"));
                                users.add(user);       
                            }
                            }
                         else//始发站->中转站->终点站
                         {
                             sql="select * from station where Id between ? and ? order by Id ";
                                conn=ut.getConn();
                                ps=conn.prepareStatement(sql);
                                ps.setInt(2, end.getId());
                                ps.setInt(1, mid2.getId()+1);
                                rs=ps.executeQuery();
                                while(rs.next()) {
                                    NN=NN+1;
                                user=new User();
                                user.setName(rs.getString("Name"));
                                user.setId(rs.getInt("Id"));
                                user.setLine(rs.getInt("Line")) ;
                                user.setNum(rs.getInt("Num"));
                                user.setExchange(rs.getInt("Exchange"));
                                users.add(user);       
                            }
                         }
                         
                    }
                    else//中转站<-始发站
                    {    
                        
                        sql="select * from station where Id between ? and ? order by Id desc";
                        conn=ut.getConn();
                        ps=conn.prepareStatement(sql);
                        ps.setInt(2, start.getId());
                        ps.setInt(1, mid1.getId());
                        rs=ps.executeQuery();
                        while(rs.next()) {
                            NN=NN+1;
                        user=new User();
                        user.setName(rs.getString("Name"));
                        user.setId(rs.getInt("Id"));
                        user.setLine(rs.getInt("Line")) ;
                        user.setNum(rs.getInt("Num"));
                        user.setExchange(rs.getInt("Exchange"));
                        users.add(user);       
                    }
                        if(mid3.getId()>mid4.getId())//3->4
                        {
                            sql="select * from station where Id between ? and ? order by Id desc";
                            conn=ut.getConn();
                            ps=conn.prepareStatement(sql);
                            ps.setInt(1,mid4.getId());
                            ps.setInt(2, mid3.getId()-1);
                            rs=ps.executeQuery();
                            while(rs.next()) {
                                NN=NN+1;
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);       
                        }
                        }
                        if(mid3.getId()<mid4.getId())//3->4
                        {
                            sql="select * from station where Id between ? and ? order by Id ";
                            conn=ut.getConn();
                            ps=conn.prepareStatement(sql);
                            ps.setInt(1,mid3.getId()+1);
                            ps.setInt(2, mid4.getId());
                            rs=ps.executeQuery();
                            while(rs.next()) {
                                NN=NN+1;
                            user=new User();
                            user.setName(rs.getString("Name"));
                            user.setId(rs.getInt("Id"));
                            user.setLine(rs.getInt("Line")) ;
                            user.setNum(rs.getInt("Num"));
                            user.setExchange(rs.getInt("Exchange"));
                            users.add(user);       
                        }
                        }
                        
                         if(mid2.getId()>end.getId())//中转站<-始发站    终点站->中转站    
                            {
                             sql="select * from station where Id between ? and ? order by Id desc";
                                conn=ut.getConn();
                                ps=conn.prepareStatement(sql);
                                ps.setInt(1, end.getId());
                                ps.setInt(2, mid2.getId()-1);
                                rs=ps.executeQuery();
                                while(rs.next()) {
                                    NN=NN+1;
                                user=new User();
                                user.setName(rs.getString("Name"));
                                user.setId(rs.getInt("Id"));
                                user.setLine(rs.getInt("Line")) ;
                                user.setNum(rs.getInt("Num"));
                                user.setExchange(rs.getInt("Exchange"));
                                users.add(user);       
                            }
                            }
                         else//中转站<-始发站    
                         {
                             sql="select * from station where Id between ? and ? order by Id ";
                                conn=ut.getConn();
                                ps=conn.prepareStatement(sql);
                                ps.setInt(2, end.getId());
                                ps.setInt(1, mid2.getId()+1);
                                rs=ps.executeQuery();
                                while(rs.next()) {
                                    NN=NN+1;
                                user=new User();
                                user.setName(rs.getString("Name"));
                                user.setId(rs.getInt("Id"));
                                user.setLine(rs.getInt("Line")) ;
                                user.setNum(rs.getInt("Num"));
                                user.setExchange(rs.getInt("Exchange"));
                                users.add(user);       
                            }
                         }
                         
                    }
                    
           
                    
                    
                    
                    
                    
                    
                }catch(SQLException e) {
                    e.printStackTrace();
                }
                
                
                
                
                
                
            }
            System.out.println("aadfasdfsdaf"+NN+"adsfads"+Num1(a,b));
            if(NN>Num1(a,b))
            {
                return Num3(a,b);
            }
            else
                {
                return users;
                }
        }
    
    
    
    }

    main.java

    package com.demo;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class main {
        public static void main(String[] args) {
            LoginerDaolmpl loginerDaoImpl=new LoginerDaolmpl();
            List<User> users = new ArrayList<User>();
            System.out.println("查询");
    //        users=loginerDaoImpl.load();
    //        
    //        for(User u:users) {
    //            System.out.println(u.getName());
    //        }
            Scanner scan=new Scanner(System.in);
            String a=scan.next();
            String b=scan.next();
            
    //        users=loginerDaoImpl.Num2(a, b);
    //        users=loginerDaoImpl.Num3(a, b);
    //        users=loginerDaoImpl.Num4(a, b);
            users=loginerDaoImpl.Num5(a, b);
            for(User u:users) {
                System.out.println(u.getId());
                System.out.println(u.getName());
            }
            System.out.println(loginerDaoImpl.Num1(a, b));
        }
    }

    User.java

    package com.demo;
    import java.sql.Connection;
    public class User {
        private int Id;
        private int Line;
        private String Name;
        private int Num;
        private int Exchange;
        
        public int getId() {
            return Id;
        }
        public void setId(int id) {
            Id = id;
        }
        public int getLine() {
            return Line;
        }
        public void setLine(int line) {
            Line = line;
        }
        public String getName() {
            return Name;
        }
        public void setName(String name) {
            Name = name;
        }
        public int getNum() {
            return Num;
        }
        public void setNum(int num) {
            Num = num;
        }
        public int getExchange() {
            return Exchange;
        }
        public void setExchange(int exchange) {
            Exchange = exchange;
        }
        public User(int id,int line ,String name,int num,int exchange) {
            this.Name = name;
            this.Id=id;
            this.Line=line;
            this.Num=num;
            this.Exchange=exchange;
        }
        public User() {
            // TODO Auto-generated constructor stub
        }
    }

    DataBuilder.java

    package DJSTL;
    import util.util;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Scanner;
    import java.util.Set;
    
    public class DataBuilder {
        static Connection conn;
        static PreparedStatement ps = null;
        static ResultSet rs;
        static String sql = "select * from station";   //连接数据库
        static util ut= new util();
        static Scanner in = new Scanner(System.in);
        static String station_name;
        
        public static List<Station> line1 = new ArrayList<Station>();//1号线
        public static List<Station> line2 = new ArrayList<Station>();//2号线
        public static List<Station> line3 = new ArrayList<Station>();//3号线
        public static List<Station> line4 = new ArrayList<Station>();//10号线
        public static List<Station> line5 = new ArrayList<Station>();//s1号线
        public static List<Station> line6 = new ArrayList<Station>();//s8号线
        public static Set<List<Station>> lineSet = new HashSet<List<Station>>();//所有线集合
        public static int totalStaion = 0;//总的站点数量
        static {        
            String[] line1Arr=new String[29];
            String[] line2Arr=new String[37];
            String[] line3Arr=new String[34];
            String[] line4Arr=new String[18];
            String[] line5Arr=new String[21];
            String[] line6Arr=new String[19];
            //1号线
            
            try {
                conn=ut.getConn();
                ps=null;
                ResultSet rs=null;
                sql="select * from station where Line=?";
                ps=conn.prepareStatement(sql);
                ps.setInt(1, 1);
                rs=ps.executeQuery();
                int i=-1;
                    while(rs.next()) 
                    {
                        
                        i++;    
                        station_name=rs.getString("Name");
                        line1Arr[i]=station_name;
    //                    System.out.println(station_name);
                        
                    }
                
                }
                catch(SQLException e) 
                {
                    e.printStackTrace();
                }
                finally 
                {
                    try 
                    {
                         if(ps!=null)ps.close();
                         if(conn!=null)conn.close();
                    }
                    catch(Exception e2) 
                    {
                         e2.printStackTrace();
                    }
                }
            try {
                conn=ut.getConn();
                ps=null;
                ResultSet rs=null;
                sql="select * from station where Line=?";
                ps=conn.prepareStatement(sql);
                ps.setInt(1, 2);
                rs=ps.executeQuery();
                int i=-1;
                    while(rs.next()) 
                    {
                        
                        i++;    
                        station_name=rs.getString("Name");
                        line2Arr[i]=station_name;
    //                    System.out.println(station_name);
                        
                    }
                }
                catch(SQLException e) 
                {
                    e.printStackTrace();
                }
                finally 
                {
                    try 
                    {
                         if(ps!=null)ps.close();
                         if(conn!=null)conn.close();
                    }
                    catch(Exception e2) 
                    {
                         e2.printStackTrace();
                    }
                }
            try {
                conn=ut.getConn();
                ps=null;
                ResultSet rs=null;
                sql="select * from station where Line=?";
                ps=conn.prepareStatement(sql);
                ps.setInt(1,3);
                rs=ps.executeQuery();
                int i=-1;
                    while(rs.next()) 
                    {
                        
                        i++;    
                        station_name=rs.getString("Name");
                        line3Arr[i]=station_name;
    //                    System.out.println(station_name);
                        
                    }
                }
                catch(SQLException e) 
                {
                    e.printStackTrace();
                }
                finally 
                {
                    try 
                    {
                         if(ps!=null)ps.close();
                         if(conn!=null)conn.close();
                    }
                    catch(Exception e2) 
                    {
                         e2.printStackTrace();
                    }
                }
            try {
                conn=ut.getConn();
                ps=null;
                ResultSet rs=null;
                sql="select * from station where Line=?";
                ps=conn.prepareStatement(sql);
                ps.setInt(1, 4);
                rs=ps.executeQuery();
                int i=-1;
                    while(rs.next()) 
                    {
                        
                        i++;    
                        station_name=rs.getString("Name");
                        line4Arr[i]=station_name;
    //                    System.out.println(station_name);
                        
                    }
                }
                catch(SQLException e) 
                {
                    e.printStackTrace();
                }
                finally 
                {
                    try 
                    {
                         if(ps!=null)ps.close();
                         if(conn!=null)conn.close();
                    }
                    catch(Exception e2) 
                    {
                         e2.printStackTrace();
                    }
                }
            try {
                conn=ut.getConn();
                ps=null;
                ResultSet rs=null;
                sql="select * from station where Line=?";
                ps=conn.prepareStatement(sql);
                ps.setInt(1, 5);
                rs=ps.executeQuery();
                int i=-1;
                    while(rs.next()) 
                    {
                        
                        i++;    
                        station_name=rs.getString("Name");
                        line5Arr[i]=station_name;
    //                    System.out.println(station_name);
                        
                    }
                }
                catch(SQLException e) 
                {
                    e.printStackTrace();
                }
                finally 
                {
                    try 
                    {
                         if(ps!=null)ps.close();
                         if(conn!=null)conn.close();
                    }
                    catch(Exception e2) 
                    {
                         e2.printStackTrace();
                    }
                }
            try {
                conn=ut.getConn();
                ps=null;
                ResultSet rs=null;
                sql="select * from station where Line=?";
                ps=conn.prepareStatement(sql);
                ps.setInt(1, 6);
                rs=ps.executeQuery();
                int i=-1;
                    while(rs.next()) 
                    {
                        
                        i++;    
                        station_name=rs.getString("Name");
                        line6Arr[i]=station_name;
    //                    System.out.println(station_name);
                        
                    }
                }
                catch(SQLException e) 
                {
                    e.printStackTrace();
                }
                finally 
                {
                    try 
                    {
                         if(ps!=null)ps.close();
                         if(conn!=null)conn.close();
                    }
                    catch(Exception e2) 
                    {
                         e2.printStackTrace();
                    }
                }
            
            for(String s : line1Arr){
                line1.add(new Station(s));
            }
            for(int i =0;i<line1.size();i++){
                if(i<line1.size()-1){
                    line1.get(i).next = line1.get(i+1);
                    line1.get(i+1).prev = line1.get(i);
                }
            }
            
            /*******************************************************************************/
            //2号线
            
            for(String s : line2Arr){
                line2.add(new Station(s));
            }
            for(int i =0;i<line2.size();i++){
                if(i<line2.size()-1){
                    line2.get(i).next = line2.get(i+1);
                    line2.get(i+1).prev = line2.get(i);
                }
            }
            
            /*******************************************************************************/
            //3号线
            
            for(String s : line3Arr){
                line3.add(new Station(s));
            }
            for(int i =0;i<line3.size();i++){
                if(i<line3.size()-1){
                    line3.get(i).next = line3.get(i+1);
                    line3.get(i+1).prev = line3.get(i);
                }
            }
            
            /*******************************************************************************/        
            //4号线
            
            for(String s : line4Arr){
                line4.add(new Station(s));
            }
            for(int i =0;i<line4.size();i++){
                if(i<line4.size()-1){
                    line4.get(i).next = line4.get(i+1);
                    line4.get(i+1).prev = line4.get(i);
                }
            }
            
            /*******************************************************************************/        
            //5号线
            
            for(String s : line5Arr){
                line5.add(new Station(s));
            }
            for(int i =0;i<line5.size();i++){
                if(i<line5.size()-1){
                    line5.get(i).next = line5.get(i+1);
                    line5.get(i+1).prev = line5.get(i);
                }
            }
            
            /*******************************************************************************/        
            //6号线
            
            for(String s : line6Arr){
                line6.add(new Station(s));
            }
            for(int i =0;i<line6.size();i++){
                if(i<line6.size()-1){
                    line6.get(i).next = line6.get(i+1);
                    line6.get(i+1).prev = line6.get(i);
                }
            }
            
            lineSet.add(line1);
            lineSet.add(line2);
            lineSet.add(line3);
            lineSet.add(line4);
            lineSet.add(line5);
            lineSet.add(line6);
            totalStaion  = line1.size() + line2.size() + line3.size() + line4.size() + line5.size() + line6.size();
            System.out.println("总的站点数量:"+totalStaion);
        }
    }

    Station.java

    package DJSTL;
    
    import java.util.HashMap;
    import java.util.LinkedHashSet;
    import java.util.Map;
     
    public class Station {
        
        private String name; //地铁站名称
        
        public Station prev; //本站在lineNo线上面的前一个站
        
        public Station next; //本站在lineNo线上面的后一个站
        
        //本站到某一个目标站(key)所经过的所有站集合(value),保持前后顺序
        private Map<Station,LinkedHashSet<Station>> orderSetMap = new HashMap<Station,LinkedHashSet<Station>>();
        
        public Station (String name){
            this.name = name;
        }
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
        
        public LinkedHashSet<Station> getAllPassedStations(Station station) {
            if(orderSetMap.get(station) == null){
                LinkedHashSet<Station> set = new LinkedHashSet<Station>(); 
                set.add(this);
                orderSetMap.put(station, set);
            }
            return orderSetMap.get(station);
        }
     
        public Map<Station, LinkedHashSet<Station>> getOrderSetMap() {
            return orderSetMap;
        }
        
        @Override
        public boolean equals(Object obj) {
            if(this == obj){
                return true;
            } else if(obj instanceof Station){
                Station s = (Station) obj;
                if(s.getName().equals(this.getName())){
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        
        @Override
        public int hashCode() {
            return this.getName().hashCode();
        }
    }

    Subway.java

    package DJSTL;
     
    import java.util.ArrayList;
    import java.util.LinkedHashSet;
    import java.util.List;
    import java.util.Scanner;
     
    public class Subway {
        
        private List<Station> outList = new ArrayList<Station>();//记录已经分析过的站点
        
        //计算从s1站到s2站的最短经过路径
        public void calculate(Station s1,Station s2){
            if(outList.size() == DataBuilder.totalStaion){
                System.out.println("找到目标站点:"+s2.getName()+",共经过"+(s1.getAllPassedStations(s2).size()-1)+"站");
                for(Station station : s1.getAllPassedStations(s2)){
                    System.out.print(station.getName()+"->");
                }
                return;
            }
            if(!outList.contains(s1)){
                outList.add(s1);
            }
            //如果起点站的OrderSetMap为空,则第一次用起点站的前后站点初始化之
            if(s1.getOrderSetMap().isEmpty()){
                List<Station> Linkedstations = getAllLinkedStations(s1);
                for(Station s : Linkedstations){
                    s1.getAllPassedStations(s).add(s);
                }
            }
            Station parent = getShortestPath(s1);//获取距离起点站s1最近的一个站(有多个的话,随意取一个)
            if(parent == s2){
                System.out.println("找到目标站点:"+s2+",共经过"+(s1.getAllPassedStations(s2).size()-1)+"站");
                for(Station station : s1.getAllPassedStations(s2)){
                    System.out.print(station.getName()+"->");
                }
                return;
            }
            for(Station child : getAllLinkedStations(parent)){
                if(outList.contains(child)){
                    continue;
                }
                int shortestPath = (s1.getAllPassedStations(parent).size()-1) + 1;//前面这个1表示计算路径需要去除自身站点,后面这个1表示增加了1站距离
                if(s1.getAllPassedStations(child).contains(child)){
                    //如果s1已经计算过到此child的经过距离,那么比较出最小的距离
                    if((s1.getAllPassedStations(child).size()-1) > shortestPath){
                        //重置S1到周围各站的最小路径
                        s1.getAllPassedStations(child).clear();
                        s1.getAllPassedStations(child).addAll(s1.getAllPassedStations(parent));
                        s1.getAllPassedStations(child).add(child);
                    }
                } else {
                    //如果s1还没有计算过到此child的经过距离
                    s1.getAllPassedStations(child).addAll(s1.getAllPassedStations(parent));
                    s1.getAllPassedStations(child).add(child);
                }
            }
            outList.add(parent);
            calculate(s1,s2);//重复计算,往外面站点扩展
        }
        
        //取参数station到各个站的最短距离,相隔1站,距离为1,依次类推
        private Station getShortestPath(Station station){
            int minPatn = Integer.MAX_VALUE;
            Station rets = null;
            for(Station s :station.getOrderSetMap().keySet()){
                if(outList.contains(s)){
                    continue;
                }
                LinkedHashSet<Station> set  = station.getAllPassedStations(s);//参数station到s所经过的所有站点的集合
                if(set.size() < minPatn){
                    minPatn = set.size();
                    rets = s;
                }
            }
            return rets;
        }
        
        //获取参数station直接相连的所有站,包括交叉线上面的站
        private List<Station> getAllLinkedStations(Station station){
            List<Station> linkedStaions = new ArrayList<Station>();
            for(List<Station> line : DataBuilder.lineSet){
                if(line.contains(station)){//如果某一条线包含了此站,注意由于重写了hashcode方法,只有name相同,即认为是同一个对象
                    Station s = line.get(line.indexOf(station));
                    if(s.prev != null){
                        linkedStaions.add(s.prev);
                    }
                    if(s.next != null){
                        linkedStaions.add(s.next);
                    }
                }
            }
            return linkedStaions;
        }
     
        /**
         * desc: How to use the method
         * author chaisson
         * since 2015-5-31
         * version 1.0
         */
        public static void main(String[] args) {
            long t1 = System.currentTimeMillis();
            Subway sw = new Subway();
            Scanner scan=new Scanner(System.in);
            String a=scan.next();
            String b=scan.next();
            sw.calculate(new Station(a), new Station(b));
            long t2 = System.currentTimeMillis();
            System.out.println();
            System.out.println("耗时:"+(t2-t1)+"ms");
        }
    }

    Util.java

    package util;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class util {
        String user="sa";
        String password="zb753951";
        String url="jdbc:sqlserver://localhost:1433;DatabaseName=XVQIU";
        public  Connection getConn(){
            Connection conn=null;
            try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                    conn=DriverManager.getConnection(url, user, password);
            } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }
            return conn;
        }
        public void close(ResultSet rs, Statement state, Connection conn) {
            if(rs!=null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException e)
                {
                    e.printStackTrace();
                }
            }
            if(state!=null)
            {
                try
                {
                    state.close();
                }
                catch(SQLException e)
                {
                    e.printStackTrace();
                }
            }
            if(conn!=null)
            {
                try
                {
                    conn.close();
                }
                catch(SQLException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

    实验截图:

  • 相关阅读:
    《汇编语言》(王爽)课后答案
    宝石迷阵-2019头条笔试题
    变量名拆分 -头条2019笔试题
    幸存者游戏, 数字对生成树, 飞机最低可俯冲高度,整理书架 -paypal笔试题2019
    括号序列, 避嫌抢劫-拼多多笔试题
    趣味字母卡片-拼多多笔试题
    爱健身的小王, 修改矩阵,最长上升子串 -美团2019笔试题
    机器人跳跃问题和毕业旅行-头条2019笔试题
    特征提取-头条2019笔试题
    疏散人群-京东2019笔试题
  • 原文地址:https://www.cnblogs.com/jbwen/p/11071622.html
Copyright © 2011-2022 走看看