合作人:李琼
目标:设计一套地铁开发系统,输入任意两个地方,给出最短路线
方案设计:
第一步做到点击地铁线路显示地铁站点
第二步做到输入起始站,终点站,输出最短路线
源代码:
package Metro; public class Calculat { public String[] judge(int site1, String[][] route, int site2, String[][] route2) { String [] route3; if(route[0][0].equals(route2[0][0])) { route3= Setroute(site1,route,site2); } else { route3= Setroute(site1,route,site2,route2); } return route3; } private String[] Setroute(int site1, String[][] route, int site2) { String [] route3=new String [30]; if(site1>site2) { for(int i=site1,j=0;i>=site2;i--,j++) { route3[j]=route[i-1][2]; } } else { for(int i=site1,j=0;i<=site2;i++,j++) { route3[j]=route[i-1][2]; } } return route3; } private String[] Setroute(int site1, String[][] route, int site2, String[][] route2) { String [] route3=new String [100]; String [] route4=new String [30]; int line2=0,site0; System.out.println(site2); line2=Integer.parseInt(route2[0][0]); site0=Find(route,line2,site1); route3=Setroute(site1,route,site0); site0=Find(route,route2,site0); route4=Setroute(site0,route2,site2); route3=Combine(route3,route4); return route3; } private int Find(String[][] route, String[][] route2, int site0) { int i=0; for(;route2[i][0]!=null;i++) { if(route2[i][2].equals(route[site0-1][2]))break; } return i; } private String[] Combine(String[] route3, String[] route4) { int i=0; String [] route=new String [100]; for(;route3[i]!=null;i++) { route[i]=route3[i]; } for(int j=0;route4[j]!=null;i++,j++) { route[i]=route4[j]; } return route; } private int Find(String[][] route, int line2, int site1) { int [] site=new int [2]; for(int i=0,j=0;route[i][0]!=null&&j<2;i++) { if(route[i][3]!=null&&route[i][3].equals(""+line2)) {site[j]=Integer.parseInt(route[i][1]);j++;} } if(site[1]!=0)site[0]=judge_site(site,site1); return site[0]; } private int judge_site(int[] site, int site1) { int diff1,diff2; diff1=site[0]-site1; diff2=site[1]-site1; if(diff1<0)diff1=0-diff1; if(diff2<0)diff2=0-diff2; if(diff1<diff2) return site[0]; else return site[1]; } }package Metro; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Dao { public String GetSite(int num,String name) { //查找节点 String sql = "select * from site"+num+" where sitename='"+name+"'"; String result=null; Connection conn = DBUtil.getConn(); Statement state=null; ResultSet rs = null; System.out.println(sql); try { state = conn.createStatement(); rs = state.executeQuery(sql); while(rs.next()) { result=rs.getString("id"); } }catch(SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return result; } public String[][] GetRote(String res) { //获取某个表整条路线 String sql = "select * from "+res+" "; String [][] result=new String [50][4]; int i=0; Connection conn = DBUtil.getConn(); Statement state=null; ResultSet rs = null; System.out.println(sql); try { state = conn.createStatement(); rs = state.executeQuery(sql); while(rs.next()) { result[i][0]=res.substring(4, 5); result[i][1]=rs.getString("id"); result[i][2]=rs.getString("sitename"); result[i][3]=rs.getString("transfer"); i++; } }catch(SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return result; } public String search(String strname) { String num=null; int i=1; for(;i<=6;i++) { num=GetSite(i,strname); if(num!=null)break; } return i+" "+num; } } package Metro; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/subway?useSSL=false&serverTimezone=GMT"; public static String db_user = "root"; public static String db_pass = "1234567"; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } /** * 关闭连接 * @param state * @param conn */ public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } package Metro; public class Main { public static void main(String[] args) { //Samerote(); Different(); } private static void Different() { Dao service=new Dao(); Calculat cal=new Calculat(); String Strname="南王"; String Endname="碧海云天"; String site1=null,site2=null; int line1,line2; //开始节点,结束节点所在的线路号 String [][] route=new String [50][4]; String [][] route2=new String [50][4]; String [] route3=new String [100]; line1=Integer.parseInt(service.search(Strname).substring(0, 1)); line2=Integer.parseInt(service.search(Endname).substring(0, 1)); site1=service.search(Strname).substring(2, service.search(Strname).length()); site2=service.search(Endname).substring(2, service.search(Endname).length()); route=service.GetRote("site"+line1); route2=service.GetRote("site"+line2); System.out.println(line1+" "+site1); System.out.println(line2+" "+site2); show(route); show(route2); route3=cal.judge(Integer.parseInt(site1),route,Integer.parseInt(site2),route2); show(route3); } private static void Samerote() { Dao service=new Dao(); Calculat cal=new Calculat(); String [][] route=new String [30][4]; //开始节点所在的线路,(线路号,结点序号,结点名称,换乘线路号) String [][] route2=new String [30][4]; //结束节点所在的线路 String [] route3=new String [50]; //所需要的线路 String site1=null,site2=null; //开始节点,结束节点所在的线路的次序号 String Strname="南王"; String Endname="华医学院"; int num=4; site1=service.GetSite(num,Strname); route=service.GetRote("site"+num); site2=service.GetSite(num,Endname); route2=service.GetRote("site"+num); System.out.println(site1); System.out.println(site2); show(route); show(route2); route3=cal.judge(Integer.parseInt(site1),route,Integer.parseInt(site2),route2); show(route3); } private static void show(String[][] route) { System.out.println(route[0][0]); System.out.println(route[0][1]); for(int i=0;route[i][0]!=null;i++) { System.out.print(route[i][2]+"->"); } System.out.println(); } private static void show(String[] route) { System.out.println("线路"); for(int i=0;route[i]!=null;i++) { System.out.print(route[i]); if(route[i+1]!=null)System.out.print("->"); else System.out.println(); } } }
开发过程中的时间记录日志:
计划 |
预估耗时 |
实际开始时间 |
实际结束时间 |
实际耗时 |
任务估计时间 |
3.5小时 |
3:30pm |
9:25pm |
大约5个小时 |
· 需求分析 |
0.5小时 |
3:30pm |
4:.15pm |
0.75小时 |
· 代码设计说明 |
0.5小时 |
4:25pm |
5:15pm |
40分钟 |
· 具体编码 |
1.5小时 |
6:00pm |
8:00pm |
2小时 |
· 测试并完善代码 |
0.5小时 |
8:pm |
8:45pm |
0.75小时 |
· 过程总结 |
0.5小时 |
8:50pm |
9:20pm |
0.5小时 |
合计 |
3.5小时 |
--- |
--- |
4小时40分钟 |
总结分析:
一个是我们的地铁外观不好看,没有调用百度开源报表
另一方面是算法不够简洁,实现时间较长