/*

THIS SOFTWARE IS DISTRIBUTED UNDER GPL LICENSE

Copyright (C) 2006  Lukasz Grzegorz Maciak
http://terminally-incoherent.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA or visit http://www.gnu.org/licenses/gpl.txt.

*/



import java.util.Random;

/*
 * Created on Mar 24, 2006
 */

/**
 * @author Lukasz Grzegorz Maciak
 *
 */
public class Customer extends Thread
{
	private String name; 			// The name/id of this customer
	private BarberShop shop; 		// The BarberShop instance associated with this customer

	private Random r;				// A random number generator

	private boolean wants_haircut;	// indicates if the customer wants a haircut

	/**
	 * Default constructor.
	 *
	 * @param shop BarberShop instance to use
	 * @param name an unique name for this customer
	 */
	public Customer(BarberShop shop, String name)
	{
		this.name = name;
		this.shop = shop;
		wants_haircut = true;

		r = new Random();

	}


	public void run()
	{
		wasteTime();
		shop.customerReady(this);

	}

	/**
	 * Check if the customer still wants a haircut.
	 *
	 * @return true if the customer wants a haircut, fals otherwise
	 */
	public boolean wantsHaircut()
	{
		return wants_haircut;
	}

	/**
	 * Indicate that the customer wants to leave. This will cause
	 * wantsHaircut() to return false from now on.
	 *
	 */
	public void wantsToLeave()
	{
		wants_haircut = false;
	}

	/**
	 * The customer will idle for a random time period. This ensures that
	 * the customers arrive at unpredictable times, or can arbitrarily wait
	 * at different times.
	 */
	public void wasteTime()
	{
		try
		{
			this.sleep(Math.abs(r.nextInt(100000)));
		}
		catch (InterruptedException e)
		{
			e.printStackTrace();
		}
	}


	public String toString()
	{
		return name;
	}


}
