• Reservation is simply an array of name/String

    initial array
    nullnullnullnullnullnullnull

    Ann reserves a room
    Annnullnullnullnullnullnull

    Bob reserves a room
    AnnBobnullnullnullnullnull

    Bob cancel the room
    Annnullnullnullnullnullnull

  • Reservation.java
    import java.util.*;
    
    /**
     * This class models a hotel reesrvation system
     *
     * @author Ting Shih
     * @version 1.0.0
     */
    public class Reservation {
    
    	/*
    	 * occupancy is an array that indicates the occupant of each room.  
    	 * If there are no occupants, it is marked null
    	 */
    	private String[] occupancy;
    
    	/* MAX_ROOMS is the maximum number of rooms.and also represent the array size */
    	private int MAX_ROOMS = 5;
    
    	/**
    	 * Creates a list of rooms and make them available (by setting them to null).
    	 */
    	public Reservation()  {
    
    		/* creates the array of occupancy for the size of MAX_ROOMS */
    		occupancy = new String[MAX_ROOMS];
    
    		/* 
    		 * The following is not necessary, since a newly constructed array
    		 * will have null values by default. But we will set them to null
    		 * for illustrative purposes.
    		 */
    		for (int index = 0; index < occupancy.length; index++)  {
    			occupancy[index] = null;
    		}
    	}
    
    	/**
    	 * Adds a new customer to the hotel.
    	 * 
    	 * @param customer is the name of the customer to be added.
    	 * @return true if the hotel is not full and reservatoin was made successfully,
    	 *         false otherwise.
    	 */
    	public boolean reserve (String customer)  {
    
    		int roomChecked = 0; 
    	
    		/* Use a while-loop to check if a room is available */
    		while (roomChecked < occupancy.length && occupancy[roomChecked] != null )  {
    			roomChecked ++; 
    		}
    
    		/*
    		 * if roomChecked is is less than the number of rooms in the hotel, 
    		 * then there is an availability in the hotel, otherwise, all the rooms 
    		 * have been searched and no availability was found 
    		 */
    		if (roomChecked < occupancy.length)  {
    			occupancy[roomChecked] = customer;
    			return true;
    		} else {
    			return false;
    		}
    	}
    
    	/** 
    	 * checks out a customer and marks the room as empty
    	 *
    	 * @param customer is the name of the customer checking out
    	 * @return true if customer had a prior reservation so cancellation possible,
    	 *         false otherwise.
    	 */
    	public boolean cancel (String customer)  {
    
    		int roomChecked = 0;
    
    		/* searches all the rooms until the customer who had a reservation is found */
    		while (roomChecked < occupancy.length && 
    			(occupancy[roomChecked] == null || !occupancy[roomChecked].equals(customer)) )  {		
    			roomChecked++; 
    		}
    	
    		/* 
    		 * system cancels the reservation made by marking the room available
    		 * otherwise return false to indicate the customer did not make a reservation
    		 */
    		if (roomChecked < occupancy.length)  {
    			occupancy[roomChecked] = null;  
    	 		return true;
    		} else {
    			return false;
    		}
    	}
    
    }