Java教程

地铁查询系统

本文主要是介绍地铁查询系统,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一个简单的地铁查询系统,包括按线路查询,按站查询,以及选择起点与终点,输出两点之间的一条最短线路,其中工程量最大的是输出两点之间最短的路线,也是本系统的核心功能。

 

 这是数据库的结构,按章线路查询,在进入index页面时,通过Ajax在后台servlet中读取数据库的内容并通过处理将这些数据变为线路类对象与地铁站类对象。

 

 

 

 将这些对象分别存放在list中,之后存入session,方便以后在其他servlet中使用,

输出两点之间的最短路径是依靠递归完成的,代码如下

package servlet;
import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletConfig;
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 javax.servlet.http.HttpSession;

import com.fasterxml.jackson.databind.ObjectMapper;

import bean.ditie;
@WebServlet("/qimodi000")
public class qimodi000 extends HttpServlet{
public qimodi000() {
super();
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
String name0=request.getParameter("station0");
String name1=request.getParameter("station1");
System.out.println(name0);
System.out.println(name1);
ObjectMapper MAPPER = new ObjectMapper();
HttpSession session=request.getSession();
ArrayList<ArrayList<ditie>> station=(ArrayList<ArrayList<ditie>>) session.getAttribute("station");
ArrayList<ditie> out=new ArrayList<ditie>();
ArrayList<ArrayList<ditie>>outlist=new ArrayList<ArrayList<ditie>>();
for(ArrayList<ditie> dt:station) {
for(int i=0;i<dt.size();i++) {
if(name0.equals(dt.get(i).getName())){
lu(dt.get(i),name1,out,station,outlist);
}
}
}
for(ArrayList<ditie> dt:outlist) {
for(ditie d:dt) {
System.out.print(d.getName()+d.getXianlu()+",");
}
System.out.println();
}
out=min(outlist);
String res=MAPPER.writeValueAsString(out);
System.out.println(res);
response.getWriter().write(res);
}
public void lu(ditie x,String mubiao,ArrayList<ditie> z,ArrayList<ArrayList<ditie>> station00,ArrayList<ArrayList<ditie>> outlist){
if(!chachong(z,x)) {
return;
}
if(z.size()>25) {
return;
}
if(mubiao.equals(x.getName())) {
z.add(x);
ArrayList<ditie> xx=new ArrayList<ditie>();
for(ditie dt:z) {
xx.add(dt);
}
outlist.add(xx);
System.out.println(outlist.size());
// for(ditie dt:z) {
// System.out.print(dt.getName()+dt.getXianlu()+",");
// }
// System.out.println();
z.remove(z.size()-1);
return;
}
z.add(x);
for(int i=0;i<x.size();i++) {
ArrayList<ditie> dt=xinlu(station00,x.getdt(i).getName());
for(ditie xx:dt) {
lu(xx,mubiao,z,station00,outlist);
}
}
z.remove(z.size()-1);
}
public ArrayList<ditie> min(ArrayList<ArrayList<ditie>> station){
ArrayList<ditie> out=null;
for(ArrayList<ditie> dt:station) {
if(out==null||out.size()>dt.size())
out=dt;
}
return out;
}
public ArrayList<ditie> lu(ditie x,String mubiao,ArrayList<ArrayList<ditie>> station00,String[] biaoji) {
String[] biaoji0=null;
if(biaoji==null) {
biaoji0=new String[1];
}else {
if(biaoji.length>10) {
return null;
}
if(!chachong(biaoji,x)) {
return null;
}
biaoji0=new String[biaoji.length+1];
for(int i=0;i<biaoji.length;i++) {
biaoji0[i]=biaoji[i];
}
}
biaoji0[biaoji0.length-1]=x.getName();
for(int i=0;i<biaoji0.length;i++) {
System.out.print(biaoji0[i]+" ");
}
System.out.println();
ArrayList<ditie> out=null;
if(mubiao.equals(x.getName())) {
out=new ArrayList<ditie>();
ditie d=new ditie(x.getName(),x.getXianlu());
out.add(0,d);
}else {
ArrayList<ditie> out00=null;
for(int i=0;i<x.size();i++) {
ArrayList<ditie> dt=xinlu(station00,x.getdt(i).getName());
for(ditie xx:dt) {
ArrayList<ditie> out0=lu(xx,mubiao,station00,biaoji0);
if(out0!=null&&(out00==null||out00.size()>out0.size())&&mubiao.equals(out0.get(out0.size()-1).getName())) {
out00=out0;
}
}
}
if(out00!=null&&(out==null||out00.size()<out.size())) {
out=out00;
ditie d=new ditie(x.getName(),x.getXianlu());
out.add(0,d);
}
}
return out;
}
public boolean chachong(String[] x,ditie d) {
for(int i=0;i<x.length;i++) {
if(x[i].equals(d.getName())) {
return false;
}
}
return true;
}
public boolean chachong(ArrayList<ditie> out,ditie d) {
if(out==null) {
return true;
}
for(ditie d0:out) {
if(d0.getName().equals(d.getName()))
return false;
}
return true;
}
public ArrayList<ditie> xinlu(ArrayList<ArrayList<ditie>> station00,String name) {
ArrayList<ditie> out=new ArrayList<ditie>();
for(ArrayList<ditie> dt:station00) {
for(ditie dt0:dt) {
if(name.equals(dt0.getName())) {
out.add(dt0);
}
}
}
return out;
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
doGet(request,response);
}
public void init(ServletConfig servletconfig) {

}
public void destroy() {

}
public ServletConfig getServletConfig() {
return null;
}
public String getServletinfo() {
return null;
}
}

这篇关于地铁查询系统的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!