OOP2

Added more to the addReservation method.

Author
Vngngdn
Date
Nov. 30, 2016, 4:43 p.m.
Hash
55a08080f586d3dc3f876e048613c7daff2a163e
Parent
e71d5ec3ad9dcfde04b78d5772529e5d0f0ff601
Modified file
Challenge 6/ReservationController.java

Challenge 6/ReservationController.java

20 additions and 2 deletions.

View changes Hide changes
1
1
import java.util.HashSet;
2
2
import java.util.Date;
3
3
4
4
/**
5
5
 * Controller class for the Reservations of this program.
6
6
 * Since this program handles a youth hostel, it's imperative that it holds a
7
7
 * number of Reservations.
8
8
 * 
9
9
 * ReservationController takes the responsibility of handling the addition,
10
10
 * cancelling, editing, ... of Reservations, and the related tasks inherent to
11
11
 * these responsibilities, such as reserving beds, generating IDs, ...
12
12
 * @author Maarten Vangeneugden - 1438256
13
13
 */
14
14
public class ReservationController {
15
15
16
16
	/* Rationale:
17
17
	 * Normally I'd put this as an interface (Set), but an interface does not
18
18
	 * inherit from Object, and thus, does not provide clone(). This is
19
19
	 * necessary, because otherwise, there's no point in having private
20
20
	 * members.
21
21
	 */
22
22
	private HashSet<Reservation> reservations; // Holds all Reservations
23
23
24
24
	/**
25
25
	 * Creates the ReservationController.
26
26
	 */
27
27
	public ReservationController() {
28
28
		this.reservations = new HashSet<>();
29
29
	}
30
30
31
31
	/**
32
32
	 * Returns a copy of all Reservations.
33
33
	 * Emphasis on "copy"; There is no setReservations() for a reason, using
34
34
	 * this to edit the pointer variable would omit the use of 'private'.
35
35
	 * @return A verbatim copy of all Reservations.
36
36
	 */
37
37
	@SuppressWarnings("unchecked")
38
38
	public Set<Reservation> getReservations() {
39
39
		return (HashSet<Reservation>)this.reservations.clone();
40
40
	}
41
41
42
42
	/**
43
43
	 * Generates a unique ID for a new Reservation.
44
44
	 * This method is to be called when a new Reservation is to be stored.
45
45
	 * As every Reservation carries an ID, this method searches for a unique
46
46
	 * one, and returns it.
47
47
	 * @return An integer, different from all other stored Reservation IDs.
48
48
	 */
49
49
	public int generateReservationID() {
50
50
		/* Small optimization idea:
51
51
		 * Instead of starting from 0, and incrementing until it's unique, it
52
52
		 * will take the amount of stored Reservations, and test that as an ID.
53
53
		 * This may still overlap (by removing an older Reservation), but may be
54
54
		 * much faster. Consider implemting and testing for effectiveness if
55
55
		 * generating an ID takes too long.
56
56
		 */
57
57
		int ID = 0;
58
58
		boolean isUnique = false;
59
59
		do {
60
60
			ID++;
61
61
			isUnique = true;
62
62
			for(Reservation reservation: this.reservations) {
63
63
				if(ID == reservation.getReservationID()) {
64
64
					isUnique = false;
65
65
				}
66
66
			}
67
67
		} while(!isUnique);
68
68
		// Test:
69
69
		for(Reservation reservation: this.reservations) {
70
70
			assert reservation.getReservationID() != ID : "Duplicate ID generated!";
71
71
		}
72
72
		return ID;
73
73
	}
74
74
	
75
75
	/** 
76
76
	 * Check if Reservation can be made.
77
77
	 * Call this method whenever you're planning on adding a new Reservation. It
78
78
	 * will check the provided requirements (Room facilities, for example), and
79
79
	 * return whether this Reservation can continue.
80
80
	 * @param reservation The Reservation to check for possible addition.
81
81
	 * @pre reservation mustn't be null.
82
82
	 * @throws NullPointerException if reservation is null.
83
83
	 * @return True if reservation can be added, False otherwise.
84
84
	 */
85
85
	public boolean checkReservation(Reservation reservation) {
86
86
		if(reservation == null) {
87
87
			throw NullPointerException("reservation was a null pointer.");
88
88
		}
89
89
		if(this.getRoomController().getQualifiedRooms(reservation).size() == 0) {
90
90
			return null;
91
91
	}
92
92
93
93
	/**
94
94
	 * Adds and confirms the reservation.
95
95
	 * By calling this method, the given reservation will be stored in the
96
96
	 * system, and the given room will be filled.
97
97
	 * NOTE: You must check for yourself if the Reservation can take place in
+
98
	 * preconditions have been met.
+
99
	 * 
+
100
	 * The best way to accomplish this, is to only send 'sterile' Reservations
+
101
	 * as the first parameter. That is: Reservations without reserved Beds,
+
102
	 * without prior addition to the active Reservations, etc.
+
103
	 * NOTE: You must check for yourself if the Reservation can take place in
98
104
	 * this Room! If there are not enough Beds available, an exception will be
99
105
	 * thrown.
100
106
	 * @param reservation The Reservation to add.
101
107
	 * @param room The Room in which the people will reside.
102
108
	 * @pre room must have enough empty beds.
+
109
	 * @pre occupants must be at least 1.
+
110
	 * @pre room must have enough empty beds.
103
111
	 * @pre room must accomodate the Reservation's requirements.
104
112
	 * @pre No parameter must be null.
105
113
	 * @post reservation is stored in the active Reservations.
+
114
	 * @post reservation is stored in the active Reservations.
106
115
	 * @post The Beds in the provided Room are reserved for the Reservation's
107
116
	 * period.
108
117
	 * @throws IllegalArgumentException if the given Room can't fulfill the
109
118
	 * requirements of the Reservation.
110
-
	 * @throws NullPointerException if any parameter is a null pointer.
+
119
	 * @throws NullPointerException if any parameter is a null pointer.
111
120
	 */
112
121
	public void addReservation(Reservation reservation, Room room) {
113
-
		// Contract validation
+
122
		// Contract validation
114
123
		
+
124
			throw IllegalArgumentException("An invalid amount of occupants was given.");
+
125
		if(occupants > room.getEmptyBeds(reservation.getBegin(), reservation.getEnd()).size())
+
126
			throw IllegalArgumentException("The given Room has not enough empty Beds for the Reservation period.");
+
127
		if(!this.getRoomController().getQualifiedRooms(reservation).contains(room))
+
128
			throw IllegalArgumentException("The given Room cannot meet all requirements of the Reservation.");
+
129
		if(this.getReservations().contains(reservation)) {
+
130
			throw IllegalArgumentException("The given Reservation was already included as 
+
131
		
+
132
		
115
133
		// TODO: Add reservation to the set, and add the reserved days to the
116
134
		// beds in the given room.
117
135
	}
118
136
	/**
119
137
	 * Cancels and removes the given Reservation.
120
138
	 * If you want to remove a Reservation, use this method, and provide the
121
139
	 * Reservation up for removal.
122
140
	 * This method will take care of related actions, such as releasing Beds.
123
141
	 * @param reservation The Reservation to be removed.
124
142
	 * @pre reservation mustn't be null.
125
143
	 * @pre reservation must be contained in the active Reservations.
126
144
	 * @post The Reservation is removed from the active Reservations.
127
145
	 * @post The Beds, previously reserved for this Reservation, are now
128
146
	 * released, and can be reserved for another Reservation.
129
147
	 * @throws NullPointerException if reservation is a null pointer.
130
148
	 * @throws IllegalArgumentException if reservation is not contained in the
131
149
	 * active Reservations.
132
150
	 */
133
151
	public void cancelReservation(Reservation reservation) {
134
152
		// Contract validation
135
153
		if(!this.getReservations().contains(reservation)) {
136
154
			throw IllegalArgumentException("The given Reservation was not contained in the active Reservations.");
137
155
		}
138
156
		if(reservation == null) {
139
157
			throw NullPointerException();
140
158
		}
141
159
		// Contract validated, execute method
142
160
		this.reservations.remove(reservation); // Remove from active Reservations
143
161
		for(Bed bed: reservation.getReservedBeds()) { // Release reserved Beds
144
162
			bed.removeReservationPeriod(reservation.getBegin(), reservation.getEnd());
145
163
		}
146
164
		// Asserting post conditions are met
147
165
		assert !this.getReservation().contains(reservation) : "The reservation is still part of the active Reservations.";
148
166
		for(Bed bed: reservation.getReservedBeds()) {
149
167
			assert bed.isFree(reservation.getBegin(), reservation.getEnd()) : "One or more of the Beds are still reserved.";
150
168
		}
151
169
	}
152
170
153
171
}
154
172