-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCentralRegistry.java
127 lines (94 loc) · 3.2 KB
/
CentralRegistry.java
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
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class CentralRegistry {
private static ArrayList<Airport> airports = new ArrayList<>();
private static ArrayList<Flight> flights = new ArrayList<>();
public static void addFlight(Flight aFlight) {
flights.add(aFlight);
aFlight.getAirportA().addConnection(aFlight.getAirportB());
aFlight.getAirportB().addConnection(aFlight.getAirportA());
aFlight.getAirportA().addCompany(aFlight.getCompany());
aFlight.getAirportB().addCompany(aFlight.getCompany());
}
public static void addAirport(Airport anAirport) {
airports.add(anAirport);
}
public static Airport getLargestHub() {
int max = airports.get(0).getDirectConnection().size();
Airport largest = airports.get(0);
for(Airport a : airports) {
if(a.getDirectConnection().size() > max) {
max = a.getDirectConnection().size();
largest = a;
}
}
return largest;
}
public static Flight getLongestFlight() {
int max = flights.get(0).getMinutes();
Flight longest = flights.get(0);
for(Flight flight : flights) {
if(flight.getMinutes() > max) {
max = flight.getMinutes();
longest = flight;
}
}
return longest;
}
public static Airport getAirport(String cityName) {
Airport rightAirport = null;
for (Airport airport : airports) {
if(cityName.compareTo(airport.getCity()) == 0) {
rightAirport = airport;
break;
}
}
return rightAirport;
}
public static ArrayList<String> getDirectFlightsDetails(Airport a, Airport b) {
ArrayList<String> toBePrinted = new ArrayList<>();
toBePrinted.add("DIRECT FLIGHTS DETAILS\n");
int counter = 0;
for(Flight flight : flights) {
if(flight.getAirportA().getName().equals(a.getName()) &&
flight.getAirportB().getName().equals(b.getName())) {
counter += 1;
toBePrinted.add("[" + counter + "]" + flight.toString());
}
if(flight.getAirportB().getName().equals(a.getName()) &&
flight.getAirportA().getName().equals(b.getName())) {
counter += 1;
toBePrinted.add("[" + counter + "]" + flight.toString());
}
}
return toBePrinted;
}
public static ArrayList<String> getInDirectFlightsDetails(Airport a, Airport b) {
ArrayList<Airport> inDirectConnections = new ArrayList<>();
ArrayList<String> toBePrinted = new ArrayList<>();
inDirectConnections = a.getInDirectConnection(b);
toBePrinted.add("INDIRECT FLIGHTS Through...\n");
int counter = 0;
Set<Airport> airSet = new HashSet<Airport>();
//Removing duplicates
for(Airport airport : inDirectConnections) {
airSet.add(airport);
}
inDirectConnections.clear();
Iterator<Airport> iterator = airSet.iterator();
while(iterator.hasNext()) {
inDirectConnections.add(iterator.next());
}
for(Airport airport : inDirectConnections){
counter ++;
toBePrinted.add("[" + counter + "]" + airport.getCity() + ", "
+ airport.getCode() + " airport\n");
}
return toBePrinted;
}
public static ArrayList<Airport> getAirports() {
return airports;
}
}