北京地铁线路规划

一、北京地铁线路图

北京地铁线路规划_第1张图片

 

 二、功能需求

1.查询北京地铁所有线路

-map \\S

2.查询线路

-line \\S+ -map \\S+ -o \\S+

3.查询两个站点间的最短路径并输出所有站点

-station \\S+ \\S+ -map \\S+ -o \\S+

三、设计分析

北京地铁线路规划_第2张图片

 

Station定义站的结构

public class Station {

    private String name;

    public Station prev;

    public Station next;

    private Map> orderSetMap = new HashMap>();

    public Station(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public LinkedHashSet getAllPassedStations(Station station) {
        if (orderSetMap.get(station) == null) {
            LinkedHashSet set = new LinkedHashSet();
            set.add(this);
            orderSetMap.put(station, set);
        }
        return orderSetMap.get(station);
    }

    public Map> getOrderSetMap() {
        return orderSetMap;
    }

    @Override
    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;
        }
    }

    @Override
    public int hashCode() {
        return this.getName().hashCode();
    }
}

 

CreateLine和CreateData用来创建线路和站点

 

找到两个站点最短路径上的站点:

public String calculate(Station s1, Station s2)

计算最短路径:

private Station getShortestPath(Station station)

得到所有站点

private List getAllLinkedStations(Station station)

将结果写入指定文件:

public static void writeFileString(String strings, String writeFileName)

主函数:根据输入参数输出结果

 public static void main(String[] args) throws IOException {
            String map = "-map \\S+ ";
            String line = "-line \\S+ -map \\S+ -o \\S+ ";
            String path = "-station \\S+ \\S+ -map \\S+ -o \\S+ ";

            String arge = "";

            for (String i : args) {
                arge += i + " ";
            }

            if (arge.matches(map)) {
                String readFile = args[1];
                CreateLine.readFile(readFile);
                for (List linename : CreateLine.lineSet) {
                    for (int i = 0; i < linename.size(); i++) {
                        if(i==linename.size()-1)
                            System.out.print(linename.get(i).getName());
                        else if(i==0)
                            System.out.print(linename.get(i).getName() + " :  ");
                        else
                            System.out.print(linename.get(i).getName() + "——>");
                    }
                    System.out.println();

                }
            } else if (arge.matches(line)) {
                String lineName = args[1];
                String readFile = args[3];
                String writeFile = args[5];
                CreateLine.readFile(readFile);
                Station station = new Station(lineName);
                String allStations = "";
                int flag = 0;
                for (List linename : CreateLine.lineSet) {
                    if (linename.contains(station)) {
                        allStations+=linename.get(0).getName() + "站点:"+"\n";
                        for (int i = 1; i < linename.size(); i++) {
                            if(i==linename.size()-1)
                                allStations+=linename.get(i).getName();
                            else
                                allStations+=linename.get(i).getName() + " -> ";
                        }
                        flag=1;
                    }
                }
                if(flag==0){
                    System.out.println("该路线不存在");

                }
                else{
                    System.out.println(allStations);
                    writeFileString(allStations, writeFile);
                }

            } else if (arge.matches(path)) {
                String start = args[1];
                String end = args[2];
                String readFile = args[4];
                String writeFile = args[6];
                CreateData.readFile(readFile);
                Path sw = new Path();
                String allStations = sw.calculate(new Station(start), new Station(end));
                System.out.println(allStations);
                writeFileString(allStations, writeFile);
            }else{
                System.out.println("输入错误");
            }
        }

 

四、示例

-map Station.txt

-map Station.txt

北京地铁线路规划_第3张图片

 

-line 10号线 -map Station.txt -o Result.txt

-line 10号线 -map Station.txt -o Result.txt

 

-station 古城 平乐园 -map Station.txt -o Result.txt

-station 古城 平乐园 -map Station.txt -o Result.txt

 

-line 20号线 -map Station.txt -o Result.txt

-line 20号线 -map Station.txt -o Result.txt

 

 五、小结

本次作业在编写代码的时候,尤其是在最短路径的算法上遇到了困难,在网上查找了相关的代码之后,有点思路,进行代码的编写。通过本次的个人实验项目,我感觉对自身的代码水平有很大的提升。总的来说,对于项目需求的理解和个人现有的知识水平都要加强。

 

 github连接:https://github.com/luoyu10/subway

你可能感兴趣的:(北京地铁线路规划)