package chapter10;
import java.util.*;
public class Subway
{
private List stations;
private List connections;
private Map network;
public Subway() {
this.stations = new LinkedList();
this.connections = new LinkedList();
this.network = new HashMap();
}
public void addStation(String stationName) {
if (!this.hasStation(stationName)) {
Station station = new Station(stationName);
stations.add(station);
}
}
public boolean hasStation(String stationName) {
return stations.contains(new Station(stationName));
}
public void addConnection(String station1Name, String station2Name, String lineName) {
if ((this.hasStation(station1Name)) && (this.hasStation(station2Name))) {
Station station1 = new Station(station1Name);
Station station2 = new Station(station2Name);
Connection connection = new Connection(station1, station2, lineName);
connections.add(connection);
connections.add(new Connection(station2, station1, connection.getLineName()));
addToNetwork(station1, station2);
addToNetwork(station2, station1);
}
else
{
throw new RuntimeException("Invalid connection: [" + station1Name + ", " + station2Name + ", " + lineName + "]");
}
}
private void addToNetwork(Station station1, Station station2) {
if (network.keySet().contains(station1)) {
List connectingStations = (List) network.get(station1);
if (!connectingStations.contains(station2)) {
connectingStations.add(station2);
}
} else {
List connectingStations = new LinkedList();
connectingStations.add(station2);
network.put(station1, connectingStations);
}
}
public List getDirections(String startStationName, String endStationName) {
if (!this.hasStation(startStationName) || !this.hasStation(endStationName))
{
throw new RuntimeException("Stations entered do not exist on this subway");
}
Station start = new Station(startStationName);
Station end = new Station(endStationName);
List route = new LinkedList();
List reachableStations = new LinkedList();
Map previousStations = new HashMap();
List neighbors = (List)network.get(start);
for (Iterator i = neighbors.iterator(); i.hasNext(); ) {
Station station = (Station) i.next();
if (station.equals(end)) {
route.add(getConnection(start, end));
return route;
} else {
reachableStations.add(station);
previousStations.put(station, start);
}
}
List nextStations = new LinkedList();
nextStations.addAll(neighbors);
Station currentStation = start;
searchLoop:
for (int i = 1; i < stations.size(); i++) {
List tmpNextStations = new LinkedList();
for (Iterator j = nextStations.iterator(); j.hasNext(); ) {
Station station = (Station) j.next();
reachableStations.add(station);
currentStation = station;
List currentNeighbors = (List) network.get(currentStation);
for (Iterator k = currentNeighbors.iterator(); k.hasNext(); ) {
Station neighbor = (Station) k.next();
if (neighbor.equals(end)) {
reachableStations.add(neighbor);
previousStations.put(neighbor, currentStation);
break searchLoop;
} else if (!reachableStations.contains(neighbor)) {
reachableStations.add(neighbor);
tmpNextStations.add(neighbor);
previousStations.put(neighbor, currentStation);
}
}
}
nextStations = tmpNextStations;
}
//We've found the path now!
boolean keepLooping = true;
Station keyStation = end;
Station station;
while (keepLooping) {
station = (Station) previousStations.get(keyStation);
route.add(0, getConnection(station, keyStation));
if (start.equals(station)) {
keepLooping = false;
}
keyStation = station;
}
return route;
}
private Connection getConnection(Station station1, Station station2) {
for (Iterator i = connections.iterator(); i.hasNext(); ) {
Connection connection = (Connection) i.next();
Station one = connection.getStation1();
Station two = connection.getStation2();
if ((station1.equals(one)) && station2.equals(two)) {
return connection;
}
}
return null;
}
public boolean hasConnection(String station1Name, String station2Name, String lineName) {
Station station1 = new Station(station1Name);
Station station2 = new Station(station2Name);
for (Iterator i = connections.iterator(); i.hasNext(); ) {
Connection connection = (Connection) i.next();
if (connection.getLineName().equalsIgnoreCase(lineName)) {
if ((connection.getStation1().equals(station1)) &&
(connection.getStation2().equals(station2)))
{
return true;
}
}
}
return false;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Head First OOAD (278个子文件)
Subway.class 5KB
FindInstrumentTester.class 4KB
FindGuitarTester.class 3KB
FindGuitarTester.class 3KB
FindGuitarTester.class 3KB
FindGuitarTester.class 3KB
UnitTester.class 3KB
UnitTester.class 3KB
UnitTester.class 2KB
FindGuitarTester.class 2KB
Unit.class 2KB
Unit.class 2KB
UnitGroup.class 2KB
SubwayTester.class 2KB
SubwayPrinter.class 2KB
Unit.class 2KB
LoadTester.class 2KB
SubwayLoader.class 2KB
Wood.class 2KB
Wood.class 2KB
Wood.class 2KB
Wood.class 2KB
Wood.class 2KB
Wood.class 2KB
Inventory.class 1KB
Inventory.class 1KB
Builder.class 1KB
Builder.class 1KB
Builder.class 1KB
Builder.class 1KB
Builder.class 1KB
Builder.class 1KB
Inventory.class 1KB
InstrumentType.class 1KB
Inventory.class 1KB
DogDoorSimulator.class 1KB
GuitarSpec.class 1KB
GuitarSpec.class 1KB
DogDoorSimulator.class 1KB
Inventory.class 1KB
DogDoorSimulator.class 1KB
DogDoor.class 1KB
Inventory.class 1KB
Inventory.class 1KB
Connection.class 1KB
BarkRecognizer.class 1KB
Guitar.class 1KB
InstrumentSpec.class 1KB
Guitar.class 1KB
InstrumentSpec.class 1KB
Type.class 1KB
Type.class 1KB
Type.class 1KB
Type.class 1KB
Type.class 1KB
Type.class 1KB
Style.class 1KB
Style.class 1KB
DogDoor.class 1006B
DogDoorSimulator.class 1003B
DogDoorSimulator.class 1003B
DogDoorSimulator.class 1000B
DogDoor.class 994B
DogDoorSimulator.class 991B
DogDoorSimulator.class 991B
BarkRecognizer.class 904B
Remote.class 889B
Remote.class 889B
Remote.class 889B
Remote.class 889B
Station.class 878B
BarkRecognizer.class 869B
GuitarSpec.class 818B
DogDoor.class 805B
DogDoor.class 805B
Wood$1.class 799B
Wood$1.class 799B
Wood$1.class 799B
Wood$1.class 799B
Wood$1.class 799B
Wood$1.class 799B
Guitar.class 785B
BarkRecognizer.class 710B
BarkRecognizer.class 710B
BarkRecognizer.class 710B
DogDoorSimulator.class 709B
FlyTest.class 704B
InstrumentType$1.class 703B
Builder$1.class 702B
Builder$1.class 702B
Builder$1.class 702B
Builder$1.class 702B
Builder$1.class 702B
Builder$1.class 702B
DogDoorSimulator.class 685B
Instrument.class 680B
Instrument.class 680B
Guitar.class 660B
Guitar.class 660B
MandolinSpec.class 626B
共 278 条
- 1
- 2
- 3
资源评论
- lingduice2013-06-17貌似没很大的用处
- chanhit2012-07-14只是代码而已没有书
jimihendrix
- 粉丝: 1
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功