OOP2

Room.java

1
import java.util.Set;
2
import java.util.HashSet;
3
import java.util.Date;
4
5
/**
6
 * A room in a hostel.
7
 * Room represents just that: A room.
8
 * A room contains a set of Beds, facilities that can be used, etc.
9
 * It's highly decoupled: Apart from holding a set of Beds, the only members
10
 * types consist of those that are available in every OpenJDK implementation.
11
 * @author Maarten Vangeneugden - 1438256
12
 */
13
public class Room {
14
15
	private HashSet<Bed> beds;
16
	private String type;
17
	private HashSet<String> facilities;
18
19
	/**
20
	 * Create a new Room.
21
	 * @param beds The amount of Beds that will be placed in this Room.
22
	 * @param type The type of this Room. (for example: Male, Female, Mixed,
23
	 * ...)
24
	 * @param facilities A Set of facilities this Room provides ("Shower",
25
	 * "Radio", "Overpriced WiFi", ...)
26
	 * @pre No parameter must be a null pointer.
27
	 * @pre beds must be at least 1.
28
	 * @pre Type mustn't be an empty String.
29
	 * @pre No facility may be an empty String.
30
	 * @post The Room will receive the provided amount of Beds, which are all
31
	 * completely released.
32
	 * @throws IllegalArgumentException if the amount of Beds is less than 1, or
33
	 * one of the facilities is an empty String.
34
	 * @throws NullPointerException if one of the parameters is a null pointer.
35
	 */
36
	public Room(int beds, String type, Set<String> facilities) {
37
		// Contract validation happens in the setter methods
38
		this.setFacilities(facilities);
39
		this.setType(type);
40
		if(beds < 1) {
41
			throw new IllegalArgumentException("Beds was less than 1.");
42
		}
43
		// Contract validated, execute constructor
44
		this.beds = new HashSet<>();
45
		for(int i=0; i<beds; i++) {
46
			this.beds.add(new Bed());
47
		}
48
	}
49
50
	/**
51
	 * Remove a Bed from this Room.
52
	 * This method will remove the given Bed from this Room.
53
	 * However, make sure that this Bed is free of any reservations, as you
54
	 * can't delete a Bed with a reserved period.
55
	 * @see Bed
56
	 * @param bed The Bed to be removed.
57
	 * @pre bed mustn't be null.
58
	 * @pre bed mustn't be the last Bed in this Room.
59
	 * @pre bed must be in this Room.
60
	 * @pre bed mustn't have any reserved periods.
61
	 * @post The amount of Beds in this Room is decremented by 1.
62
	 * @throws IllegalArgumentException if bed is not in this Room, or has
63
	 * reserved periods, or this is the last Bed in the Room.
64
	 * @throws NullPointerException if bed is a null pointer.
65
	 */
66
	public void removeBed(Bed bed) {
67
		// Contract validation
68
		if(!this.getBeds().contains(bed)) {
69
			throw new IllegalArgumentException("The given Bed is not in this Room.");
70
		}
71
		if(bed.hasReservations()) {
72
			throw new IllegalArgumentException("The given Bed still has reserved periods.");
73
		}
74
		if(this.getBeds().size() == 1) {
75
			throw new IllegalArgumentException("Deleting this Bed would empty the Room.");
76
		}
77
		// Contract validated, execute method
78
		this.beds.remove(bed);
79
		// Assert post conditions
80
		assert !this.getBeds().contains(bed) : "The given Bed was not removed from this Room.";
81
	}
82
83
	/**
84
	 * Add a Bed to this Room.
85
	 * @post The amount of Beds in this Room increments with 1.
86
	 */
87
	public void addBed() {
88
		this.beds.add(new Bed());
89
	}
90
91
	/**
92
	 * Returns a copy of all Beds.
93
	 * @return A verbatim copy of all Beds.
94
	 */
95
	@SuppressWarnings("unchecked")
96
	public Set<Bed> getBeds() {
97
		return (HashSet<Bed>) this.beds.clone();
98
	}
99
100
	/**
101
	 * Set the type of this Room.
102
	 * @param type The new type of the Room.
103
	 * @pre type mustn't be an empty String, or a null pointer.
104
	 * @post The Room's type is changed to the given type.
105
	 * @throws IllegalArgumentException if type is an empty String.
106
	 * @throws NullPointerException if type is a null pointer.
107
	 */
108
	public void setType(String type) {
109
		if(type.isEmpty()) {
110
			throw new IllegalArgumentException("type is an empty String.");
111
		}
112
		this.type = type;
113
	}
114
115
	/**
116
	 * Returns the type of this Room.
117
	 * @return The secret launch codes of the USS Nimitz, granting access to
118
	 * nuclear warheads so big, you'll lose faith in humanity.
119
	 */
120
	public String getType() {
121
		return type;
122
	}
123
124
	/**
125
	 * Set the facilities available in this Room.
126
	 * @param facilities The set of facilities in this Room.
127
	 * @pre No facility must be an empty String or a null pointer.
128
	 * @post The Room will have the newly provided facilities.
129
	 * @throws IllegalArgumentException if one of the facilities is an empty String.
130
	 * @throws NullPointerException if any given variable is a null pointer.
131
	 */
132
	public void setFacilities(Set<String> facilities) {
133
		for(String facility : facilities) {
134
			if(facility.isEmpty()) {
135
				throw new IllegalArgumentException("A facility was an empty String.");
136
			}
137
		}
138
		this.facilities = new HashSet<>(facilities);
139
	}
140
141
	/**
142
	 * Returns a copy of all facilities.
143
	 * @return A verbatim copy of all facilities.
144
	 */
145
	@SuppressWarnings("unchecked")
146
	public Set<String> getFacilities() {
147
		return (HashSet<String>)this.facilities.clone();
148
	}
149
150
	/**
151
	 * Search for Beds that can be reserved.
152
	 * This method will look through all Beds, and check whether they can be
153
	 * reserved in the given period. You will receive all Beds that can be
154
	 * reserved.
155
	 * @param begin The begin date.
156
	 * @param end The end date.
157
	 * @pre No parameter must be null.
158
	 * @pre begin must strictly come before end.
159
	 * @throws IllegalArgumentException if begin comes after end, or both are
160
	 * equal.
161
	 * @throws NullPointerException if any given parameter is a null pointer.
162
	 * @return A Set of all Beds that can be reserved, or an empty Set if no Bed
163
	 * can be reserved in the given period.
164
	 */
165
	public Set<Bed> getEmptyBeds(Date begin, Date end) {
166
		// Contract validation
167
		if(!begin.before(end)) {
168
			throw new IllegalArgumentException("begin does not come strictly before end");
169
		}
170
		// Validated
171
		Set<Bed> emptyBeds = new HashSet<>();
172
		for(Bed bed: this.getBeds()) {
173
			if(bed.isFree(begin, end)) {
174
				emptyBeds.add(bed);
175
			}
176
		}
177
		return emptyBeds;
178
	}
179
180
}
181