地铁线路查询的总结分析

设计思想:

站点查询为数据库查询操作,通过输入站点名称,输出线路

线路查询为数据库list查询操作,通过输入线路名称。输出当前输入线路所有站点

最短路线为BFS广度优先遍历,输入起点终点,输出经过线路,转站次数,转站点。

界面添加当前时间,添加跳转链接,将三个查询总结到同一个界面,输出在查询下方。

全国地图为高德地图,在已有的北京、石家庄有marker点,点击可以跳转到所在城市的地铁图。

下面是代码:

package result;

import java.io.IOException;
import java.util.List;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/Servse")
public class Servse extends HttpServlet{
    Dao d=new Dao();
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String method=request.getParameter("method");
        if (method==null){
            method="findAll";
        }
        switch (method){
        case "find":
            String nol=request.getParameter("nol");
            List<String> l=d.find(nol);
            request.setAttribute("test", l);
               request.getRequestDispatcher("xian.jsp").forward(request, response);
            break;
        case "findd":
            String name=request.getParameter("name");
            List<String> li=d.findd(name);
            request.setAttribute("test", li);
               request.getRequestDispatcher("zhan.jsp").forward(request, response);
            break;
        case "min":
            String start=request.getParameter("start");
            String end=request.getParameter("end");
            List<String> lis=d.result(start,end);
            request.setAttribute("test", lis);
               request.getRequestDispatcher("zui.jsp").forward(request, response);
            break;
    }
    }
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        doPost(request,response);
}
}
package result;

import line.Tool;
import java.util.*;

import Contrl.BFS;
import Contrl.Read;
import Model.Route;
import Model.Station;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Dao {
    // 获取该线路的所有站点
    public List<String> find(String nol) {
        List<String> list = new ArrayList<>();
        Connection conn = Tool.getConnection();
        PreparedStatement pre = null;
        ResultSet res = null;
        String sql = "SELECT *FROM ditie1 where xianlu=? ";
        try {
            pre = conn.prepareStatement(sql);
            pre.setString(1, nol);
            res = pre.executeQuery();
            while (res.next()) {
                String num1 = res.getString("zhandian");
                list.add(num1);
            }
        } catch (SQLException e) {

            e.printStackTrace();
        } finally {
            Tool.release(conn, pre, res);
        }
        return list;
    }

//获取站点所在的线路
    public List<String> findd(String name) {
        List<String> list = new ArrayList<>();
        Connection conn = Tool.getConnection();
        PreparedStatement pre = null;
        ResultSet res = null;
        String sql = "SELECT *FROM ditie1 where zhandian=? ";
        try {
            pre = conn.prepareStatement(sql);
            pre.setString(1, name);
            res = pre.executeQuery();
            while (res.next()) {
                String hao = res.getString("xianlu");
                list.add(hao);
            }
        } catch (SQLException e) {

            e.printStackTrace();
        } finally {
            Tool.release(conn, pre, res);
        }
        return list;
    }


    public ArrayList<String> result(String name1, String name2) {
        BFS bfs = new BFS();
        String start = name1;
        Read re = new Read();
        ArrayList<Station> station = re.getStation();
        ArrayList<Route> route = re.getRoute();
        station = bfs.FindMin(start, station);
        String end = name2;
        ArrayList<String> str = new ArrayList<>();
        str = bfs.shortPath(end, station, str);
        return str;
    }
}
package line;
import java.sql.*;
public class Tool {
private static Connection connection;
private static String url="jdbc:mysql://localhost:3306/jiaqi?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
private static String user="roots";
private static String pass="123456";
static {
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
    }catch(ClassNotFoundException e){
        e.printStackTrace();
    }
}
public static Connection getConnection() {
    try {
        connection=DriverManager.getConnection(url,user,pass);
    }catch(SQLException e) {
        e.printStackTrace();
    }
    return connection;
}
public static void release(Connection connection,Statement statement,ResultSet resultSet) {
    try {
        if(connection!=null) {
            connection.close();
        }
        if(statement!=null) {
            statement.close();
        }
        if(resultSet!=null) {
            resultSet.close();
        }
    }
    catch(SQLException e) {
        e.printStackTrace();
    }
}
}
package Model;

import java.util.*;

public class Route {
    private String rname;
    private ArrayList<String> route = new ArrayList<>();
    
    public String getRname() {
        return rname;
    }
    public void setRname(String rname) {
        this.rname = rname;
    }

    public void addRoute(String sname) {
        this.route.add(sname);
    }

    public String allRoute() {
        String result="";
        
        for(int i=0;i<route.size();i++) {
            result+=route.get(i)+" ";
        }
        
        return result.trim();
    }
    public ArrayList<String> getRoute() {
        return this.route;
    }
}
package Model;

import java.util.*;

public class Station {
    public final static int MaxDist = 65535;
    private String sname;//站名
    private ArrayList<String> bTR = new ArrayList<>();//线路名
    private ArrayList<String> bs = new ArrayList<>();//邻站(距离为1的站)

    
    //执行算法后更改
    private Station ps;//前一个站点
    private int dist;//距离(距起始站)
    private int transNum;//换乘数
    private int visited;//保存是否访问
    
    public Station() {
        this.dist=MaxDist;
        this.transNum=0;
        this.visited=0;
    }
    
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    //站所属路线(可能有多个)
    public void setBTR(String belongToRname) {
        this.bTR.add(belongToRname);
    }
    //所属路线输出(用于算法)
    public ArrayList<String> getBTR() {
        return this.bTR;
    }
    //相邻站录入
    public void setBs(String sname) {
        for(int i=0;i<this.bs.size();i++) {
            if(this.bs.get(i).equals(sname)) {
                return;
            }
        }
        this.bs.add(sname);
    }
    //相邻站输出(用于算法)
    public ArrayList<Station> getBs(ArrayList<Station> station){
        ArrayList<Station> result = new ArrayList<>();
        for(int i=0;i<this.bs.size();i++) {
            String tmp = this.bs.get(i);
            for(int j=0;j<station.size();j++) {
                if(station.get(j).getSname().equals(tmp)) {
                    result.add(station.get(j));
                    break;
                }
            }
        }
        return result;
    }
    public int getVisited() {
        return visited;
    }

    public void setVisited(int visited) {
        this.visited = visited;
    }

    //执行算法后更改
    public Station getPs() {
        return ps;
    }
    public void setPs(Station ps) {
        this.ps = ps;
    }
    public int getDist() {
        return dist;
    }
    public void setDist(int dist) {
        this.dist = dist;
    }
    public int getTransNum() {
        return transNum;
    }
    public void setTransNum(int transNum) {
        this.transNum = transNum;
    }
}

 

 

 总结分析:需要在网上学习不会的。需要研究许多的代码。

 

posted @ 2023-03-27 18:44  一个小虎牙  阅读(48)  评论(0编辑  收藏  举报