-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlightsDatabase.java
More file actions
163 lines (138 loc) · 5.28 KB
/
FlightsDatabase.java
File metadata and controls
163 lines (138 loc) · 5.28 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import java.io.*;
import java.util.*;
public class FlightsDatabase {
private HashMap<Integer, Flight> flights;
private PriorityQueue<Flight> flightsQueue;
private Comparator<Flight> comparator;
private ArrayList<Flight> activeFlights;
private Position position;
private Universe myUniverse;
private static final double CRASH_DISTANCE = 2;
private static final double CRASH_ALT = 500;
private int time;
public FlightsDatabase(String filePath) {
flights = new HashMap<Integer, Flight>();
comparator = new FlightComparator();
flightsQueue = new PriorityQueue<Flight>(1, comparator);
activeFlights = new ArrayList<Flight>();
myUniverse = Universe.getInstance();
parseFile(filePath);
};
// Adds a flight to database
public void add(Flight flight) {
flights.put(flight.getID(), flight);
flightsQueue.add(flight);
}
public Flight getFlightByID(int id) {
return flights.get(id);
}
public int countActiveFlights() {
return activeFlights.size();
}
public ArrayList<Flight> getActiveFlights() {
return activeFlights;
}
// Parse fligths database from file
public void parseFile(String filePath) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath))));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length != 9) {
throw new WrongInputFormatException("Wrong input file format: " + filePath);
}
int id = Integer.parseInt(parts[0]);
int startTime = Integer.parseInt(parts[1]) * 60;
int departureAirportID = Integer.parseInt(parts[2]);
int arrivalAirportID = Integer.parseInt(parts[3]);
String name = parts[4];
int aircraftType = Integer.parseInt(parts[5]);
int flightSpeed = Integer.parseInt(parts[6]);
int altitude = Integer.parseInt(parts[7]);
int fuel = Integer.parseInt(parts[8]);
Flight currentFlight = new Flight(id, startTime, departureAirportID,
arrivalAirportID, name, aircraftType, flightSpeed, altitude, fuel);
if (currentFlight.isValidFlight()) {
this.add(currentFlight);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (WrongInputFormatException e) {
System.out.println(e);
}
}
// Prints debugging info
public void print() {
System.out.println("Printing Flights Database");
flights.forEach((k, flight) -> {
flight.print();
});
}
public void init() {
time = 0;
integrate(0);
checkForColissions();
}
// Integrates for time interval dt in seconds
public void integrate(double dt) {
time += dt;
Flight currentFlight = flightsQueue.peek();
while (true) {
if (currentFlight != null && currentFlight.getStartTime() <= time) {
currentFlight.init();
flightsQueue.remove(currentFlight);
activeFlights.add(currentFlight);
myUniverse.addMessage("[Flight ID: " + currentFlight.getID() + "] TAKES OFF NOW");
currentFlight = flightsQueue.peek();
} else {
break;
}
}
checkForColissions();
ArrayList<Flight> markedFlights = new ArrayList<Flight>();
for (Flight flight : activeFlights) {
if (flight.isActive()) {
flight.integrate(dt);
} else {
markedFlights.add(flight);
}
}
for (Flight flight : markedFlights) {
activeFlights.remove(flight);
}
if (activeFlights.isEmpty() && flightsQueue.isEmpty()) {
myUniverse.addMessage("END OF SIMULATION");
myUniverse.stopTimer();
}
}
public void checkForColissions() {
for (Flight f1 : activeFlights) {
for (Flight f2 : activeFlights) {
double distance = f1.getAircraftPosition().euclideanDistance(f2.getAircraftPosition());
double altDif = Math.abs(f1.getCurrentAltitude() - f2.getCurrentAltitude());
if (f1.getID() != f2.getID() && distance <= CRASH_DISTANCE && altDif <= CRASH_ALT) {
System.out.println("F1ID: " + f1.getID() + " F2ID: " + f2.getID() + " Distance: " + distance + " AltDif: " + altDif);
f1.setCrashed();
f2.setCrashed();
}
}
}
}
public String stringifyAllFlights() {
String airportString;
StringBuilder result = new StringBuilder();
flights.forEach((k, flight) -> {
result.append(flight.stringify());
});
return result.toString();
}
public String stringifyActiveFlights() {
StringBuilder result = new StringBuilder();
for (Flight flight : activeFlights) {
result.append(flight.stringifyActiveFlight());
}
return result.toString();
}
}