2005 Hotel
<< 2006Customer | APQuestionsTrailIndex | 2005Tickets >>
Question from the College Board
Reservation.java
public class Reservation { private String guestName; private int roomNumber; public Reservation(String guestName, int roomNumber){ this.setGuestName(guestName); this.setRoomNumber(roomNumber); } public void setGuestName(String guestName) { this.guestName = guestName; } public String getGuestName() { return guestName; } public void setRoomNumber(int roomNumber) { this.roomNumber = roomNumber; } public int getRoomNumber() { return roomNumber; } public String toString(){ return "Guest: "+this.getGuestName() +" Room: "+this.getRoomNumber(); } }
Hotel.java
import java.util.ArrayList; public class Hotel { private Reservation[] rooms; private ArrayList waitList; public Hotel(int numberOfRooms) { rooms=new Reservation[numberOfRooms]; waitList = new ArrayList(); } public Reservation requestRoom(String guestName){ /* to be implemented in part (a) */ return null; } public Reservation cancelAndResassign(Reservation res){ /* to be implemented in part (b) */ return null; } public void report(){ System.out.println("Rooms:"); for (int i=0; i<rooms.length;i++){ if (rooms[i]==null) System.out.println("Room "+i+": empty"); else System.out.println(rooms[i]); } System.out.println("Waiting List:"); if (waitList.size()==0){ System.out.println("empty"); } else for (Object n:waitList){ System.out.println((String)n); } } }
HotelTester.java
public class HotelTester { public static void main(String[] args){ Hotel hotel=new Hotel(5); hotel.report(); System.out.println("above should be empty"); Reservation a= hotel.requestRoom("Alice"); Reservation b=hotel.requestRoom("Bob"); Reservation c=hotel.requestRoom("Clare"); Reservation d=hotel.requestRoom("Dan"); Reservation e=hotel.requestRoom("Ed"); Reservation f=hotel.requestRoom("Fred"); hotel.requestRoom("Greg"); hotel.report(); System.out.println("above should be full with 2 waiting"); if (f==null){ System.out.println("f is null as it should be"); }else{ System.out.println("Error: f is not null as it should be"); } Reservation g=hotel.cancelAndResassign(c); Reservation h=hotel.cancelAndResassign(d); Reservation i=hotel.cancelAndResassign(e); hotel.report(); System.out.println("Fred should be in room 2, Clare should not be in the list"); System.out.println("Greg should be in room 3, Dan should not be in the list"); System.out.println("Room 4 should be empty"); if (i==null){ System.out.println("i is null as it should be"); }else{ System.out.println("Error: i is not null as it should be"); } } }