-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlanner.java
More file actions
120 lines (97 loc) · 3.07 KB
/
Planner.java
File metadata and controls
120 lines (97 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
// @TODO
// Make path: using Flight,Flight
// Airline Travel Scheduler - Planner
// Bongki Moon (bkmoon@snu.ac.kr)
public class Planner {
private Flight[] flights;
private boolean[] remainder;
private HashMap<String, Airport> StoA;
private int numPort;
// constructor
public Planner(LinkedList<Airport> portList, LinkedList<Flight> fltList) {
numPort = fltList.size();
flights = new Flight[numPort];
remainder = new boolean[numPort];
StoA = new HashMap<String, Airport>();
for (Airport port : portList){
StoA.put(port.getPort(), port);
}
//ListIterator<Flight> iter = fltList.listIterator();
Iterator<Flight> iter = fltList.iterator();
while (iter.hasNext()) {
Flight flt = iter.next();
Airport port = StoA.get(flt.getSrc());
if (port == null)
continue;
if (flt.getSrc().equals(port.getPort()) == true){
port.addFlight(flt);
iter.remove();
}
}
}
public Itinerary Schedule(String start, String end, String departure) {
for (int i = 0; i < numPort; ++i){
flights[i] = null;
remainder[i] = true;
}
Airport src = StoA.get(start);
Airport dest = StoA.get(end);
if (src == null || dest == null) return new Itinerary();
int startIndex = src.getIndex();
int endIndex = dest.getIndex();
int time = 60 * Integer.parseInt(departure.substring(0,2));
time = time + Integer.parseInt(departure.substring(2));
remainder[startIndex] = false;
flights[startIndex] = new Flight(start, start, time, time - src.getTime());
update(src);
while (remainder[endIndex] == true){
Flight flt = findMin();
if (flt == null) return new Itinerary();
Airport arrive = StoA.get(flt.getDest());
flights[arrive.getIndex()] = flt;
remainder[arrive.getIndex()] = false;
update(arrive);
}
LinkedList<Flight> route = new LinkedList<Flight>();
Flight node = flights[endIndex];
while (true){
route.add(node);
node = flights[StoA.get(node.getSrc()).getIndex()];
if (node.getSrc().equals(start))
break;
}
if (node.getSrc() != node.getDest()) route.add(node);
return new Itinerary(route);
}
private void update(Airport port){
int time = flights[port.getIndex()].getDtime() + port.getTime();
int index;
port.update(time);
LinkedList<Flight> route = port.getFlights();
for (Flight node : route){
Airport dest = StoA.get(node.getDest());
if (dest == null) continue;
index = dest.getIndex();
if (flights[index] == null)
flights[index] = node;
if (node.getDtime() < flights[index].getDtime())
flights[index] = node;
}
}
private Flight findMin(){
Flight result = null;
for (int i = 0; i < numPort; ++i){
if (flights[i] != null && remainder[i] == true){
if (result == null)
result = flights[i];
else if(result.getDtime() > flights[i].getDtime())
result = flights[i];
}
}
return result;
}
}