1 /* =============================================================================
4 * -- Travel reservation resource manager
6 * =============================================================================
8 * Copyright (C) Stanford University, 2006. All Rights Reserved.
11 * =============================================================================
13 * For the license of bayes/sort.h and bayes/sort.c, please see the header
16 * ------------------------------------------------------------------------
18 * For the license of kmeans, please see kmeans/LICENSE.kmeans
20 * ------------------------------------------------------------------------
22 * For the license of ssca2, please see ssca2/COPYRIGHT
24 * ------------------------------------------------------------------------
26 * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
27 * header of the files.
29 * ------------------------------------------------------------------------
31 * For the license of lib/rbtree.h and lib/rbtree.c, please see
32 * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
34 * ------------------------------------------------------------------------
36 * Unless otherwise noted, the following license applies to STAMP files:
38 * Copyright (c) 2007, Stanford University
39 * All rights reserved.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions are
45 * * Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
48 * * Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in
50 * the documentation and/or other materials provided with the
53 * * Neither the name of Stanford University nor the names of its
54 * contributors may be used to endorse or promote products derived
55 * from this software without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
58 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
60 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
62 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
63 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
64 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
65 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
66 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
67 * THE POSSIBILITY OF SUCH DAMAGE.
69 * =============================================================================
72 /* =============================================================================
73 * DECLARATION OF TM_CALLABLE FUNCTIONS
74 * =============================================================================
76 public class Manager {
79 RBTree flightTablePtr;
80 RBTree customerTablePtr;
82 /* =============================================================================
84 * =============================================================================
87 carTablePtr = new RBTree();
88 roomTablePtr = new RBTree();
89 flightTablePtr = new RBTree();
90 customerTablePtr = new RBTree();
93 /* =============================================================================
95 * -- If 'num' > 0 then add, if < 0 remove
96 * -- Adding 0 seats is error if does not exist
97 * -- If 'price' < 0, do not update price
98 * -- Returns TRUE on success, else FALSE
99 * =============================================================================
101 boolean addReservation (RBTree tablePtr, int id, int num, int price) {
102 Reservation reservationPtr;
104 reservationPtr = (Reservation)tablePtr.find(id);
105 if (reservationPtr == null) {
106 /* Create new reservation */
107 if (num < 1 || price < 0) {
110 reservationPtr = new Reservation(id, num, price);
111 // assert(reservationPtr != NULL);
112 tablePtr.insert(id, reservationPtr);
114 /* Update existing reservation */
115 if (!reservationPtr.reservation_addToTotal(num)) {
118 if (reservationPtr.numTotal == 0) {
119 boolean status = tablePtr.remove(id);
121 System.out.println("TMRESTART7");
125 reservationPtr.reservation_updatePrice(price);
133 /* =============================================================================
135 * -- Add cars to a city
136 * -- Adding to an existing car overwrite the price if 'price' >= 0
137 * -- Returns TRUE on success, else FALSE
138 * =============================================================================
140 boolean manager_addCar (int carId, int numCars, int price) {
141 return addReservation(carTablePtr, carId, numCars, price);
145 /* =============================================================================
147 * -- Delete cars from a city
148 * -- Decreases available car count (those not allocated to a customer)
149 * -- Fails if would make available car count negative
150 * -- If decresed to 0, deletes entire entry
151 * -- Returns TRUE on success, else FALSE
152 * =============================================================================
154 boolean manager_deleteCar (int carId, int numCar) {
155 /* -1 keeps old price */
156 return addReservation(carTablePtr, carId, -numCar, -1);
160 /* =============================================================================
162 * -- Add rooms to a city
163 * -- Adding to an existing room overwrite the price if 'price' >= 0
164 * -- Returns TRUE on success, else FALSE
165 * =============================================================================
167 boolean manager_addRoom(int roomId, int numRoom, int price) {
168 return addReservation(roomTablePtr, roomId, numRoom, price);
174 /* =============================================================================
176 * -- Delete rooms from a city
177 * -- Decreases available room count (those not allocated to a customer)
178 * -- Fails if would make available room count negative
179 * -- If decresed to 0, deletes entire entry
180 * -- Returns TRUE on success, else FALSE
181 * =============================================================================
183 boolean manager_deleteRoom(int roomId, int numRoom) {
184 /* -1 keeps old price */
185 return addReservation(roomTablePtr, roomId, -numRoom, -1);
189 /* =============================================================================
191 * -- Add seats to a flight
192 * -- Adding to an existing flight overwrite the price if 'price' >= 0
193 * -- Returns TRUE on success, FALSE on failure
194 * =============================================================================
196 boolean manager_addFlight(int flightId, int numSeat, int price) {
197 return addReservation(flightTablePtr, flightId, numSeat, price);
202 /* =============================================================================
203 * manager_deleteFlight
204 * -- Delete an entire flight
205 * -- Fails if customer has reservation on this flight
206 * -- Returns TRUE on success, else FALSE
207 * =============================================================================
209 boolean manager_deleteFlight(int flightId) {
210 Reservation reservationPtr = (Reservation)flightTablePtr.find(flightId);
211 if (reservationPtr == null) {
215 if (reservationPtr.numUsed > 0) {
216 return false; /* somebody has a reservation */
219 return addReservation(flightTablePtr,
221 -reservationPtr.numTotal,
222 -1 /* -1 keeps old price */);
226 /* =============================================================================
227 * manager_addCustomer
228 * -- If customer already exists, returns failure
229 * -- Returns TRUE on success, else FALSE
230 * =============================================================================
232 boolean manager_addCustomer(int customerId) {
233 Customer customerPtr;
236 if (customerTablePtr.contains(customerId)) {
240 customerPtr = new Customer(customerId);
241 // assert(customerPtr != null);
242 status = customerTablePtr.insert(customerId, customerPtr);
244 System.out.println("TMRESTART8");
252 /* =============================================================================
253 * manager_deleteCustomer
254 * -- Delete this customer and associated reservations
255 * -- If customer does not exist, returns success
256 * -- Returns TRUE on success, else FALSE
257 * =============================================================================
259 boolean manager_deleteCustomer (int customerId) {
260 Customer customerPtr;
261 RBTree reservationTables[]=new RBTree[NUM_RESERVATION_TYPE];
262 List_t reservationInfoListPtr;
266 customerPtr = (Customer) customerTablePtr.find(customerId);
267 if (customerPtr == null) {
271 reservationTables[RESERVATION_CAR] = carTablePtr;
272 reservationTables[RESERVATION_ROOM] = roomTablePtr;
273 reservationTables[RESERVATION_FLIGHT] = flightTablePtr;
275 /* Cancel this customer's reservations */
276 reservationInfoListPtr = customerPtr.reservationInfoListPtr;
277 it=reservationInfoListPtr.head;
278 while (it.nextPtr!=null) {
279 Reservation_Info reservationInfoPtr;
280 Reservation reservationPtr;
282 reservationInfoPtr =(Reservation_Info)it.dataPtr;
283 reservationPtr = (Reservation)reservationTables[reservationInfoPtr.type].find(reservationInfoPtr.id);
284 if (reservationPtr == null) {
285 System.out.println("TMRESTART9");
288 status = reservationPtr.reservation_cancel();
291 System.out.println("TMRESTART10");
296 status = customerTablePtr.remove(customerId);
298 System.out.println("TMRESTART11");
305 /* =============================================================================
307 * =============================================================================
311 /* =============================================================================
313 * -- Return numFree of a reservation, -1 if failure
314 * =============================================================================
316 int queryNumFree(RBTree tablePtr, int id) {
318 Reservation reservationPtr = (Reservation)tablePtr.find(id);
319 if (reservationPtr != null) {
320 numFree = reservationPtr.numFree;
327 /* =============================================================================
329 * -- Return price of a reservation, -1 if failure
330 * =============================================================================
332 int queryPrice (RBTree tablePtr, int id) {
334 Reservation reservationPtr=(Reservation) tablePtr.find(id);
335 if (reservationPtr != null) {
336 price = reservationPtr.price;
343 /* =============================================================================
345 * -- Return the number of empty seats on a car
346 * -- Returns -1 if the car does not exist
347 * =============================================================================
349 int manager_queryCar (int carId) {
350 return queryNumFree(carTablePtr, carId);
354 /* =============================================================================
355 * manager_queryCarPrice
356 * -- Return the price of the car
357 * -- Returns -1 if the car does not exist
358 * =============================================================================
360 int manager_queryCarPrice (int carId) {
361 return queryPrice(carTablePtr, carId);
365 /* =============================================================================
367 * -- Return the number of empty seats on a room
368 * -- Returns -1 if the room does not exist
369 * =============================================================================
371 int manager_queryRoom (int roomId) {
372 return queryNumFree(roomTablePtr, roomId);
376 /* =============================================================================
377 * manager_queryRoomPrice
378 * -- Return the price of the room
379 * -- Returns -1 if the room does not exist
380 * =============================================================================
382 int manager_queryRoomPrice (int roomId) {
383 return queryPrice(roomTablePtr, roomId);
387 /* =============================================================================
388 * manager_queryFlight
389 * -- Return the number of empty seats on a flight
390 * -- Returns -1 if the flight does not exist
391 * =============================================================================
393 int manager_queryFlight (int flightId) {
394 return queryNumFree(flightTablePtr, flightId);
398 /* =============================================================================
399 * manager_queryFlightPrice
400 * -- Return the price of the flight
401 * -- Returns -1 if the flight does not exist
402 * =============================================================================
404 int manager_queryFlightPrice(int flightId) {
405 return queryPrice(flightTablePtr, flightId);
409 /* =============================================================================
410 * manager_queryCustomerBill
411 * -- Return the total price of all reservations held for a customer
412 * -- Returns -1 if the customer does not exist
413 * =============================================================================
415 int manager_queryCustomerBill(int customerId) {
417 Customer customerPtr;
419 customerPtr = (Customer) customerTablePtr.find(customerId);
421 if (customerPtr != null) {
422 bill = customerPtr.customer_getBill();
429 /* =============================================================================
430 * RESERVATION INTERFACE
431 * =============================================================================
435 /* =============================================================================
437 * -- Customer is not allowed to reserve same (type, id) multiple times
438 * -- Returns TRUE on success, else FALSE
439 * =============================================================================
441 static boolean reserve (RBTree tablePtr, RBTree customerTablePtr,
442 int customerId, int id, int type) {
443 Customer customerPtr;
444 Reservation reservationPtr;
446 customerPtr = (Customer)customerTablePtr.find(customerId);
448 if (customerPtr == null) {
452 reservationPtr = (Reservation)tablePtr.find(id);
453 if (reservationPtr == null) {
457 if (!reservationPtr.reservation_make()) {
461 if (!customerPtr.customer_addReservationInfo(type,
463 reservationPtr.price)) {
464 /* Undo previous successful reservation */
465 boolean status = reservationPtr.reservation_cancel();
467 System.out.println("TMRESTART12");
477 /* =============================================================================
479 * -- Returns failure if the car or customer does not exist
480 * -- Returns TRUE on success, else FALSE
481 * =============================================================================
483 boolean manager_reserveCar (int customerId, int carId) {
484 return reserve(carTablePtr,
492 /* =============================================================================
493 * manager_reserveRoom
494 * -- Returns failure if the room or customer does not exist
495 * -- Returns TRUE on success, else FALSE
496 * =============================================================================
498 boolean manager_reserveRoom (int customerId, int roomId) {
499 return reserve(roomTablePtr,
507 /* =============================================================================
508 * manager_reserveFlight
509 * -- Returns failure if the flight or customer does not exist
510 * -- Returns TRUE on success, else FALSE
511 * =============================================================================
513 boolean manager_reserveFlight (int customerId, int flightId) {
514 return reserve(flightTablePtr,
522 /* =============================================================================
524 * -- Customer is not allowed to cancel multiple times
525 * -- Returns TRUE on success, else FALSE
526 * =============================================================================
528 static boolean cancel (RBTree tablePtr, RBTree customerTablePtr,
529 int customerId, int id, int type) {
530 Customer customerPtr;
531 Reservation reservationPtr;
533 customerPtr = (Customer) customerTablePtr.find(customerId);
534 if (customerPtr == null) {
538 reservationPtr = (Reservation) tablePtr.find(id);
539 if (reservationPtr == null) {
543 if (!reservationPtr.reservation_cancel()) {
547 if (!customerPtr.customer_removeReservationInfo(type, id)) {
548 /* Undo previous successful cancellation */
549 boolean status = reservationPtr.reservation_make();
551 System.out.println("TMRESTART13");
561 /* =============================================================================
563 * -- Returns failure if the car, reservation, or customer does not exist
564 * -- Returns TRUE on success, else FALSE
565 * =============================================================================
567 boolean manager_cancelCar(int customerId, int carId) {
568 return cancel(carTablePtr,
576 /* =============================================================================
578 * -- Returns failure if the room, reservation, or customer does not exist
579 * -- Returns TRUE on success, else FALSE
580 * =============================================================================
582 boolean manager_cancelRoom (int customerId, int roomId) {
583 return cancel(roomTablePtr,
592 /* =============================================================================
593 * manager_cancelFlight
594 * -- Returns failure if the flight, reservation, or customer does not exist
595 * -- Returns TRUE on success, else FALSE
596 * =============================================================================
598 boolean manager_cancelFlight(int customerId, int flightId) {
599 return cancel(flightTablePtr,