-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlightSort.java
75 lines (71 loc) · 2.21 KB
/
FlightSort.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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class FlightSort {
private ArrayList<Flight> flights;
/**
* Initializes FlightSort
* @param flights
* @param seats
*/
public FlightSort(ArrayList<Flight> flights){
this.flights = flights;
}
/**
* Helper method to sort the flight by name
* https://www.geeksforgeeks.org/java-program-to-sort-an-arraylist/
*/
public Comparator<Flight> sortName = new Comparator<Flight>() {
public int compare(Flight f1, Flight f2){
String flight1 = f1.getAirline().toString().toUpperCase();
String flight2 = f2.getAirline().toString().toUpperCase();
return flight1.compareTo(flight2);
}
};
/**
* Helper method to sort the seats on the flight by price
* https://www.geeksforgeeks.org/java-program-to-sort-an-arraylist/
*/
public Comparator<Flight> sortPrice = new Comparator<Flight>() {
public int compare(Flight f1, Flight f2) {
double minPriceF1 = Double.MAX_VALUE;
double minPriceF2 = Double.MAX_VALUE;
for (ArrayList<Seat> a : f1.getSeats()) {
for (Seat s : a) {
if (s.getCost() < minPriceF1) {
minPriceF1 = s.getCost();
}
}
}
for (ArrayList<Seat> a : f2.getSeats()) {
for (Seat s : a) {
if (s.getCost() < minPriceF2) {
minPriceF2 = s.getCost();
}
}
}
if (minPriceF1 < minPriceF2) {
return -1;
} else if (minPriceF1 > minPriceF2) {
return 1;
}
return 0;
}
};
/**
* Sorts the flights by the airline name
* @return ArrayList
*/
public ArrayList<Flight> sortNames(){
Collections.sort(flights, sortName);
return flights;
}
/**
* Sorts the flights by the seat price
* @return ArrayList
*/
public ArrayList<Flight> sortPrices(){
Collections.sort(flights, sortPrice);
return flights;
}
}