地铁售票系统设计思想及部分代码
? ? ?設(shè)計(jì)思想:地鐵售票系統(tǒng)的關(guān)鍵點(diǎn)在于換乘,所以首先要分為換乘和不換乘兩種情況。不換乘比較簡(jiǎn)單,通過(guò)起始站名和終點(diǎn)站名查詢他們的num,然后list打包輸出到j(luò)sp就可以。換乘的話就先要找到兩條線路,找到兩條線路的交點(diǎn)也就是換乘站,然后分別輸出起始站到換乘站,換乘站到終點(diǎn)站兩段路線就完成了,這里面還涉及到一個(gè)最短路徑問(wèn)題,我的想法是把全部的可能線路都找到,然后比較大小就完成了。目前進(jìn)度到換乘部分。
? ? ?雙人項(xiàng)目合作人:鄭錦
? ? ?部分源代碼:
package Dao;import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement;import connection.DBUtil;public class dao {/*** 通過(guò)name得到number(線路號(hào))* @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;}/*** 通過(guò)name得到zhanhao(站臺(tái)號(hào))* @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);} }}}
實(shí)驗(yàn)截圖
項(xiàng)目總結(jié)分析
這個(gè)項(xiàng)目因?yàn)樯婕暗揭粋€(gè)最短路徑的問(wèn)題,最開始我是想用迪杰斯特拉算法來(lái)解決這個(gè)問(wèn)題。但是我嘗試了很久沒(méi)有成功,可能是我的水平還是太有限。所以最后用了最簡(jiǎn)單的方法來(lái)解決這個(gè)問(wèn)題。我了解到有的同學(xué)是用迪杰斯特拉算法完成了這個(gè)項(xiàng)目,所以還是去請(qǐng)教一下。后期有時(shí)間的話還是想改善一下我程序的算法。
?
轉(zhuǎn)載于:https://www.cnblogs.com/xuange1/p/10652328.html
總結(jié)
以上是生活随笔為你收集整理的地铁售票系统设计思想及部分代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: vue.js的项目实战
- 下一篇: java信息管理系统总结_java实现科