/*****************************************************************************/
/*                                                                           */
/* Car Pool: Car Pool Vehicle Tracker Support Class (bitmap implementation)  */
/*                                                                           */
/*                       October 1999, Jacob Lundberg                        */
/*                                                                           */
/*****************************************************************************/

// cars in a CarPool are get'ed and returned in order.
// Unlike the non-bitmap version, there is no color here.

import java.util.BitSet;

class CarPool
  {
  // Bitmap.
  BitSet cars = null;
  int carcount = 0;

  // Constructor.
  public CarPool( int nbits )
    {
    int counter;
    // bitmap constructed by BitSet constructor
    carcount = nbits;
    // initialize the bitmap
    cars = new BitSet( nbits );
    for( counter = 0 ; counter < nbits ; counter++ )
      cars.set( counter );
    System.out.println( "CarPool: Contructed new CarPool." );
    if( nbits == 2 )
      System.out.println( "CarPool: Added car 0 via constructor." );
    else
      System.out.println( "CarPool: Added cars 0 through " + ( carcount - 1 ) + " via constructor." );
    }

  // Add a car or several.
  public boolean addCar( int num )
    {
    BitSet newCars = new BitSet( carcount + num );
    int counter;
    // initialize the new section of the bitmap
    for( counter = 0 ; counter < carcount + num ; counter++ )
      {
      if( counter < carcount && ! cars.get( counter ) )
        newCars.clear( counter );
      else
        newCars.set( counter );
      }
    cars = newCars;
    carcount += num;
    if( num < 2 )
      System.out.println( "CarPool: Added car " + ( carcount - 1 ) + "." );
    else
      System.out.println( "CarPool: Added cars " + ( carcount - 1 - num ) + " through " + ( carcount - 1 ) + "." );
    return true;
    }

  // Delete a car or several.
  public int delCar( int num )
    {
    if( carcount < 1 )
      {
      System.out.println( "CarPool: Can't delete. No cars exist." ); 
      return -1;
      }
    // We don't care what bits we leave in the deleted cars.
    carcount -= num;
    if( num < 2 )
      System.out.println( "CarPool: Deleted car " + carcount + "." );
    else
      System.out.println( "CarPool: Deleted cars " + carcount + " through " + ( carcount + num - 1 ) + "." );
    return carcount;
    }

  // Check a car out of the pool.
  public int getCar()
    {
    int counter;
    for( counter = 0 ; counter < carcount ; counter++ )
      if( cars.get( counter ) )
        {
        cars.clear( counter );
        System.out.println( "CarPool: Checked out car " + counter + "." );
        return( counter );
        }
    System.out.println( "CarPool: Can't get. No cars exist or all cars checked out." ); 
    return -1;
    }

  // Check a car into the pool.
  public boolean returnCar( int num )
    {
    if( num >= carcount || num < 0 )
      {
      System.out.println( "CarPool: Can't return. Car " + num + " doesn't exist." ); 
      return false;
      }
    cars.set( num );
    System.out.println( "CarPool: Returned car " + num + "." ); 
    return true;
    }

  // Count the total number of cars in a CarPool.
  public int countCars()
    {
    System.out.println( "CarPool: Number of cars total counted to be " + carcount + "." );
    return carcount;
    }
  }

