@matfah/

Hotel FR 2005

Java

No description

fork
loading
Files
  • Main.java
  • Hotel.java
  • Reservation.java
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import java.lang.reflect.*;
import java.util.ArrayList;

public class Main {
  
  public static void main(String[] args) {
    testRequestRoom();
    System.out.println("Passed requestRoom() tests!");
    testCancelAndReassign();
    System.out.println("Passed cancelAndReassign() tests!");
    System.out.println("All Tests Passed!");
  }
  
  public static void testRequestRoom() {
    
    Hotel h = new Hotel();
    
    Field roomsField = null;
    try {
      roomsField = Hotel.class.getDeclaredField("rooms");
    }
    catch(Exception e) {
      System.out.println("Did you remove the rooms instance variable?!");
      System.exit(0);
    }
    roomsField.setAccessible(true);
    
    Field waitListField = null;
    try {
      waitListField = Hotel.class.getDeclaredField("waitList");
    }
    catch(Exception e) {
      System.out.println("Did you remove the rooms instance variable?!");
      System.exit(0);
    }
    waitListField.setAccessible(true);
    
    Field nameField = null;
    try {
      nameField = Reservation.class.getDeclaredField("name");
    }
    catch(Exception e) {
      System.out.println("Did you remove the name instance variable?!");
      System.exit(0);
    }
    nameField.setAccessible(true);
    
    
    for(int waitListSize = 0; waitListSize <= 2; waitListSize++) {
      for(int roomsSize = 1; roomsSize <= 5; roomsSize++) {

        for(int occupiedRooms = 0; occupiedRooms <= roomsSize; occupiedRooms++) {

          if(waitListSize > 0) //if we have a waitlist, then the Hotel must be full!
            occupiedRooms = roomsSize;

          ArrayList<String> waitList = new ArrayList<String>();
          ArrayList<String> waitListClone = new ArrayList<String>();
          for(int i = 0; i < waitListSize; i++) {
            String randName = randomName();
            waitList.add(randName);
            waitListClone.add(randName);
          }
          
          Reservation[] rooms = new Reservation[roomsSize];
          Reservation[] roomsClone = new Reservation[roomsSize];
          
          for(int j = 0; j < occupiedRooms; j++) {
            int randSpot = (int)(Math.random()*roomsSize);
            if(rooms[randSpot] != null) {
              j--;
            }
            else {
              roomsClone[randSpot] = rooms[randSpot] = new Reservation(randomName(), randSpot);
            }
          }
          
          
          try {
            waitListField.set(h, waitListClone);
            roomsField.set(h, roomsClone);
          }
          catch(Exception e) {
            System.out.println("Did you change the types of the instance variables?!");
            System.exit(0);
          }
          
          String randomName = randomName();
          Reservation request = null;
          try {
            request = h.requestRoom(randomName);
          }
          catch(Exception e) {
            System.out.println("Error calling requestRoom() method");
            e.printStackTrace();
            System.exit(0);
          }
          
          if(occupiedRooms == roomsSize) { //no room
            if(request != null) {
              System.out.println("Making a request for a room when the Hotel is full should return null... but your code does NOT return null!!!");
              System.exit(0);
            }
            
            waitList.add(randomName);
            if(!waitList.equals(waitListClone)) {
              System.out.println("Making a request for a room when the Hotel is full is should add the name to the end of the waitList... your code does NOT do this correctl!");
              System.exit(0);
            }
            waitList.remove(waitList.size()-1);
            
            if(differences(rooms, roomsClone).size() != 0) {
              System.out.println("You should NOT change the rooms instance variable if the Hotel is full and a room is requested");
              System.exit(0);
            }
          }
          else { //there is room
            
            if(request == null) {
              System.out.println("Making a request for a room when the Hotel is not full should return a Reservation... but instead your code is returning null!");
              System.exit(0);
            }
            
            if(!waitList.equals(waitListClone)) {
              System.out.println("Making a request for a room when the Hotel is not full is incorrectly modifying the waitList");
              System.exit(0);
            }
            
            if(rooms.length != roomsClone.length) {
              System.out.println("Requesting a room modified the length of the rooms array...!");
              System.exit(0);
            }
            
            ArrayList<Integer> diffs = differences(rooms, roomsClone);
            if(diffs.size() != 1) {
              System.out.println("Requesting a room did not change exactly one spot in the rooms array!");
              System.exit(0);
            }
            
            if(rooms[diffs.get(0)] != null) {
              System.out.println("Requesting a room changed a non-null spot in the rooms array");
              System.exit(0);
            }
            
            if(roomsClone[diffs.get(0)] == null) {
              System.out.println("Requesting a room changed a spot in the rooms array to be null");
              System.exit(0);
            }
            
            String returnedName = null;
            try {
              returnedName = (String)nameField.get(request);
            }
            catch(Exception e) {
              System.out.println("Did you change the types of the instance variables?!");
              System.exit(0);
            }
            
            if(diffs.get(0) != request.getRoomNumber() || !randomName.equals(returnedName)) {
              
              System.out.println("Returned Reservation for requesting a new room is wrong");
              System.exit(0);
            }
          }
        }
      }
    }
    
  }
  
