WalkController.java
1 |
|
2 |
import java.util.HashSet; |
3 |
|
4 |
/** |
5 |
* Controller class for everything related to the guided walks. |
6 |
* This class contains the links between other controllers, but also holds the |
7 |
* information related to the walks, such as reservations, possible walks, ... |
8 |
* @author Maarten Vangeneugden - 1438256 |
9 |
*/ |
10 |
public class WalkController { |
11 |
|
12 |
private ReservationController reservationController; |
13 |
private HashSet<Walk> walks; |
14 |
private HashSet<WalkReservation> walkReservations; |
15 |
|
16 |
public WalkController(ReservationController reservationController) { |
17 |
this.reservationController = reservationController; |
18 |
this.walks = new HashSet<>(); |
19 |
this.walkReservations = new HashSet<>(); |
20 |
} |
21 |
|
22 |
public void setReservationController(ReservationController reservationController) { |
23 |
this.reservationController = reservationController; |
24 |
} |
25 |
|
26 |
public ReservationController getReservationController() { |
27 |
return reservationController; |
28 |
} |
29 |
|
30 |
/** |
31 |
* Returns a copy of all Walk Reservations. |
32 |
* Emphasis on "copy"; There is no setWalkReservations() for a reason, using |
33 |
* this to edit the pointer variable would omit the use of 'private'. |
34 |
* @return A verbatim copy of all Walk Reservations. |
35 |
*/ |
36 |
@SuppressWarnings("unchecked") |
37 |
public Set<WalkReservation> getWalkReservations() { |
38 |
return (HashSet<WalkReservation>)this.walkReservations.clone(); |
39 |
} |
40 |
|
41 |
/** |
42 |
* Add a Walk Reservation to the system. |
43 |
* Calling this method adds the Walk Reservation to the system, allowing it |
44 |
* to be queried through the search menu, and allowing other parts of the |
45 |
* program to interact with it. |
46 |
* @param walkReservation The WalkReservation you wish to add. |
47 |
* @pre walkReservation mustn't be null. |
48 |
* @pre walkReservation mustn't already exist in the system. |
49 |
* @throws IllegalArgumentException if walkReservation is already in the |
50 |
* system. |
51 |
* @throws NullPointerException if walkReservation is a null pointer. |
52 |
* @post walkReservation is added to the set of active Walk Reservations. |
53 |
*/ |
54 |
public void addWalkReservation(WalkReservation walkReservation) { |
55 |
if(this.getWalkReservations().contains(walkReservation)) { |
56 |
throw new IllegalArgumentException("walkReservation already exists in the system."); |
57 |
} |
58 |
// Contract validated |
59 |
this.walkReservations.add(walkReservation); |
60 |
} |
61 |
|
62 |
/** |
63 |
* Cancels and removes the given WalkReservation. |
64 |
* If you want to remove a WalkReservation, use this method, and provide the |
65 |
* WalkReservation up for removal. |
66 |
* This method will take care of related actions, such as releasing Beds. |
67 |
* @param walkReservation The WalkReservation to be removed. |
68 |
* @pre reservation mustn't be null. |
69 |
* @pre reservation must be contained in the active WalkReservations. |
70 |
* @post The WalkReservation is removed from the active WalkReservations. |
71 |
* @throws NullPointerException if reservation is a null pointer. |
72 |
* @throws IllegalArgumentException if reservation is not contained in the |
73 |
* active WalkReservations. |
74 |
*/ |
75 |
public void cancelWalkReservation(WalkReservation walkReservation) { |
76 |
// Contract validation |
77 |
if(!this.getWalkReservations().contains(walkReservation)) { |
78 |
throw new IllegalArgumentException("The given WalkReservation was not contained in the active WalkReservations."); |
79 |
} |
80 |
if(walkReservation == null) { |
81 |
throw new NullPointerException(); |
82 |
} |
83 |
// Contract validated, execute method |
84 |
this.walkReservations.remove(walkReservation); |
85 |
} |
86 |
|
87 |
/** |
88 |
* Add Walk to the system. |
89 |
* @pre walk must have a unique name compared to the already stored Walks. |
90 |
*/ |
91 |
public void addWalk(Walk walk) { |
92 |
this.walks.add(walk); |
93 |
// FIXME Contract! |
94 |
} |
95 |
|
96 |
/** |
97 |
* Returns a copy of all Walks. |
98 |
* @return A verbatim copy of all Walk Reservations. |
99 |
*/ |
100 |
@SuppressWarnings("unchecked") |
101 |
public Set<Walk> getWalks() { |
102 |
return (HashSet<Walk>)this.walks.clone(); |
103 |
} |
104 |
|
105 |
/** |
106 |
* Returns a Walk based on the given name. |
107 |
* Every walk has its own name. This can be as simple as "Walk 1", "Walk2", |
108 |
* ... to cultural names in the native language like "Twaalfkroegentocht", |
109 |
* "Bloedprocessie", ... |
110 |
* Because of this uniqueness, it's possible to retrieve a walk by giving |
111 |
* its name to this method. |
112 |
* @param walkName The name of the Walk you want to find. |
113 |
* @pre walkName mustn't be blank or null. |
114 |
* @throws IllegalArgumentException if walkName is an empty String. |
115 |
* @throws NullPointerException if walkName is a null pointer. |
116 |
* @return The Walk with the given name, or null if there isn't a Walk with |
117 |
* that name in the system. |
118 |
*/ |
119 |
public Walk getWalkFromName(String walkName) { |
120 |
// Contract validation |
121 |
if(walkName.isEmpty()) |
122 |
throw new IllegalArgumentException("walkName mustn't be an empty String."); |
123 |
// Contract validated |
124 |
for(Walk walk: this.getWalks()) { |
125 |
if(walk.getName() == walkName) { |
126 |
return walk; |
127 |
} |
128 |
} |
129 |
return null; // No walk was found with that name. |
130 |
} |
131 |
} |
132 |