-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHotelSort.java
78 lines (71 loc) · 2.14 KB
/
HotelSort.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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class HotelSort {
private ArrayList<Hotel> hotels;
/**
* Initializes HotelSort
* @param hotels
* @param rooms
*/
public HotelSort(ArrayList<Hotel> hotels) {
this.hotels = hotels;
}
/**
* helper method for sorting the names of the hotels
* https://www.geeksforgeeks.org/java-program-to-sort-an-arraylist/
*/
public Comparator<Hotel> sortName = new Comparator<Hotel>() {
public int compare(Hotel h1, Hotel h2){
String hotel1 = h1.getName().toString().toUpperCase();
String hotel2 = h2.getName().toString().toUpperCase();
return hotel1.compareTo(hotel2);
}
};
/**
* helper method for sorting the price of the rooms
* https://www.geeksforgeeks.org/java-program-to-sort-an-arraylist/
*/
public Comparator<Hotel> sortPrice = new Comparator<Hotel>() {
public int compare(Hotel h1, Hotel h2) {
double minPriceF1 = Double.MAX_VALUE;
double minPriceF2 = Double.MAX_VALUE;
for (ArrayList<Room> a : h1.getRooms()) {
for (Room s : a) {
if (s.getPrice() < minPriceF1) {
minPriceF1 = s.getPrice();
}
}
}
for (ArrayList<Room> a : h2.getRooms()) {
for (Room s : a) {
if (s.getPrice() < minPriceF2) {
minPriceF2 = s.getPrice();
}
}
}
if (minPriceF1 < minPriceF2) {
return -1;
} else if (minPriceF1 > minPriceF2) {
return 1;
}
return 0;
}
};
/**
* Sorts the hotels by name
* @return ArrayList
*/
public ArrayList<Hotel> sortNames(){
Collections.sort(hotels, sortName);
return hotels;
}
/**
* Sorts the rooms by price
* @return
*/
public ArrayList<Hotel> sortPrices(){
Collections.sort(hotels, sortPrice);
return hotels;
}
}