  public static void testCancelAndReassign() {
    CustomHotel h = new CustomHotel();
    
    Field roomsField = null;
    try {
      roomsField = Hotel.class.getDeclaredField("rooms");
    }
    catch(Exception e) {
      System.out.println("Did you remove the rooms instance variable?!");
      System.exit(0);
    }
    roomsField.setAccessible(true);
    
    Field waitListField = null;
    try {
      waitListField = Hotel.class.getDeclaredField("waitList");
    }
    catch(Exception e) {
      System.out.println("Did you remove the rooms instance variable?!");
      System.exit(0);
    }
    waitListField.setAccessible(true);
    
    Field nameField = null;
    try {
      nameField = Reservation.class.getDeclaredField("name");
    }
    catch(Exception e) {
      System.out.println("Did you remove the name instance variable?!");
      System.exit(0);
    }
    nameField.setAccessible(true);
    
    
    for(int waitListSize = 0; waitListSize <= 2; waitListSize++) {
      for(int roomsSize = 1; roomsSize <= 5; roomsSize++) {

        for(int occupiedRooms = 1; occupiedRooms <= roomsSize; occupiedRooms++) {

          if(waitListSize > 0) //if we have a waitlist, then the Hotel must be full!
            occupiedRooms = roomsSize;

          ArrayList<String> waitList = new ArrayList<String>();
          ArrayList<String> waitListClone = new ArrayList<String>();
          for(int i = 0; i < waitListSize; i++) {
            String randName = randomName();
            waitList.add(randName);
            waitListClone.add(randName);
          }
          
          Reservation[] rooms = new Reservation[roomsSize];
          Reservation[] roomsClone = new Reservation[roomsSize];
          Reservation toCancel = null;
          
          for(int j = 0; j < occupiedRooms; j++) {
            int randSpot = (int)(Math.random()*roomsSize);
            if(rooms[randSpot] != null) {
              j--;
            }
            else {
              roomsClone[randSpot] = rooms[randSpot] = new Reservation(randomName(), randSpot);
              if(toCancel == null || Math.random() < 1.0 / occupiedRooms)
                toCancel = roomsClone[randSpot];
            }
          }
          
          
          try {
            waitListField.set(h, waitListClone);
            roomsField.set(h, roomsClone);
          }
          catch(Exception e) {
            System.out.println("Did you change the types of the instance variables?!");
            System.exit(0);
          }
          
          
          Reservation request = null;
          
          try {
            request = h.cancelAndReassign(toCancel);
          }
          catch(Exception e) {
            System.out.println("Error calling cancelAndReassign() method");
            e.printStackTrace();
            System.exit(0);
          }
          
          if(waitList.size() == 0) { //no one will get the room!
            
            if(request != null) {
              System.out.println("When canceling a room with nobody on the waitlist, null is not returned");
              System.exit(0);
            }
            
            if(roomsClone[toCancel.getRoomNumber()] != null) {
              System.out.println("When canceling a room with nobody on the waitlist, null is not placed in rooms array at the canceled location");
              System.exit(0);
            }
            
            if(rooms.length != roomsClone.length) {
              System.out.println("When canceling a room with nobody on the waitlist, the rooms array should not change in length!");
              System.exit(0);
            }
            
            if(differences(rooms, roomsClone).size() != 1) {
              System.out.println("When canceling a room with nobody on the waitlist, there is not exactly one change made to the rooms array");
              System.exit(0);
            }
          }
          else { //we have a waitlist!

            waitListClone.add(0, waitList.get(0));
            
            if(!waitListClone.equals(waitList)) {
              System.out.println("When canceling a room with somebody on the waitlist, the waitlist is modified incorrectly");
              System.exit(0);
            }
            
            String returnedName = null;
            try {
              returnedName = (String)nameField.get(request);
            }
            catch(Exception e) {
              System.out.println("Did you change the types of the instance variables?!");
              System.exit(0);
            }
            
            if(!waitList.get(0).equals(returnedName)) {
              System.out.println("When canceling a room with somebody on the waitlist, the reservation is not filled with the first person on the waitlist");
              System.exit(0);
            }
            
            if(roomsClone[toCancel.getRoomNumber()] != request) {

              System.out.println("When canceling a room with somebody on the waitlist, the returned reservation is not the same as the one in the rooms array");
              System.exit(0);
            }
          
             if(toCancel.getRoomNumber() != request.getRoomNumber()) {
               System.out.println("When canceling a room with somebody on the waitlist, the returned reservation has a room that is different than the room that was canceled");
              System.exit(0);
             }
             
             if(differences(rooms, roomsClone).size() != 1) {
               System.out.println("When canceling a room with somebody on the waitlist, the rooms array is not changed in exactly one way");
              System.exit(0);
             }

             //I'm a little surprised they didn't require this!
             if(h.name == null && noCallRequestRoomCounter == 0) {
              System.out.println("\n***When canceling a room with somebody on the waitlist, you could haved called requestRoom().  It would be less efficient to do so, but would make your code shorter.  This did not result in a deduciont at the AP Reading.***\n");
              noCallRequestRoomCounter++;
             }
            
          }
        }
      }
    }
  }
  
  private static int noCallRequestRoomCounter = 0;
  
  private static String[] names = {"Amy", "Brian", "Colby", "Dave", "Emily", "Frank", "Gina"};
  
  public static String randomName() {
    return names[(int)(Math.random()*names.length)];
  }
  
  public static ArrayList<Integer> differences(Reservation[] rooms1, Reservation[] rooms2) {
    ArrayList<Integer> diffs = new ArrayList<Integer>();
    
    int max = Math.max(rooms1.length, rooms2.length);
    int min = Math.min(rooms1.length, rooms2.length);
    
    for(int i = 0; i < max; i++) {
      if(i >= min) diffs.add(i);
      else if(rooms1[i] != rooms2[i]) diffs.add(i);
    }
    
    return diffs;
    
  }
  
}

class CustomHotel extends Hotel {
  
    String name = null;
  
    public Reservation requestRoom(String guestName) {
      name = guestName;
      return super.requestRoom(guestName);
    }
}