RoomController.java
1 |
|
2 |
import java.util.Date; |
3 |
import java.util.HashSet; |
4 |
|
5 |
/** |
6 |
* Holds all Rooms in the hostel. |
7 |
* The hostel contains a set of Rooms, with facilities and stuff. |
8 |
* This class takes care of storing all those Rooms, and provides methods to |
9 |
* manage this, like removing Rooms, adding Rooms, ... |
10 |
* @author Maarten Vangeneugden - 1438256 |
11 |
*/ |
12 |
public class RoomController { |
13 |
|
14 |
private Set<Room> rooms; |
15 |
|
16 |
public RoomController() { |
17 |
this.rooms = new HashSet<>(); |
18 |
} |
19 |
|
20 |
|
21 |
public Set<Room> getRooms() { |
22 |
return rooms; |
23 |
} |
24 |
|
25 |
public void setRooms(Set<Room> rooms) { |
26 |
this.rooms = rooms; |
27 |
} |
28 |
|
29 |
/** |
30 |
* Returns all rooms that meet the given requirements. |
31 |
* This method will search through all rooms, and check which rooms qualify. |
32 |
* Currently, this method checks for the following things: |
33 |
* - Is the requested type available? |
34 |
* - Does the Set of requested facilities form a subset of the Room's |
35 |
* facilities? |
36 |
* - Are there any Beds in the Room that can be reserved in the given |
37 |
* period? |
38 |
* @param reservation The Reservation for which to find eligible rooms. |
39 |
* @pre reservation mustn't be null. |
40 |
* @throws NullPointerException if reservation is a null pointer. |
41 |
* @return A set of all rooms that meet the requirements, or an empty set if |
42 |
* none were found. |
43 |
*/ |
44 |
public Set<Room> getQualifiedRooms(Reservation reservation) { |
45 |
Set<Room> qualifiedRooms = new HashSet<>(); |
46 |
for(Room room : this.getRooms()) { |
47 |
System.out.println("Not enough"); |
48 |
if(room.getType().equals(reservation.getRoomType())) |
49 |
continue; |
50 |
if(!room.getFacilities().containsAll(reservation.getRoomFacilities())) |
51 |
continue; |
52 |
if(room.getEmptyBeds(reservation.getBeginDate(), reservation.getEndDate()).size() < reservation.getPeople()) { |
53 |
continue; |
54 |
} |
55 |
// The Room fulfills all requirements at this point, so add it to |
56 |
// the set |
57 |
qualifiedRooms.add(room); |
58 |
} |
59 |
return qualifiedRooms; |
60 |
} |
61 |
|
62 |
/** |
63 |
* Returns the Room most suited for the given Reservation. |
64 |
* What this method does, is passing itself on to the getQualifiedRooms() |
65 |
* method. Out of the offered options, it picks the most suited Room, based |
66 |
* on predetermined requests. |
67 |
* As of writing, return a Room that is already partially filed. If there |
68 |
* are only unoccupied Rooms, return one of those. |
69 |
* |
70 |
* WARNING: This method returns null if no Room is available. |
71 |
* @return The Room that meets the requirements the best of all available |
72 |
* Rooms, or null if no suitable Room was found. |
73 |
*/ |
74 |
public Room getQualifiedRoom(Reservation reservation) { |
75 |
Set<Room> qualifiedRooms = this.getQualifiedRooms(reservation); |
76 |
if(qualifiedRooms.isEmpty()) return null; |
77 |
for(Room room: qualifiedRooms) { |
78 |
// If Room has less occupants than Beds, then it's partially filled. |
79 |
// getQualifiedRooms() implies there is enough space. |
80 |
if( |
81 |
room.getEmptyBeds(reservation.getBeginDate(), reservation.getEndDate()).size() < |
82 |
room.getBeds().size()) { |
83 |
return room; |
84 |
} |
85 |
// No good Room found, return one of the other Rooms |
86 |
} |
87 |
return (Room)qualifiedRooms.toArray()[0]; |
88 |
} |
89 |
} |
90 |