课堂练习----结对开发地铁

1.开发一套石家庄地铁线路查询系统。

2.功能设计

1)数据库设计:将石家庄地铁线路图的各个线路,各个站点,换乘信息等用数据库的形式保存起来,应该保存的信息有 {线路号,线路的各个站名,车站的换乘信息}

2)站点查询:用户可以输入任一一条线路或输入出发地和目的地信息,可以查询到相关内容。

例如输入出发地:石家庄铁道大学   目的地 博物院

返回经历的站名的个数,和路径,如果有换乘,请列出换乘的线路

后台:

  1 package com.test.dijkstra;
  2  
  3 import java.util.ArrayList;
  4 import java.util.HashSet;
  5 import java.util.List;
  6 import java.util.Set;
  7  
  8 
  9 public class DataBuilder {
 10     
 11     public static List<Station> line1 = new ArrayList<Station>();//1号线
 12     public static List<Station> line2 = new ArrayList<Station>();//2号线
 13     public static List<Station> line3 = new ArrayList<Station>();//3号线
 14     public static List<Station> line10 = new ArrayList<Station>();//4号线
 15     public static List<Station> lineS1 = new ArrayList<Station>();//5号线
 16     public static List<Station> lineS8 = new ArrayList<Station>();//6号线
 17     public static Set<List<Station>> lineSet = new HashSet<List<Station>>();//所有线集合
 18     public static int totalStaion = 0;//总的站点数量
 19     static {        
 20         //1号线
 21         String line1Str = "上庄南、上庄、西王、时光街、长城桥、和平医院、烈士陵园、新百广场、解放广场、平安大街、北国商城、博物院、体育场、北宋、谈固、朝晖桥、白佛、留村、火炬广场、石家庄东、南村、洨河大道、西庄、东庄、会展中心、行政中心、园博园、天元湖、东上泽、东洋";
 22         String[] line1Arr = line1Str.split("、");
 23         for(String s : line1Arr){
 24             line1.add(new Station(s));
 25         }
 26         for(int i =0;i<line1.size();i++){
 27             if(i<line1.size()-1){
 28                 line1.get(i).next = line1.get(i+1);
 29                 line1.get(i+1).prev = line1.get(i);
 30             }
 31         }
 32         
 33         /*******************************************************************************/
 34         //2号线
 35         String line2Str = "西古城、铁道大学、运河桥、蓝天圣木、长安公园、北国商城、大戏院、新世隆、东岗头、东三教、石家庄站、塔谈、塔谈南、南位、嘉华 ";
 36         String[] line2Arr = line2Str.split("、");
 37         for(String s : line2Arr){
 38             line2.add(new Station(s));
 39         }
 40         for(int i =0;i<line2.size();i++){
 41             if(i<line2.size()-1){
 42                 line2.get(i).next = line2.get(i+1);
 43                 line2.get(i+1).prev = line2.get(i);
 44             }
 45         }
 46         
 47         /*******************************************************************************/
 48         //3号线
 49         String line3Str = "西三庄、水上公园、柏林庄、市庄、市二中、新百广场、东里、槐安桥、西三教、石家庄站、东广场、孙村、塔冢、东王、南王、位同、三教堂、中仰陵、天山大街、南豆、韩通、北乐乡";
 50         String[] line3Arr = line3Str.split("、");
 51         for(String s : line3Arr){
 52             line3.add(new Station(s));
 53         }
 54         for(int i =0;i<line3.size();i++){
 55             if(i<line3.size()-1){
 56                 line3.get(i).next = line3.get(i+1);
 57                 line3.get(i+1).prev = line3.get(i);
 58             }
 59         }
 60         
 61         /*******************************************************************************/        
 62         //10号线
 63         String line10Str = "碧水蓝湾、东良厢、华医学院、法商学院、滨河街、京广东街、塔谈南、南栗、南焦客运站、赵卜口、南王、经济学院、东明商城、儿童医院、北宋、长安医院、建华市场、北翟营、十里铺、南高营、交通驾校";
 64         String[] line10Arr = line10Str.split("、");
 65         for(String s : line10Arr){
 66             line10.add(new Station(s));
 67         }
 68         for(int i =0;i<line10.size();i++){
 69             if(i<line10.size()-1){
 70                 line10.get(i).next = line10.get(i+1);
 71                 line10.get(i+1).prev = line10.get(i);
 72             }
 73         }
 74         
 75         /*******************************************************************************/        
 76         //s1号线
 77         String lineS1Str = "宫家庄、法商学院、东五里、碧海云天、审计厅、益友百货、和平医院、西焦、合作路、农科院、火车北站、市庄、军械学院、蓝天圣木、常青园、建华市场、南翟营\r\n" + 
 78                 "";
 79         String[] lineS1Arr = lineS1Str.split("、");
 80         for(String s : lineS1Arr){
 81             lineS1.add(new Station(s));
 82         }
 83         for(int i =0;i<lineS1.size();i++){
 84             if(i<lineS1.size()-1){
 85                 lineS1.get(i).next = lineS1.get(i+1);
 86                 lineS1.get(i+1).prev = lineS1.get(i);
 87             }
 88         }
 89         
 90         /*******************************************************************************/        
 91         //s8号线
 92         String lineS8Str = "东明商城、国际城、二十里铺、留村、星辰广场、北豆、南豆、东仰陵、北郗马、东佐";
 93         String[] lineS8Arr = lineS8Str.split("、");
 94         for(String s : lineS8Arr){
 95             lineS8.add(new Station(s));
 96         }
 97         for(int i =0;i<lineS8.size();i++){
 98             if(i<lineS8.size()-1){
 99                 lineS8.get(i).next = lineS8.get(i+1);
100                 lineS8.get(i+1).prev = lineS8.get(i);
101             }
102         }
103         
104         lineSet.add(line1);
105         lineSet.add(line2);
106         lineSet.add(line3);
107         lineSet.add(line10);
108         lineSet.add(lineS1);
109         lineSet.add(lineS8);
110         totalStaion  = line1.size() + line2.size() + line3.size() + line10.size() + lineS1.size() + lineS8.size();
111         System.out.println("总的站点数量:"+totalStaion);
112     }
113 }
 1 package com.test.dijkstra;
 2   
 3 import java.util.HashMap;
 4 import java.util.LinkedHashSet;
 5 import java.util.Map;
 6   
 7  
 8 public class Station {
 9      
10     private String name;
11     public Station prev; 
12     public Station next; 
13     private Map<Station,LinkedHashSet<Station>> orderSetMap = new HashMap<Station,LinkedHashSet<Station>>();
14      
15     public Station (String name){
16         this.name = name;
17     }
18   
19     public String getName() {
20         return name;
21     }
22   
23     public void setName(String name) {
24         this.name = name;
25     }
26      
27     public LinkedHashSet<Station> getAllPassedStations(Station station) {
28         if(orderSetMap.get(station) == null){
29             LinkedHashSet<Station> set = new LinkedHashSet<Station>();
30             set.add(this);
31             orderSetMap.put(station, set);
32         }
33         return orderSetMap.get(station);
34     }
35   
36     public Map<Station, LinkedHashSet<Station>> getOrderSetMap() {
37         return orderSetMap;
38     }
39      
40     @Override
41     public boolean equals(Object obj) {
42         if(this == obj){
43             return true;
44         } else if(obj instanceof Station){
45             Station s = (Station) obj;
46             if(s.getName().equals(this.getName())){
47                 return true;
48             } else {
49                 return false;
50             }
51         } else {
52             return false;
53         }
54     }
55      
56     @Override
57     public int hashCode() {
58         return this.getName().hashCode();
59     }
60 }
  1 package com.test.dijkstra;
  2  
  3 import java.util.ArrayList;
  4 import java.util.LinkedHashSet;
  5 import java.util.List;
  6 import java.util.Scanner;
  7  
  8  
  9 
 10 public class Subway {
 11     
 12     private List<Station> outList = new ArrayList<Station>();
 13     public void calculate(Station s1,Station s2){
 14         if(outList.size() == DataBuilder.totalStaion){
 15             System.out.println("找到目标站点:"+s2.getName()+",共经过"+(s1.getAllPassedStations(s2).size()-1)+"站");
 16             for(Station station : s1.getAllPassedStations(s2)){
 17                 System.out.print(station.getName()+"->");
 18             }
 19             return;
 20         }
 21         if(!outList.contains(s1)){
 22             outList.add(s1);
 23         }
 24         if(s1.getOrderSetMap().isEmpty()){
 25             List<Station> Linkedstations = getAllLinkedStations(s1);
 26             for(Station s : Linkedstations){
 27                 s1.getAllPassedStations(s).add(s);
 28             }
 29         }
 30         Station parent = getShortestPath(s1);
 31         if(parent == s2){
 32             System.out.println("找到目标站点:"+s2+",共经过"+(s1.getAllPassedStations(s2).size()-1)+"站");
 33             for(Station station : s1.getAllPassedStations(s2)){
 34                 System.out.print(station.getName()+"->");
 35             }
 36             return;
 37         }
 38         for(Station child : getAllLinkedStations(parent)){
 39             if(outList.contains(child)){
 40                 continue;
 41             }
 42             int shortestPath = (s1.getAllPassedStations(parent).size()-1) + 1;
 43             if(s1.getAllPassedStations(child).contains(child)){
 44                 if((s1.getAllPassedStations(child).size()-1) > shortestPath){
 45                     s1.getAllPassedStations(child).clear();
 46                     s1.getAllPassedStations(child).addAll(s1.getAllPassedStations(parent));
 47                     s1.getAllPassedStations(child).add(child);
 48                 }
 49             } else {         s1.getAllPassedStations(child).addAll(s1.getAllPassedStations(parent));
 50                 s1.getAllPassedStations(child).add(child);
 51             }
 52         }
 53         outList.add(parent);
 54         calculate(s1,s2);
 55     }
 56     private Station getShortestPath(Station station){
 57         int minPatn = Integer.MAX_VALUE;
 58         Station rets = null;
 59         for(Station s :station.getOrderSetMap().keySet()){
 60             if(outList.contains(s)){
 61                 continue;
 62             }
 63             LinkedHashSet<Station> set  = station.getAllPassedStations(s);
 64             if(set.size() < minPatn){
 65                 minPatn = set.size();
 66                 rets = s;
 67             }
 68         }
 69         return rets;
 70     }
 71     private List<Station> getAllLinkedStations(Station station){
 72         List<Station> linkedStaions = new ArrayList<Station>();
 73         for(List<Station> line : DataBuilder.lineSet){
 74             if(line.contains(station)){
 75                 Station s = line.get(line.indexOf(station));
 76                 if(s.prev != null){
 77                     linkedStaions.add(s.prev);
 78                 }
 79                 if(s.next != null){
 80                     linkedStaions.add(s.next);
 81                 }
 82             }
 83         }
 84         return linkedStaions;
 85     }
 86 
 87     public static void main(String[] args) {
 88         long t1 = System.currentTimeMillis();
 89         Subway sw = new Subway();
 90          Scanner sc = new Scanner(System.in); 
 91          System.out.println("请输入起点站:"); 
 92          String s1 = sc.nextLine();
 93          System.out.println("请输入终点站:"); 
 94          String s2 = sc.nextLine(); 
 95         sw.calculate(new Station(s1), new Station(s2));
 96         long t2 = System.currentTimeMillis();
 97         System.out.println();
 98         System.out.println("耗时:"+(t2-t1)+"ms");
 99     }
100 }

项目并不完善。

posted @ 2019-04-01 19:03  ZZKZS  阅读(174)  评论(0编辑  收藏  举报
/*鼠标跟随效果*/