//assign a customer to this register public void AssignCustomerToThisRegister(Customer customer) { // if we have customer already in this register we are calculating the total time taken for this register to process this customer if (Customers != null && Customers.Any()) { if (Customers.Last().ElapsedTimeToCompleteThisCustomer > customer.ArrivalTime) { customer.ElapsedTimeToCompleteThisCustomer = Customers.Last().ElapsedTimeToCompleteThisCustomer + (TimeTakenToProcessEachItem * customer.Items); } else { customer.ElapsedTimeToCompleteThisCustomer = customer.ArrivalTime + (TimeTakenToProcessEachItem * customer.Items); } } else { customer.ElapsedTimeToCompleteThisCustomer = customer.ArrivalTime + (customer.Items * TimeTakenToProcessEachItem) - 1; } Customers.Add(customer); }
public static void ProcessFile(StreamReader file) { var noOfRegisters = 0; int counter = 0; string line; while ((line = file.ReadLine()) != null) { if (counter == 0) { noOfRegisters = int.Parse(line); } else { var customerInput = line.Split(' '); var customer = new Customer(); customer.CustomerType = customerInput[0].ToString(); customer.ArrivalTime = Convert.ToInt32(customerInput[1]); customer.Items = Convert.ToInt32(customerInput[2]); customerList.Add(customer); } //Console.WriteLine(line); counter++; } file.Close(); // building the registers for (int i = 1; i <= noOfRegisters; i++) { int timeTakenForItems = 1; if (i == noOfRegisters) // for last register we will set the time to two minutes { timeTakenForItems = 2; } registerList.Add(new Register() { Customers = new List<Customer>(), TimeTakenToProcessEachItem = timeTakenForItems }); } }
private static void AssignBTypeCustomerToRegister(Customer customer) { registerList.OrderBy(x => x.LastCustomerItemsForGivenMinute(customer.ArrivalTime)).FirstOrDefault().AssignCustomerToThisRegister(customer); customerList.Remove(customer); }
private static void ProcessGivenCustomer(Customer customer) { if (customer.CustomerType == "A") { AssignATypeCustomerToRegister(customer); } else if (customer.CustomerType == "B") { //Customer Type B looks at the last customer in each line, and always chooses to be behind the customer with the fewest number // of items left to check out, regardless of how many other customers are in the line or how many items they have. // Customer Type B will always choose an empty line before a line with any customers in it. AssignBTypeCustomerToRegister(customer); } }
private static void AssignATypeCustomerToRegister(Customer customer) { // find and assign the customer to the register registerList.OrderBy(x => x.CurrentCustomerCountForGivenMinute(customer.ArrivalTime)).FirstOrDefault().AssignCustomerToThisRegister(customer); // remove the customer for the customer list customerList.Remove(customer); }