地铁系统

队员:赵家明

我写了上图连接数据库和建建各种需要的变量的代码,在数据库中id作为主码,line中存放各个站点所在的线路,num存放需要经过的站点,name存放各站点的名称,exchange存放转站时的下一条线路。

在设计算法的时候,最开始我们是打算建两张表,一张表存站点,一张表存转站的站点,转站的表中有一个变量存放与它紧邻的站点及路线,将每个站点标号,当遇到需要换乘的站点时,查询在两条线路相交的点,但算法设计没有完全实现,后面又改了一种想法,也是借鉴了在网上看到的一些方法

package DJSTL;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
 

public class Station {
    
    private String name; //地铁站名称,假设具备唯一性
    
    public Station prev; //本站在lineNo线上面的前一个站
    
    public Station next; //本站在lineNo线上面的后一个站
    
    //本站到某一个目标站(key)所经过的所有站集合(value),保持前后顺序
    private Map<Station,LinkedHashSet<Station>> orderSetMap = new HashMap<Station,LinkedHashSet<Station>>();
    
    public Station (String name){
        this.name = name;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
    
    public LinkedHashSet<Station> getAllPassedStations(Station station) {
        if(orderSetMap.get(station) == null){
            LinkedHashSet<Station> set = new LinkedHashSet<Station>(); 
            set.add(this);
            orderSetMap.put(station, set);
        }
        return orderSetMap.get(station);
    }
 
    public Map<Station, LinkedHashSet<Station>> getOrderSetMap() {
        return orderSetMap;
    }
    
    
    public boolean equals(Object obj) {
        if(this == obj){
            return true;
        } else if(obj instanceof Station){
            Station s = (Station) obj;
            if(s.getName().equals(this.getName())){
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    
    
    public int hashCode() {
        return this.getName().hashCode();
    }
}

猜你喜欢

转载自www.cnblogs.com/baixiaoli/p/10652410.html