Question: need help python only i want to show how long the barber takes cutting hair and how long the customer takes to pay the cashier
need help
python only
i want to show how long the barber takes cutting hair and how long the customer takes to pay the cashier please help
this is the code
from threading import Thread, Lock, Event import time, random mutex = Lock() #Interval in seconds customerIntervalMin = 1 customerIntervalMax = 9 haircutDurationMin = 4 haircutDurationMax = 20 cashierDurationMin = 2 cashierDurationMax = 5 class BarberShop: waitingCustomers = [] def __init__(self, barber, numberOfSeats,cashier): self.barber = barber self.numberOfSeats = numberOfSeats self.cashier = cashier print('BarberShop initilized with {0} seats'.format(numberOfSeats)) print('Customer min interval {0}'.format(customerIntervalMin)) print('Customer max interval {0}'.format(customerIntervalMax)) print('Haircut min duration {0}'.format(haircutDurationMin)) print('Haircut max duration {0}'.format(customerIntervalMax)) print('Cashier min duration {0}'.format(cashierDurationMin)) print('Cashier max duration {0}'.format(cashierDurationMax)) print('---------------------------------------') def openShop(self): print('Barber shop is opening') workingThread = Thread(target = self.barberGoesToWork) workingThread.start() def barberGoesToWork(self): while True: mutex.acquire() if len(self.waitingCustomers) > 0: c = self.waitingCustomers[0] del self.waitingCustomers[0] mutex.release() self.barber.cutHair(c) else: mutex.release() print('Aaah, all done, going to sleep') barber.sleep() print('Barber woke up') def enterBarberShop(self, customer): mutex.acquire() print('>> {0} entered the shop and is looking for a seat'.format(customer.name)) if len(self.waitingCustomers) >= self.numberOfSeats: print('All barbers are occupied, {0} is going to the waiting room.'.format(customer.name)) self.waitingCustomers.append(c) mutex.release() if len(self.waitingCustomers) < self.numberOfSeats: print('{0} went to the chair'.format(customer.name)) self.waitingCustomers.append(c) mutex.release() barber.wakeUp() class Customer: def __init__(self, name): self.name = name class Barber: barberWorkingEvent = Event() def sleep(self): self.barberWorkingEvent.wait() def wakeUp(self): self.barberWorkingEvent.set() def cutHair(self, customer): #Set barber as busy self.barberWorkingEvent.clear() print('{0} is having a haircut'.format(customer.name)) randomHairCuttingTime = random.randrange(haircutDurationMin, haircutDurationMax+1) time.sleep(randomHairCuttingTime) print('{0} is going to the cashier to pay'.format(customer.name)) print('{0} is done'.format(customer.name)) class Cashier: cashierWorkingEvent = Event() def work(self, customer): self.cashierWorkingEvent.clear() print('{0} is going to pay for the haircut.'.format(customer.name)) randomPaymentTime = random.randrange(cashierDurationMin, cashierDurationMax+1) print('{0} paid and is going home.'.format(customer.name)) if __name__ == '__main__': customers = [] customers.append(Customer('Rick')) customers.append(Customer('Damien')) customers.append(Customer('Maria')) customers.append(Customer('Axel')) customers.append(Customer('Andrea')) customers.append(Customer('Angel')) customers.append(Customer('Kyairaa')) customers.append(Customer('Daniel')) customers.append(Customer('Kat')) customers.append(Customer('Miguel')) customers.append(Customer('Kevin')) customers.append(Customer('John')) customers.append(Customer('Bryan')) customers.append(Customer('Matt')) customers.append(Customer('Chris')) customers.append(Customer('Larssen')) customers.append(Customer('Ruben')) barber = Barber() cashier = Cashier() numberOfSeats = 3 barberShop = BarberShop(barber, numberOfSeats, cashier) barberShop.openShop() while len(customers) > 0: c = customers.pop() #New customer enters the barbershop barberShop.enterBarberShop(c) customerInterval = random.randrange(customerIntervalMin,customerIntervalMax+1) time.sleep(customerInterval)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
