设计思想:地铁售票系统的关键点在于换乘,所以首先要分为换乘和不换乘两种情况。不换乘比较简单,通过起始站名和终点站名查询他们的num,然后list打包输出到jsp就可以。换乘的话就先要找到两条线路,找到两条线路的交点也就是换乘站,然后分别输出起始站到换乘站,换乘站到终点站两段路线就完成了,这里面还涉及到一个最短路径问题,我的想法是把全部的可能线路都找到,然后比较大小就完成了。目前进度到换乘部分。
双人项目合作人:郑锦
部分源代码:
package Dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import connection.DBUtil; public class dao { /** * 通过name得到number(线路号) * @param name * @return */ public static int getNum(String name) { String sql = "select xianluhao from aaa where name ='" + name + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; int number=0; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { number = rs.getInt("xianluhao"); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return number; } /** * 通过name得到zhanhao(站台号) * @param name * @return */ public static int getZhanhao(String name) { String sql = "select num from aaa where name ='" + name + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; int zhanhao=0; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { zhanhao = rs.getInt("num"); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return zhanhao; } public static String getLine1(int zhanhao1,int zhanhao2) { String line=""; String sql = "select name from aaa where num between '"+zhanhao1+"' and '"+zhanhao2+"'order by num ASC ";//升序 Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); if(rs.next()) line=rs.getString("name"); while (rs.next()) { String name=rs.getString("name"); line=line+"->"+name; } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return line; } public static String getLine2(int zhanhao1,int zhanhao2) { String line=""; String sql = "select name from aaa where num between '"+zhanhao1+"' and '"+zhanhao2+"' order by num DESC ";//降序 Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); if(rs.next()) line=rs.getString("name"); while (rs.next()) { String name=rs.getString("name"); line=line+"->"+name; } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return line; }}
package servlet; import java.io.IOException; 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 Dao.dao; @WebServlet("/servlet") public class servlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("chaxun".equals(method)) { chaxun(req, resp); } } private void chaxun(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); String qi=req.getParameter("qi"); String zhong=req.getParameter("zhong"); int zhanhao1=dao.getZhanhao(qi); int zhanhao2=dao.getZhanhao(zhong); int number1=dao.getNum(qi); int number2=dao.getNum(zhong); if(number1==number2) { if(zhanhao1<zhanhao2) { String line=dao.getLine1(zhanhao1, zhanhao2); req.setAttribute("line",line ); req.setAttribute("num",number1); req.getRequestDispatcher("list.jsp").forward(req,resp); }if(zhanhao1>zhanhao2) { String line=dao.getLine2(zhanhao2, zhanhao1); System.out.print(line); req.setAttribute("num",number1); req.setAttribute("line",line ); req.getRequestDispatcher("list.jsp").forward(req,resp); } }}}
实验截图
项目总结分析
这个项目因为涉及到一个最短路径的问题,最开始我是想用迪杰斯特拉算法来解决这个问题。但是我尝试了很久没有成功,可能是我的水平还是太有限。所以最后用了最简单的方法来解决这个问题。我了解到有的同学是用迪杰斯特拉算法完成了这个项目,所以还是去请教一下。后期有时间的话还是想改善一下我程序的算法。