学习进度3/16
一、基础数据与背景
基于北京地铁线路图设计系统,标注 7 号线双井站暂缓开通。
需存储线路号、车站 ID、站点名称、换乘信息、相邻站点等核心数据。
二、核心功能设计
基础查询
线路查询:输入线路号,按顺序输出该线路所有站点。
站点查询:输入站点名,输出途经该站的所有线路。
起终点查询:输出最短路径、途经站数、站点列表及换乘线路。
进阶功能
最短路径封装为类并完成单元测试。
车站遍历:输入起始站,输出遍历所有站点的总站数与路径(换乘不出地铁系统)。
最少换乘:规划起点到终点换乘次数最少的线路。
开发目标:完成手机端地铁查询系统开发。
三、关键要求
数据需清晰表示站点相邻关系与换乘信息。
路径输出需明确换乘线路、行驶方向,遍历需保证站点仅经过一次
、打开 IDEA,新建项目
打开 IntelliJ IDEA
点击 New Project
选择 Java → 下一步
项目名随便写:SubwaySystem
点 Finish
第二步:建包结构(必须按我这个来)
在 src 上右键 → New → Package,依次建 3 个包:
entity
service
test
结构:
plaintext
src/
├── entity/
├── service/
├── test/
第三步:写第一个类 —— 站点类 Station
在 entity 上右键 → New → Java Class → 输入 Station
把下面代码完整复制进去:
java
运行
package entity;
import java.util.ArrayList;
import java.util.List;
public class Station {
private String id;
private String name;
private List
private List
public Station(String id, String name) {
this.id = id;
this.name = name;
}
public void addLine(String line) {
if (!lines.contains(line)) {
lines.add(line);
}
}
public void addNeighbor(Station station) {
if (!neighbors.contains(station)) {
neighbors.add(station);
}
}
// getter
public String getId() { return id; }
public String getName() { return name; }
public List
public List
}
第四步:写第二个类 —— 线路类 Line
在 entity 上右键 → New → Java Class → Line
复制代码:
java
运行
package entity;
import java.util.ArrayList;
import java.util.List;
public class Line {
private String name;
private List
public Line(String name) {
this.name = name;
}
public void addStation(Station station) {
stations.add(station);
station.addLine(this.name);
}
// getter
public String getName() { return name; }
public List
}
第五步:写核心业务类 SubwayService
在 service 上右键 → New → Java Class → SubwayService
复制代码:
java
运行
package service;
import entity.Line;
import entity.Station;
import java.util.*;
public class SubwayService {
private final Map<String, Station> stationMap = new HashMap<>();
private final Map<String, Line> lineMap = new HashMap<>();
// 添加站点
public void addStation(String id, String name) {
if (!stationMap.containsKey(name)) {
stationMap.put(name, new Station(id, name));
}
}
// 添加线路,并自动建相邻关系
public void addLine(String lineName, List
Line line = new Line(lineName);
lineMap.put(lineName, line);
Station prev = null;
for (String name : stationNames) {
addStation(UUID.randomUUID().toString(), name);
Station curr = stationMap.get(name);
line.addStation(curr);
if (prev != null) {
curr.addNeighbor(prev);
prev.addNeighbor(curr);
}
prev = curr;
}
}
// 1. 查线路:返回该线路所有站
public List
Line line = lineMap.get(lineName);
if (line == null) return Collections.emptyList();
List
for (Station s : line.getStations()) {
names.add(s.getName());
}
return names;
}
// 2. 查站点经过哪些线路
public List
Station s = stationMap.get(stationName);
return s == null ? Collections.emptyList() : s.getLines();
}
// 3. 最短路径(站数最少)
public List
Station sStart = stationMap.get(start);
Station sEnd = stationMap.get(end);
if (sStart == null || sEnd == null) return Collections.emptyList();
Queue<List
Set
queue.add(Collections.singletonList(sStart));
visited.add(sStart);
while (!queue.isEmpty()) {
List
Station last = path.get(path.size() - 1);
if (last == sEnd) {
List
for (Station st : path) result.add(st.getName());
return result;
}
for (Station neighbor : last.getNeighbors()) {
if (!visited.contains(neighbor)) {
visited.add(neighbor);
List
newPath.add(neighbor);
queue.add(newPath);
}
}
}
return Collections.emptyList();
}
}

浙公网安备 33010602011771号