/// <summary> /// calculates the time the customer CUST will spend in the queue /// QueueNumber is the number of the Queue customer CUST is at /// </summary> private int TimesInQueues(int QueueNumber, Customer cust) { if (Checkouts.ElementAt(QueueNumber).Customers > 4) // if there are more than 4 customers, Queue becomes faster { Checkouts.ElementAt(QueueNumber).AssignedStaffMember.Speed = Checkouts.ElementAt(QueueNumber).AssignedStaffMember.MaxSpeed; } return (cust.Items + Checkouts.ElementAt(QueueNumber).Items) * Checkouts.ElementAt(QueueNumber).AssignedStaffMember.Speed; }
/// <summary> /// Begins running the simulation. /// </summary> public void Run() { for (int hour = 0; hour < 12; hour++) { int f = hour+1; custsEntering = Hours.ElementAt(hour).totalCustomers; customerDistribution = customersPerMinuteMethod(custsEntering); for (int t = 0; t < Hours.ElementAt(hour).customerTypeCounter; t++) { SQLiteResult resultT = SQLiteController.Instance.Query("SELECT hour" + f + " FROM Customers"); int totalCustOfType = Convert.ToInt32(resultT.Rows[t]["hour" + f]); resultT = SQLiteController.Instance.Query("SELECT Type FROM Customers"); String custType = (String)resultT.Rows[t]["Type"]; SQLiteResult resultZ = SQLiteController.Instance.Query("SELECT * FROM Customers WHERE (type = '" + custType + "')"); minItems = Convert.ToInt32(resultZ.Rows[0]["minItems"]); maxItems = Convert.ToInt32(resultZ.Rows[0]["maxItems"]); Concentration = Convert.ToInt32(resultZ.Rows[0]["concentration"]); Dawdling = Convert.ToInt32(resultZ.Rows[0]["dawdling"]); Patience = Convert.ToInt32(resultZ.Rows[0]["patience"]); for (int h = 0; h < totalCustOfType; h++) { HourlyCust.Add(new CustomerTypes(custType)); HourlyCust.ElementAt(HourlyCust.Count - 1).minItems = minItems; HourlyCust.ElementAt(HourlyCust.Count - 1).maxItems = maxItems; HourlyCust.ElementAt(HourlyCust.Count - 1).Concentration = Concentration; HourlyCust.ElementAt(HourlyCust.Count - 1).Dawdling = Dawdling; HourlyCust.ElementAt(HourlyCust.Count - 1).Patience = Patience; } } shuffleList(HourlyCust); for (secondsTimer = 0; secondsTimer < 3600; secondsTimer++) { if (secondsTimer % 60 == 0) { forTheMinute = customerDistribution[secondsTimer / 60]; for (int w = 0; w < forTheMinute; w++) { Customer cust = new Customer(HourlyCust.ElementAt(0).custType, HourlyCust.ElementAt(0).minItems, HourlyCust.ElementAt(0).maxItems); cust.EntryTime = dayTimer; cust.Concentration = HourlyCust.ElementAt(0).Concentration; cust.Dawdling = HourlyCust.ElementAt(0).Dawdling; cust.Patience = HourlyCust.ElementAt(0).Patience; Shoppers.Add(cust); HourlyCust.RemoveAt(0); } calcEventTimes(dayTimer, Shoppers.Count); } if (nextEventTime == dayTimer) { Customer cust2 = Shoppers.ElementAt(0); int itemSearchTime = dayTimer - cust2.EntryTime; if (Shoppers.ElementAt(0).Shopping) { int basket = Shoppers.ElementAt(0).Basket + 1; Shoppers.ElementAt(0).Basket = basket; if (Shoppers.ElementAt(0).Basket == Shoppers.ElementAt(0).Items) { int shortestQueueIndex = 0; int queueLength = TimesInQueues(0, cust2); ; for (int d = 1; d < Checkouts.Count; d++) { if (queueLength > TimesInQueues(d, cust2)) { shortestQueueIndex = d; queueLength = TimesInQueues(d, cust2); } } double timePreparedToQueue = (double)itemSearchTime / cust2.Patience; if (timePreparedToQueue > queueLength) { cust2.Shopping = false; Checkouts.ElementAt(shortestQueueIndex).Customers++; Checkouts.ElementAt(shortestQueueIndex).Items = Checkouts.ElementAt(shortestQueueIndex).Items + cust2.Items; } else { cust2.LeavingTime = dayTimer; Shoppers.RemoveAt(0); Hours.ElementAt(hour).customersLeaving++; Hours.ElementAt(hour).customersWalkedOut++; Hours.ElementAt(hour).customerTimeSpentShopping = Hours.ElementAt(hour).customerTimeSpentShopping + itemSearchTime; Hours.ElementAt(hour).customerTotalHappiness = Hours.ElementAt(hour).customerTotalHappiness + 0; } } } else { cust2.LeavingTime = dayTimer; double happiness = itemSearchTime / (dayTimer - cust2.EntryTime); Shoppers.RemoveAt(0); Hours.ElementAt(hour).customersLeaving++; Hours.ElementAt(hour).customerTimeSpentShopping = Hours.ElementAt(hour).customerTimeSpentShopping + itemSearchTime; Hours.ElementAt(hour).customerTotalHappiness = Hours.ElementAt(hour).customerTotalHappiness + happiness; } calcEventTimes(dayTimer, Shoppers.Count); } dayTimer++; } } }