Personal work subway project

Individual project --- Beijing subway shortest path

First, the program

Categories: Station, Path, Map, Subwany four classes respectively

  1. class Station: subway stations class that contains the name of the subway station, subway station number, belongs subway line
  2. class Path: class path, comprising a path from all sites last station in the path, and path
  3. class Map (read file processing and calculating the shortest path)

    • public void loadSubwayFile (String subway_file)
      read each line in the file, that is, every subway line temporarily stored.

      public void loadSubwayFile(String subway_file) {
      BufferedReader reader=null;
      File subway=new File(subway_file);
      try { reader=new BufferedReader(new FileReader(subway));
      String t=null;
      while((t=reader.readLine())!=null) {
      subway_allstation.add(t);
      }
      }catch (IOException e) {
      e.printStackTrace();
      } finally {
      if (reader != null) {
      try {
      reader.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      }

    • public void parseSubway ()
      File read, the content is divided, respectively id subway line, the subway line name, and each of subway stations, subway stations, and forming the line id = id * 1000 subway stations to each of the + the site where the first few lines of the site. Stored in the map corresponding to:
      the HashMap <String, Station> name_to_station domain name corresponding to the subway station

      HashMap <Integer, Station> id_to_station subway station corresponding to station id

      HashMap<String,List > Line_to_station subway line should be the name for a collection of all subway stations of line

      HashMap <Integer, String> lineid_to_linename subway line id corresponding to the name of the subway line

      HashMap<Integer,Set > Lineid_to_transferstation subway line should line id for all transit stations

      Distance HashMap <String, Integer> transferstationname_to_distanc transfer station name corresponding path

    public void parseSubway(){
    for(int i=0;i<subway_allstation.size();i++) {
    String line=subway_allstation.get(i);
    String[] arrIdAndStations=line.split(",");
    if(arrIdAndStations.length!=2) {
    System.out.println("地铁数据错误" + line);
    return;
    }
    int lineid=i+1;
    if(lineid==-1) {
    System.out.println("地铁线路号数据错误"+lineid);
    }
    String[]arrLineAndStations =arrIdAndStations[1].split(":");
    lineid_to_linename.put(lineid, arrLineAndStations[0]);
    String[]arrStationNames=arrLineAndStations[1].split(" ");
    for(int i1=0;i1<arrStationNames.length;i1++) {
    String Stationname=arrStationNames[i1];
    Station station=new Station();
    station.station_name=Stationname;
    int stationid=lineid*1000+i1+1;
    station.station_id.add(stationid);
    station.line_name=arrLineAndStations[0];
    station.line_id=lineid;
    if(!line_to_station.containsKey(station.line_name)) {
    List stations=new ArrayList ();
    stations.add(station);
    line_to_station.put(station.line_name, stations);
    }
    else {
    List stations=line_to_station.get(station.line_name);
    stations.add(station);
    }
    id_to_station.put(stationid, station);
    //注意中转站 要先判断再存
    if(!name_to_station.containsKey(arrStationNames[i1])) {
    name_to_station.put(Stationname, station);
    }
    else {
    Station stationExistedTransferStation =name_to_station.get(arrStationNames[i1]);
    stationExistedTransferStation.station_id.add(stationid);
    updateTransferStation(stationExistedTransferStation);
    }
    }`

    • Path shortedPath (String startname, String endname) calculates the shortest path

    If the starting and ending points are on the same subway line, then just traversed once, Path deposit is returned starting point and destination.
    To be replaced once, we need to traverse twice, Path deposit is returned starting point, transit point and destination. And so on.
    If you need to transfer the traverse agree that all transit station on the line, find the shortest path.

    for(String stationname:transferstationname_to_distance.keySet()) {
    Station stationtransfer=name_to_station.get(stationname);
    int currenttotransferdis =getStationDistance(pathcurrent.path_laststation,stationtransfer);
    int finaldis=pathcurrent.distance+currenttotransferdis;
    if(finaldis>=transferstationname_to_distance.get(stationname))
    continue;
    transferstationname_to_distance.put(stationname, finaldis);
    if(finaldis<1000&&finaldis<shorted.distance) {
    Path pathnew=new Path();
    pathnew.distance=finaldis;
    pathnew.path_laststation=stationtransfer;
    pathnew.path_stationlist=new Vector<>(pathcurrent.path_stationlist);
    pathnew.path_stationlist.addElement(stationtransfer);
    stackAllPaths.push(pathnew);
    }

  1. class Subway output
    printStationsOfLine (String Line, String strOutFile) returns all site name subway line
    printPath (Path path) Returns the name of all sites on the path
    printFile (String strContent, String strOutFile) The String output to a file

    void printStationsOfLine(String Line, String strOutFile) {
    StringBuffer strRst = new StringBuffer();
    strRst.append(Line+"\r\n");
    if (line_to_station.containsKey(Line)) {
    List stations=new ArrayList ();
    stations=line_to_station.get(Line);
    for(int i=0;i<stations.size();i++) {
    Station s=stations.get(i);
    strRst.append(s.station_name+ "\r\n");
    }
    }
    printFile(strRst.toString(), strOutFile);
    }

  2. Test Results

    • Output all sites java Subway -a Line -map subway.txt -o station.txt

      一号线 苹果园 古城 八角游乐园 八宝山 玉泉路 五棵松 万寿路 公主坟 军事博物馆 木樨地 南礼士路 复兴门 西单 天安门西 天安门东 王府井 东单 建国门 永安里 国贸 大望路 四惠 四惠东
    • Erroneous input java Subway -a 0 Line -map subway.txt -o station.txt`

      得到结果:0号线 地铁线不存在
    • The shortest path without transfer java Subway -b Jin'anqiao West Huangcun -map subway.txt -o routine.txt`

      得到结果:六号线 金安桥 苹果园 杨庄 西黄村
    • The shortest path to take 1 java Subway -b Andingmenwai Beixinqiao -map subway.txt -o routine.txt

      得到结果:二号线 安定门 雍和宫 五号线 北新桥
    • The shortest path to transfer twice java Subway -b Dongzhimen Anzhenmen -map subway.txt -o routine.txt`

      得到结果:二号线 和平门 宣武门 四号线 菜市口 七号线 虎坊桥
    • Shortest Path error input java Subway -b Peace Arch Tiger Square -map subway.txt -o routine.txt`

      得到结果:查询站点输入有误
  3. Personal Summary

    The private job to learn a lot of other people's code because of problems encountered in the process of writing, and led to the idea of ​​a beginning of a big difference, read the code written by someone else found a lot of code to learn, there are a lot of knowledge do not know, some know it will not use, but also requires a lot of practice and improve skills.

Guess you like

Origin www.cnblogs.com/c-zq/p/11666841.html