bug fixes
[IRC.git] / Robust / src / Benchmarks / SingleTM / Vacation / Reservation.java
1 /* =============================================================================
2  *
3  * reservation.c
4  * -- Representation of car, flight, and hotel relations
5  *
6  * =============================================================================
7  *
8  * Copyright (C) Stanford University, 2006.  All Rights Reserved.
9  * Author: Chi Cao Minh
10  *
11  * =============================================================================
12  *
13  * For the license of bayes/sort.h and bayes/sort.c, please see the header
14  * of the files.
15  * 
16  * ------------------------------------------------------------------------
17  * 
18  * For the license of kmeans, please see kmeans/LICENSE.kmeans
19  * 
20  * ------------------------------------------------------------------------
21  * 
22  * For the license of ssca2, please see ssca2/COPYRIGHT
23  * 
24  * ------------------------------------------------------------------------
25  * 
26  * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
27  * header of the files.
28  * 
29  * ------------------------------------------------------------------------
30  * 
31  * For the license of lib/rbtree.h and lib/rbtree.c, please see
32  * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
33  * 
34  * ------------------------------------------------------------------------
35  * 
36  * Unless otherwise noted, the following license applies to STAMP files:
37  * 
38  * Copyright (c) 2007, Stanford University
39  * All rights reserved.
40  * 
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions are
43  * met:
44  * 
45  *     * Redistributions of source code must retain the above copyright
46  *       notice, this list of conditions and the following disclaimer.
47  * 
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
51  *       distribution.
52  * 
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.
56  * 
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.
68  *
69  * =============================================================================
70  */
71
72 public class Reservation {
73   public Reservation(int id, int numTotal, int price) {
74     this.id=id;
75     this.numUsed=0;
76     this.numFree=numTotal;
77     this.numTotal=numTotal;
78     this.price=price;
79     checkReservation();
80   }
81
82   int id;
83   int numUsed;
84   int numFree;
85   int numTotal;
86   int price;
87
88 /* =============================================================================
89  * checkReservation
90  * -- Check if consistent
91  * =============================================================================
92  */
93   public void checkReservation() {
94     int numUsed = this.numUsed;
95     int numFree = this.numFree;
96     int numTotal = this.numTotal;
97     int price = this.price;
98   }
99   
100   
101   /* =============================================================================
102    * reservation_addToTotal
103    * -- Adds if 'num' > 0, removes if 'num' < 0;
104    * -- Returns TRUE on success, else FALSE
105    * =============================================================================
106    */
107   boolean reservation_addToTotal (int num) {
108     if (numFree + num < 0) {
109       return false;
110     }
111     
112     numFree+=num;
113     numTotal+=num;
114     checkReservation();
115     return true;
116   }
117   
118   
119   /* =============================================================================
120    * reservation_make
121    * -- Returns TRUE on success, else FALSE
122    * =============================================================================
123    */
124   public boolean reservation_make() {
125     if (numFree < 1) {
126       return false;
127     }
128     numUsed++;
129     numFree--;
130     checkReservation();
131     return true;
132   }
133   
134
135 /* =============================================================================
136  * reservation_cancel
137  * -- Returns TRUE on success, else FALSE
138  * =============================================================================
139  */
140   boolean reservation_cancel() {
141     if (numUsed < 1) {
142       return false;
143     }
144     numUsed--;
145     numFree++;
146     checkReservation();
147     return true;
148   }
149
150   
151 /* =============================================================================
152  * reservation_updatePrice
153  * -- Failure if 'price' < 0
154  * -- Returns TRUE on success, else FALSE
155  * =============================================================================
156  */
157   boolean reservation_updatePrice (int newPrice) {
158     if (newPrice < 0) {
159       return false;
160     }
161     
162     this.price=newPrice;
163     checkReservation();
164     return true;
165   }
166
167
168 /* =============================================================================
169  * reservation_compare
170  * -- Returns -1 if A < B, 0 if A = B, 1 if A > B
171  * =============================================================================
172  */
173   int reservation_compare (Reservation aPtr, Reservation bPtr) {
174     return aPtr.id-bPtr.id;
175   }
176
177
178 /* =============================================================================
179  * reservation_hash
180  * =============================================================================
181  */
182   int reservation_hash() {
183     return id;
184   }
185   
186 }