Click here to Skip to main content
15,883,843 members
Articles / Programming Languages / C

Simulation of Barber shop problem

Rate me:
Please Sign up or sign in to vote.
1.29/5 (11 votes)
9 Jul 2008CPOL3 min read 36.3K   763   10  
Simulation of Barber shop problem
#include <stdlib.h>


class CBarberShop
{
	private:

		int m_NoWaitingChairs,m_NoCustomers,m_HairCutTime;
		const int MAX_CHAIRS;
		bool m_BarberStatus,IsRandEven;

	public:
		
		CBarberShop():MAX_CHAIRS(5)  //Constructor initializing variables
		{
			m_NoWaitingChairs=5;
			m_NoCustomers=0;
			m_HairCutTime=0;
			m_BarberStatus=false;
		}
		
		bool Barber_Status()   //returns the barber's status. true=busy; false=free;
		{
			if(m_BarberStatus == false)
			   return false;
			else
			   return true;
		}
		void Wakeup()  // if the barber is sleeping customer wakes him up
		{
			cout << "Barber is sleeping. Wake him up" << endl;
			Set_Barber_Status(true);
		}

		//constantly checks for new customer arriaval. If the random number is even, it triggers
		// new customer arrival. if random number is odd, no trigger.

		void Check_For_New_Customer() 
		{
			IsRandEven =  IsEven(rand());

		   if(IsRandEven ==  true)
		   {
			  cout << "NEW CUSTOMER ARRIVED" << endl<<endl;
			  
			  m_NoCustomers++;
			  m_NoWaitingChairs--;

			  if(m_NoWaitingChairs < 0)
				  m_NoWaitingChairs=0;

		   }
			
		}
		//while barber doing haircut. decrease no.of customers and increase waiting chairs.

		void Haircut()
		{
			m_NoCustomers--;
			m_NoWaitingChairs++;
		}

		//gets the current customer and seats info

		void Get_Customer_Seat_Status()
		{
			cout << "No of customers waiting:" << No_Customers() << endl;
			cout << "No. of available waiting chairs:" << No_WaitChairs_Available() << endl<<endl;
		}

		// sets the barber status to true or false depending on the customers.

		void Set_Barber_Status(bool status)
		{
			m_BarberStatus=status;
		}

		//returns no.of customers at any time
		int No_Customers()
		{
			if(m_NoCustomers > 5)
			   m_NoCustomers = 5;
			return m_NoCustomers;
		}
		//retuns no. of waiting chairs availalbe at any time
		int No_WaitChairs_Available()
		{
			return m_NoWaitingChairs;
		}

		bool IsEven(int x)
		{	
		   if((x % 2) == 0)
		     return true;
		   else
		     return false;
		}	

};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I am Ananth Nag Sastry Ganti, working as a software engineer at IBM T.J Watson Research Center, NY. I am working on Image processing and Video suviellence projects. My areas of interests include DSP, Image Processing.

Any advices or suggestions on my articles are always welcome.

Comments and Discussions