//首先把要的数据导入到数据库中
//数据库名bus,建立数据表bus
//表bus结构如:
/**
* **********************************************************************************************
* id(自动编号 pk) busline(公交线路 varchar(8)) station_name(站点名称 varchar 20) )
* 1 1 火车站 1
* 2 1 胜利广场 2
* 3 1 卖渔桥 3
* ... ... ... ...
* 25 2 胜利广场 1
* 26 2 天香电器城 2
* 27 2 五里墩 3
* ... ... ... ...
* 数据库就是这样插记录的,可以把城市的公交线路数据全部插进去
************************************************************************************************
*/
package lzm;
import java.sql.*;
import java.io.*;
public class BusToBus {
Connection conn;
Statement stm;
ResultSet rs;
String conUser = "lzm";
String conPsw = "lzm";
String station_total[];
// 数据库连接操作
void ConnectDatabase() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// sun.jdbc.odbc.JdbcOdbcDriver
} catch (ClassNotFoundException e) {
e.getMessage();
System.out.println("数据库程序加载错误");
}
//
try {
conn = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;DatabaseName=mcm",// 一会在改吧,名字肯定不能是它~
conUser, conPsw);
} catch (SQLException e) {
System.out.println("数据库连不上啊,怎么工作!!!!");
}
}
// 统计数据库中所有公交站点名,存入数组
// 返回站点名
public String[] getStation() {
String station_name[] = new String[10000];// 一万够大了吧-----------------------------------
int count = 0;
String sql = "select * from bus ";// group by station_name";//
// 建表时注意统一,一会看看重名的影响
try {
stm = conn.createStatement();
rs = stm.executeQuery(sql);
// System.out.println("ddddd");
while (rs.next()) {
station_name[count] = rs.getString("station_name");// 建表时注意统一
count++;
}
} catch (SQLException e) {
System.out.println("数据库执行时发生错误1!!!!");
// return null;
}
return station_name;
}
// 统计数据库中所有公交路线,存入数组
// 返回公交线路
public String[] getBusline() {
String busline[] = new String[1000];// 这个一千就够了吧--------------------
int count = 0;
String sql = "select * from bus";// group by busline";
try {
stm = conn.createStatement();
rs = stm.executeQuery(sql);
while (rs.next()) {
busline[count] = rs.getString("busline");// 建表时注意统一
count++;
}
} catch (SQLException e) {
System.out.println("数据库执行时发生错误2!!!");
}
return busline;
}
// 统计数据库中每一线路经过的站点,存入数组
// 需要参数line,区别每一路车
// 返回站点名
public String[] stationPerLine(String line) {
String station_per_line[] = new String[1000];// 一个公交1000个站点够了吧
String sql = "select * from bus where busline ='line'";// 建表时注意统一
int count = 0;
try {
stm = conn.createStatement();
rs = stm.executeQuery(sql);
while (rs.next()) {
station_per_line[count] = rs.getString("station_name");
count++;
}
} catch (SQLException e) {
System.out.println("数据执行错误3!!!!");
}
return station_per_line;
}
// 统计经过某站点的所有公交车的组合
// 需要参数station,表示经过的站点
// 返回公交线路
public String[] linePerStation(String station) {
String line_per_station[] = new String[1000];// 一千就够了,不够在加
String sql = "select * from bus where station_name = 'station' ";// group
// by
// busline";
int count = 0;
try {
stm = conn.createStatement();
rs = stm.executeQuery(sql);
while (rs.next()) {
line_per_station[count] = rs.getString("station_name");
count++;
}
} catch (SQLException e) {
System.out.println("数据库执行错误4!!!");
}
return line_per_station;
}
// 实现换乘算法的函数
// 需要提供参数,查询的起点和终点
public void busStationToStation(String start, String end) {
boolean start_flag, end_flag;
start_flag = false;
end_flag = false;
String station_total[] = this.getStation();
String busline_total[] = this.getBusline();
// 判断数据库中是否有此站点
for (int i = 0; i < station_total.length; i++) {
if (start.equals(station_total[i]))
start_flag = true;
if (end.equals(station_total[i]))
end_flag = true;
if (start_flag && end_flag)
break;
}
if (!(start_flag && end_flag)) {
System.out.println("大哥,站点都不存在还找什么啊??????");
}
// 两个站点都存在的情况
// 首先判断有无直达车
String line_temp = "";
for (int j = 0; j < busline_total.length; j++) {
int direct_flag = 0;
String station_per_line[] = this.stationPerLine(busline_total[j]);
for (int k = 0; k < station_per_line.length; k++) {
if (start.equals(station_per_line[k]))
direct_flag++;
if (end.equals(station_per_line[k]))
direct_flag++;
if (direct_flag == 2) {
System.out.println("有直达");
break;
}
}
if (direct_flag == 2) {
line_temp = busline_total[j];
System.out.println(line_temp + "可直接到达这两个站点");// 这块可以改成字符串链接-----------------
}
}
// 没有直达车
if (line_temp == "") {
System.out.println("没有直达车,下面进行一次中转的算法");
String start_pass[] = this.linePerStation(start);
String end_pass[] = this.linePerStation(end);
String station_temp = "";// 用来保存站点用的,最后用+连成路径,一次输出三个站点
for (int s = 0; s < start_pass.length; s++) {
for (int e = 0; e < end_pass.length; e++) {
// 判断两条线路有没有交叉点
String start_per_line[] = this.stationPerLine(start_pass[s]);
String end_per_line[] = this.stationPerLine(end_pass[e]);
for (int ss = 0; ss < start_per_line.length; ss++)
for (int ee = 0; ee < end_per_line.length; ee++) {
if ((start_per_line[ss])==(end_per_line[ee])) {
// 成功找到交叉点后
// 存储交叉点处信息
// 此只为一次换乘
// System.out.print("lzm");
String first_line = start_pass[s];
String second_line = end_pass[e];
String change_station = start_per_line[ss];
station_temp += "@..@" + first_line
+ second_line + change_station + "@..@";
}
}
}
}
if (station_temp != "")
System.out.println("乘车路线如下:\n" + station_temp);
else
System.out.println("你是到不了了,只陈一次车,呵呵");
}
}
//
public static void main(String args[]) {
BusToBus bus = new BusToBus();
bus.ConnectDatabase();
String s, e;
try {
BufferedReader bfr = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("请输入起始站点:");
s = bfr.readLine();
System.out.print("请输入结束站点:");
e = bfr.readLine();
s = "S1915 ";
e = "S1914 ";
bus.busStationToStation(s, e);
} catch (Exception aaa) {
System.out.println("IO错误,人品问题~");
}
}
}