地铁查询系统

1、数据库设计: 1.1存储:线路号,车站唯一标识ID,线路的各个站名,车站的换乘信息等信息。

1.2需要考虑:如何表示两个小白圆点中间有一条线表示这两个车站是相邻的;这个车站可以转 5 号线等信息。

2、功能设计: 线路查询:输入线路号,输出此地铁线路的所有站名(按某一方向顺序输出即可)。 站点查询:输入站点名称,输出途径该站点的线路名称。 起点-终点查询:输入起点、终点,找到两点之间的最有效线路 起点:知春路 终点:中关村 返回经历的站名的个数,和路径,如果有换乘,请列出换乘的线路, 例如: 4 知春路(地铁10号线) 知春里 海淀黄庄 换乘4号线(安定桥河北方向) 中关村

2、优化返回两个站点之间最短路径功能:成为一个类,进行单元测试。

2、生成遍历车站类:要求尽可能快地遍历地铁的所有车站呢(只用经过一次,不用下车,就算经过车站)。 例如,注意到13号线 和 10 号线相交的知春路地铁站,选它作为一个起始站,从这个站出发,要经历多少站(换乘不出地铁系统,即不能从一个地铁口走到路面,然后从另一个站进去),才能把所有地铁站都遍历呢? 输入起始站,输出总共经历多少站,以及经历的站名。 举一个特例,假如地铁系统只有知春路, 西土城两个站, 那么这个程序应该输出:   

3   知春路   西土城   知春路

代码部分

station.java

public class Station {
    private String stationname;
    private String stationid;
    private String front;
    private String after;
    private String line1;
    private String line2;
    private String line3;

    @Override
    public String toString() {
        return "Station{" +
                "stationname='" + stationname + '\'' +
                ", stationid='" + stationid + '\'' +
                ", front='" + front + '\'' +
                ", after='" + after + '\'' +
                ", line1='" + line1 + '\'' +
                ", line2='" + line2 + '\'' +
                ", line3='" + line3 + '\'' +
                '}';
    }

    public Station() {
    }

    public String getStationname() {
        return stationname;
    }

    public void setStationname(String stationname) {
        this.stationname = stationname;
    }

    public String getStationid() {
        return stationid;
    }

    public void setStationid(String stationid) {
        this.stationid = stationid;
    }

    public String getFront() {
        return front;
    }

    public void setFront(String front) {
        this.front = front;
    }

    public String getAfter() {
        return after;
    }

    public void setAfter(String after) {
        this.after = after;
    }

    public String getLine1() {
        return line1;
    }

    public void setLine1(String line1) {
        this.line1 = line1;
    }

    public String getLine2() {
        return line2;
    }

    public void setLine2(String line2) {
        this.line2 = line2;
    }

    public String getLine3() {
        return line3;
    }

    public void setLine3(String line3) {
        this.line3 = line3;
    }

    public Station(String stationname, String stationid, String front, String after, String line1, String line2, String line3) {
        this.stationname = stationname;
        this.stationid = stationid;
        this.front = front;
        this.after = after;
        this.line1 = line1;
        this.line2 = line2;
        this.line3 = line3;
    }
}

Stationdao.java

import bean.Station;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import util.DButil;

import java.sql.SQLException;
import java.util.List;

