标签:static final equals 规划 etl screens over end ext
项目概述
根据输入的出发站和终点站推荐最短路线,显示搭乘线路及经过站点
采用Dijkstra算法,采用HashMap作为主要数据存取结构
运行结果:
代码分析
txt文件用空格分隔,先存储在LinkedHashSet中
public class Data {
public static LinkedHashSet<List<Station>> lineSet = new LinkedHashSet<>();//List<Station>集合
public static void TxttoData(String filePath) {
try {
File file = new File(filePath);
if (file.isFile() && file.exists()) { // 判断文件是否存在
FileInputStream fis = new FileInputStream(file);
InputStreamReader read = new InputStreamReader(fis, "GBK");
BufferedReader br = new BufferedReader(read);
String ttxt = null;
while ((ttxt = br.readLine()) != null) {//br.readLine()每执行一次向下读一行
List<Station> Linelist = new ArrayList<Station>();
String[] line = ttxt.split(" ");//分隔符
for(int i=1;i<line.length;i++) {
Linelist.add(new Station(line[0],line[i]));//Station:线路名,站名
}
lineSet.add(Linelist);
}
read.close();
} else {
System.out.println("文件不存在");
}
} catch (Exception e) {
System.out.println("读取错误");
e.printStackTrace();
}
}
public static Station toStation(String station) {
for(List<Station> ls:Data.lineSet)
for(Station st:ls)
if(station.equals(st.getname()))
return st;
return null;
}
}
将数据用Dijkstra算法计算出路径并存入Map中
public static Line set(Station start,Station over) {
if (!liststation.contains(start)) {//起点站加入list
liststation.add(start);
}
if (start.equals(over)){//起点=终点
Line result = new Line(start, over, 0);
resultMap.put(start, result);//存入HashMap
return resultMap.get(start);
}
if (resultMap.isEmpty()) {//第一站
List<Station> linkStations = getLinkStation(start);//获取相邻站
for (Station station : linkStations) {//遍历
Line result = new Line();
result.setStart(start);
result.setOver(station);
int distance = 1;
result.setDistance(distance);
result.getPass().add(station);
resultMap.put(station, result);
}
}
Station next = getNextStation();
if (next==null){
Line result = new Line();
result.setDistance(0);
result.setStart(start);
result.setOver(over);
return resultMap.put(over, result);
}
if (next.equals(over)) {//找到终点
return resultMap.get(next);
}
List<Station> nextLink = getLinkStation(next);
for (Station after : nextLink) {
if (liststation.contains(after)) {
continue;
}
int distance = resultMap.get(next).getDistance()+1;
if( next.getname().equals(after.getname())){
distance = 0;
}
List<Station> beforePass = resultMap.get(next).getPass();
Line afterResult = resultMap.get(after);
if (afterResult!=null){
if (afterResult.getDistance() > distance) {
afterResult.setDistance(distance);
afterResult.getPass().clear();
afterResult.getPass().addAll(beforePass);
afterResult.getPass().add(after);
}
}
else {
afterResult = new Line();
afterResult.setDistance(distance);
afterResult.setStart(start);
afterResult.setOver(after);
afterResult.getPass().addAll(beforePass);
afterResult.getPass().add(after);
}
resultMap.put(after, afterResult);
}
liststation.add(next);
set(start, over);
return resultMap.get(over);
}
UI 手动输入站点,程序下方进行输出
public class Mainui extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel jp2 = new JPanel();
private JButton search = new JButton("查询");
private JLabel start = new JLabel("起始站:");
private JLabel end = new JLabel("终点站:");
private JTextField starttxt = new JTextField(30);
private JTextField endtxt = new JTextField(30);
public Mainui() {
super( "北京地铁出行规划系统");
jp2.add(start);
jp2.add(starttxt);
jp2.add(end);
jp2.add(endtxt);
jp2.add(search);
this.add(jp2,BorderLayout.CENTER);
this.setSize(400, 150);
// 屏幕居中显示
double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
this.setLocation((int) (width - this.getWidth()) / 2,
(int) (height - this.getHeight()) / 2);
this.validate();
search.addActionListener(this);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==this.search) {
LineOut lo = new LineOut();
System.out.println((lo.LineOut(Data.toStation((String)starttxt.getText()),Data.toStation((String)endtxt.getText()))));
}
}
}
txt文件格式
GitHub地址 :https://github.com/caiweiwen/learning/tree/master/subway
标签:static final equals 规划 etl screens over end ext
原文地址:https://www.cnblogs.com/cwwaxx/p/11674894.html