OOP2

Initiated reworking the Reservation class of challenge 6. Currently, only the default constructor is properly made, so work will continue later.

Author
Maarten 'Vngngdn' Vangeneugden
Date
Nov. 23, 2016, 10:51 p.m.
Hash
7345d9a840ba28d7ad85ce5f886ee3256dd1280b
Parent
7592b73b42ce858ed5d466a98e15effd5f24dd1c
Modified file
Challenge 6/Reservation.java

Challenge 6/Reservation.java

66 additions and 13 deletions.

View changes Hide changes
1
1
import java.util.HashSet;
2
2
import java.util.Date;
3
3
4
4
public class Reservation {
+
5
 * Represents a reservation in a hostel.
+
6
 * Reservation is a simple class that allows one to store reservation
+
7
 * information, request it when necessary, and so on.
+
8
 * Certain methods are provided for interaction, which use contracts to assert
+
9
 * proper functioning.
+
10
 * @author Maarten Vangeneugden - 1438256
+
11
 */
+
12
public class Reservation {
5
13
6
14
	private String groupName;
+
15
	private String groupName;
7
16
	private Date date;
8
-
	private Set<Bed> reservedBeds;
+
17
	private Date endDate;
+
18
	private Set<Bed> reservedBeds;
9
19
	private int reservationID;
10
20
	private String roomType;
11
-
	private Set<String> roomFacilities;
12
-
	private int nights;
13
-
	private int[] breakfastDays;
14
-
	// Controllers
+
21
	private Set<String> roomFacilities; // Requested room facilities
+
22
	private Set<Date> breakfastDays; // Set of days the group wants breakfast
+
23
	// Controllers
15
24
	private RoomController roomController;
16
25
	private ReservationController reservationController;
17
26
18
27
	public Reservation(String groupName, Date date, Set<Bed> reservedBeds, String roomType, Set<String> roomFacilities, int nights, int[] breakfastDays, ReservationController reservationController, RoomController roomController) {
19
-
		this.reservationController = reservationController;
20
-
		this.roomController = roomController;
21
-
22
-
		this.groupName = groupName;
+
28
	 * Create a new Reservation.
+
29
	 * Be aware about the limitations of a Reservation: it's not the
+
30
	 * Reservation's duty to check whether the provided Beds are properly
+
31
	 * reserved; take care of this accordingly.
+
32
	 * 
+
33
	 * Some information is implicit. For example: The size of the set of
+
34
	 * reserved Beds implies the amount of people in the group; no breakfast
+
35
	 * days implies no breakfast, ...
+
36
	 * @param groupName The name of the group.
+
37
	 * @param begin The date that the Reservation begins.
+
38
	 * @param end The date that the Reservation ends.
+
39
	 * @param reservedBeds The set of Beds reserved and assigned to this
+
40
	 * Reservation.
+
41
	 * @param roomType The requested room type.
+
42
	 * @param roomFacilities The set of all requested room facilities.
+
43
	 * @param breakfastDays A set of all days that the group wants breakfast.
+
44
	 * @param reservationID An ID for this reservation, to differentiate from
+
45
	 * other Reservations.
+
46
	 * @pre No parameter must be a null pointer.
+
47
	 * @pre begin must come before end.
+
48
	 * @pre All dates in breakfastDays must fall between begin and end.
+
49
	 * @pre No string parameter may be empty.
+
50
	 * @post The amount of people in the Reservation is determined by the amount
+
51
	 * of reserved Beds.
+
52
	 * @throws NullPointerException if any parameter is a null pointer.
+
53
	 * @throws IllegalArgumentException if any of the other preconditions is not
+
54
	 * met.
+
55
	 */
+
56
	public Reservation(String groupName, Date begin, Date end, Set<Bed> reservedBeds, String roomType, Set<String> roomFacilities) {
+
57
		// Contract validation:
+
58
		if(!begin.before(end)) {
+
59
			throw IllegalArgumentException("The begin date occurs after the end date.");
+
60
		}
+
61
		if(groupName.isEmpty() || roomType.isEmpty()) {
+
62
			throw IllegalArgumentException("groupName and/or roomType were empty strings.");
+
63
		}
+
64
		for(String roomFacility: roomFacilities) {
+
65
			if(roomFacility.isEmpty()) {
+
66
				throw IllegalArgumentException("One of the room facilities was an empty string.");
+
67
			}
+
68
		}
+
69
		for(Date breakfastDay : breakfastDays) {
+
70
			if(breakfastDay.before(begin) || breakfastDay.after(end)) {
+
71
				throw IllegalArgumentException("One of the breakfast days occurs before/after the reservation period.");
+
72
			}
+
73
		}
+
74
		// Contract validated, execute constructor
+
75
		this.groupName = groupName;
23
76
		this.date = date;
24
-
		this.reservedBeds = reservedBeds;
+
77
		this.endDate = end;
+
78
		this.reservedBeds = reservedBeds;
25
79
		this.reservationID = this.reservationController.generateReservationID();
26
-
		this.roomType = roomType;
+
80
		this.roomType = roomType;
27
81
		this.roomFacilities = roomFacilities;
28
82
		this.nights = nights;
29
-
30
-
	}
+
83
	}
31
84
32
85
	/**
33
86
	 * Creates an empty Reservation.
34
87
	 * This constructor is espcially useful for adding a new reservation.
35
88
	 */
36
89
	public Reservation(ReservationController reservationController, RoomController roomController) {
37
90
		this(
38
91
				"",
39
92
				new Date(),
40
93
				new HashSet<>(),
41
94
				"",
42
95
				new HashSet<>(),
43
96
				1,
44
97
				new int[0],
45
98
				reservationController,
46
99
				roomController
47
100
				);
48
101
	}
49
102
50
103
	public void setGroupName(String groupName) {
51
104
		this.groupName = groupName;
52
105
	}
53
106
54
107
	public String getGroupName() {
55
108
		return groupName;
56
109
	}
57
110
58
111
	public void setDate(Date date) {
59
112
		this.date = date;
60
113
	}
61
114
62
115
	public Date getDate() {
63
116
		return date;
64
117
	}
65
118
66
119
	public void setReservedBeds(Set<Bed> reservedBeds) {
67
120
		this.reservedBeds = reservedBeds;
68
121
	}
69
122
70
123
	public Set<Bed> getReservedBeds() {
71
124
		return reservedBeds;
72
125
	}
73
126
74
127
	public void setReservationID(int reservationID) {
75
128
		this.reservationID = reservationID;
76
129
	}
77
130
78
131
	public int getReservationID() {
79
132
		return reservationID;
80
133
	}
81
134
82
135
	public void setRoomType(String roomType) {
83
136
		this.roomType = roomType;
84
137
	}
85
138
86
139
	public String getRoomType() {
87
140
		return roomType;
88
141
	}
89
142
90
143
	public void setRoomFacilities(Set<String> roomFacilities) {
91
144
		this.roomFacilities = roomFacilities;
92
145
	}
93
146
94
147
	public Set<String> getRoomFacilities() {
95
148
		return roomFacilities;
96
149
	}
97
150
98
151
	public void setNights(int nights) {
99
152
		this.nights = nights;
100
153
	}
101
154
102
155
	public int getNights() {
103
156
		return nights;
104
157
	}
105
158
106
159
	/**
107
160
	 * Calculates the price of the Reservation, based on its current state.
108
161
	 * Price table:
109
162
	 * - 20/person/day
110
163
	 *   - 5 less in low season, 5 more in high season
111
164
	 * - 4/breakfast ordered
112
165
	 * - If room is fully booked by the group --> 10% discount
113
166
	 * @return The price in euros.
114
167
	 */
115
168
	public int getPrice() {
116
169
		int totalPrice = 0;
117
170
		// Jan - Apr: Mid
118
171
		// May - Aug: High
119
172
		// Sep - Dec: Low
120
173
		
121
174
		// Calculate bed prices
122
175
		int month = this.getDate().getMonth();
123
176
		int bedPrice = 20;
124
177
		if(month >=8) { // From September:
125
178
			bedPrice -= 5;
126
179
		} else if(month >=4) { // From May:
127
180
			bedPrice += 5;
128
181
		}
129
182
		totalPrice += (this.getReservedBeds().size() * this.getNights() * bedPrice);
130
183
		// Calculate price for breakfasts
131
184
		int breakfasts = this.getBreakfastDays().length;
132
185
		totalPrice += breakfasts * this.getReservedBeds().size();
133
186
		// Check if eligible for discount
134
187
		for(Room room: roomController.getRooms()) {
135
188
			Set<Bed> roomBeds = room.getBeds();
136
189
			if(roomBeds.containsAll(this.reservedBeds)) {
137
190
				double discount = (double)totalPrice * 0.1;
138
191
				totalPrice -= (int)discount;
139
192
			}
140
193
		}
141
194
		return totalPrice;
142
195
	}
143
196
144
197
145
198
146
199
147
200
148
201
149
202
150
203
	public void setBreakfastDays(int[] breakfastDays) {
151
204
		this.breakfastDays = breakfastDays;
152
205
	}
153
206
	public int[] getBreakfastDays() {
154
207
		return this.breakfastDays;
155
208
	}
156
209
157
210
}
158
211