public class Stationdao {
    private QueryRunner queryRunner = new QueryRunner(DButil.getDruidDataSource());
    public List<Station> select(String stationname){
        List<Station> station = null;
        try {
            station = queryRunner.query("select * from station where stationname=?",new BeanListHandler<Station>(Station.class),stationname);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return station;
    }
    public List<Station> selectall(){
        List<Station> stationList = null;
        try {
            stationList = queryRunner.query("select * from station",new BeanListHandler<Station>(Station.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return stationList;
    }

    public List<Station> selectline(String line) {
        List<Station> stationList1 = null;
        List<Station> stationList2 = null;
        List<Station> stationList3 = null;

        try {
            stationList1 = queryRunner.query("select * from station where line1=?", new BeanListHandler<Station>(Station.class),line);
            stationList2 = queryRunner.query("select * from station where line2=?", new BeanListHandler<Station>(Station.class),line);
            stationList3 = queryRunner.query("select * from station where line3=?", new BeanListHandler<Station>(Station.class),line);
            for(int i=0;i<stationList2.size();i++)
            {
                stationList1.add(stationList2.get(i));
            }
            for(int i=0;i<stationList3.size();i++)
            {
                stationList1.add(stationList3.get(i));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return stationList1;
    }
}

Huancheng.java

import bean.Station;
import dao.Stationdao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/Huancheng")
public class Huancheng extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String start = req.getParameter("start");
        String last = req.getParameter("last");
        String panduan = null;
        List<Station> stationList = new ArrayList<>();
        List<Station> stationList1 = new ArrayList<>();
        List<Station> stationstart = null;
        List<Station> stationlast = null;
        Stationdao stationdao = new Stationdao();
        stationstart = stationdao.select(start);
        stationlast = stationdao.select(last);
        if (stationstart.size() == 0 || stationlast.size() == 0) {
//            System.out.println(start);
            panduan = "no";
            req.setAttribute("panduan", panduan);
            req.getRequestDispatcher("/huancheng1.jsp").forward(req, resp);
        } else {
            boolean one = stationstart.get(0).getLine1().equals(stationlast.get(0).getLine1());
            boolean two = stationstart.get(0).getLine1().equals(stationlast.get(0).getLine2());
            boolean three = stationstart.get(0).getLine1().equals(stationlast.get(0).getLine3());
            boolean four = stationstart.get(0).getLine2().equals(stationlast.get(0).getLine2());
            boolean fire = stationstart.get(0).getLine2().equals(stationlast.get(0).getLine3());
            boolean six = stationstart.get(0).getLine3().equals(stationlast.get(0).getLine3());
            boolean finish = false;
            String station1 = null;
            if (one) {
                finish = true;
                station1 = stationstart.get(0).getLine1();
            }
//            else {
//                if (two) {
//                    finish = true;
//                    station1 = stationstart.get(0).getLine1();
//                } else {
//                    if (three) {
//                        finish = true;
//                        station1 = stationstart.get(0).getLine1();
//                    } else {
//                        if (four) {
//                            finish = true;
//                            station1 = stationstart.get(0).getLine2();
//                        } else {
//                            if (fire) {
//                                finish = true;
//                                station1 = stationstart.get(0).getLine2();
//                            } else {
//                                if (six) {
//                                    finish = true;
//                                    station1 = stationstart.get(0).getLine3();
//                                }
//                            }
//                        }
//                    }
//                }
//            }
            System.out.println(finish);

            if (finish) {
                stationList = stationdao.selectline(station1);
                int i = 0;
                boolean temp = true;
                while (temp) {
                    Station station = stationList.get(i);
                    if (start.equals(station.getStationname()) || last.equals(station.getStationname())) {
                        temp = false;
                    }
                    i++;
                }

                temp = true;
                i = i - 1;
                while (temp) {
                    Station station = stationList.get(i);
                    stationList1.add(station);

                    if (stationList1.size() != 1) {
                        if (start.equals(station.getStationname()) || last.equals(station.getStationname())) {
                            temp = false;
                        }
                    }
                    i++;
                }
                req.setAttribute("start", start);
                req.setAttribute("last", last);
                req.setAttribute("StationList1", stationList1);
                req.getRequestDispatcher("/huancheng1.jsp").forward(req, resp);
            } else {
//                System.out.println("0");
                boolean temp = true; //控制器
                Station station = null;
                while (temp) {
                    stationList.add(stationstart.get(0));
                    stationstart = stationdao.select(stationstart.get(0).getAfter());
                    System.out.println(stationstart.get(0));
                    if (!(stationstart.get(0).getLine2().equals("无"))) {
//                        System.out.println("2");
                        System.out.println(stationlast.get(0));
                        if (stationstart.get(1).getLine2().equals(stationlast.get(0).getLine1())) {
//                            System.out.println("3");
//                            if (stationstart.get(1).getLine1().equals(last)) {
//                                System.out.println("4");
//                                station = stationstart.get(0);
//                            } else {
//                                System.out.println("5");
//                                station = stationstart.get(1);
//                            }
                            stationList.add(stationstart.get(0));
//                            System.out.println(stationstart.get(0));
//                            System.out.println(stationstart.get(1));
                            stationstart = stationdao.select(stationstart.get(0).getAfter());
                            while (temp) {
                                System.out.println("4");
                                stationList.add(stationstart.get(0));
                                stationstart = stationdao.select(stationstart.get(0).getAfter());
                                System.out.println(stationstart.get(0));
                                if (stationstart.get(0).getStationname().equals(last)) {
                                    stationstart.add(stationlast.get(0));
                                    temp = false;
                                }
                            }
                        }
                    }
                }
                req.setAttribute("start", start);
                req.setAttribute("last", last);
                req.setAttribute("StationList1", stationList);
                req.getRequestDispatcher("/huancheng1.jsp").forward(req, resp);
            }

        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

line,java

import bean.Station;
import dao.Stationdao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/line")
public class line extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String line = null;
        line = req.getParameter("line");
//        System.out.println(line);
        List<Station> stationList = null;
        Stationdao stationdao = new Stationdao();
        stationList = stationdao.selectline(line);
//        System.out.println(stationList.get(0));
        req.setAttribute("stationList",stationList);
        req.getRequestDispatcher("/line.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

Result.java

import java.util.Arrays;

public class Result {
    private static final int max = 999999;
    private int[][] ShortestPath;
    private int[][] ShortestDis;

    public Result() {
    }

    public int[][] getShortestPath() {
        return ShortestPath;
    }

    public void setShortestPath(int[][] shortestPath) {
        ShortestPath = shortestPath;
    }

    public int[][] getShortestDis() {
        return ShortestDis;
    }

    public void setShortestDis(int[][] shortestDis) {
        ShortestDis = shortestDis;
    }

    @Override
    public String toString() {
        return "Result{" +
                "ShortestPath=" + Arrays.toString(ShortestPath) +
                ", ShortestDis=" + Arrays.toString(ShortestDis) +
                '}';
    }

}

Zhuanzhan.java

package servlet;

import bean.Station;
import dao.Stationdao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/Zhandian")
public class Zhandian extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String stationname = null;
        stationname = req.getParameter("stationname");
        String panduan = "no";
        if(stationname == null)
        {
            stationname = "为空";
            req.setAttribute("stationname",stationname);
            req.getRequestDispatcher("/zhandian1.jsp").forward(req,resp);
        }
        List<Station> station = null;
        Stationdao stationdao = new Stationdao();
        station = stationdao.select(stationname);
        if(station.size() != 0)
        {
            req.setAttribute("station",station);
            req.getRequestDispatcher("/zhandian1.jsp").forward(req,resp);
        }else {
            req.setAttribute("panduan",panduan);
            req.getRequestDispatcher("/zhandian1.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

DButil.java

package util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DButil {
    private static DruidDataSource druidDataSource;
    static {
        Properties properties = new Properties();
        InputStream inputStream = DButil.class.getResourceAsStream("/datebase.properties");
        try {
            properties.load(inputStream);
            try {
                druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection()
    {
        try {
            return druidDataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void closeall(Connection connection, Statement statement, ResultSet resultSet)
    {
        try {
            if(resultSet != null)
                resultSet.close();
            if(statement != null)
                statement.close();
            if(connection != null)
                connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static DruidDataSource getDruidDataSource()
    {
        return druidDataSource;
    }
}
地铁查询系统,最困难的在于找出最短路径,我首先想到的是迪杰斯特拉算法,但是由于我水平实在有限,我最终还是没有写出来,我最后采取了自己的想法,先暴力查询,之后再进行筛选,因为地铁站总数并不多
所以这种算法有可行之处
posted @ 2022-06-14 13:11  软工小蜗牛  阅读(924)  评论(0编辑  收藏  举报