/* * initiate a retailer thread */ public void retailerFunc() { Retailer retailer = new Retailer(threadID); ChickenFarm chicken = new ChickenFarm(); Thread.Sleep(500); Int32 p = chicken.getPrice(); Console.WriteLine("\tStore{0} current price: ${1} each", Thread.CurrentThread.Name, p); }
public Program() { chickenFarm = new ChickenFarm(); retailersCount = 0; }
/* * This method retrieves the price of the chicken based on the availability */ public int retrieveNewPrice() { acquireLock(); try { ChickenFarm chickenFarm = new ChickenFarm(); int currentAvailability = chickenFarm.currentChickenAvailability(); /* * Divide the total number of chickens into 5 intervals */ int priceRange = currentAvailability / 5; /* * Calculate the range of the available chickens */ int priceForSwitchCase = (priceRange == 0) ? currentAvailability : (currentAvailability / priceRange); switch (priceForSwitchCase) { /* * Assign cost based on the value of the range */ case (0): //[0,20] { newPrice = random.Next(15, 20); } break; case (1): //[20,40] { newPrice = random.Next(20, 25); } break; case (2): //[40,60] { newPrice = random.Next(25, 30); } break; case (3): //[60,80] { newPrice = random.Next(30, 35); } break; case (4): //[80,100] case (5): //[80,100] { newPrice = random.Next(35, 40); } break; default: break; } return(newPrice); } finally { releaseLock(); } }