? ? ?設計思想:地鐵售票系統的關鍵點在于換乘,所以首先要分為換乘和不換乘兩種情況。不換乘比較簡單,通過起始站名和終點站名查詢他們的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 stubreq.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);}
}}}
實驗截圖
項目總結分析
這個項目因為涉及到一個最短路徑的問題,最開始我是想用迪杰斯特拉算法來解決這個問題。但是我嘗試了很久沒有成功,可能是我的水平還是太有限。所以最后用了最簡單的方法來解決這個問題。我了解到有的同學是用迪杰斯特拉算法完成了這個項目,所以還是去請教一下。后期有時間的話還是想改善一下我程序的算法。
?