/// <summary> /// The priority method that is called when /// inserting orders into the ready for shipping queue /// </summary> /// <param name="a">An order to compare</param> /// <param name="b">Another order to compare</param> /// <returns></returns> public int Compare(Order a, Order b) { DateTime aTime = a.Created; DateTime bTime = b.Created; // Add the estimated delivery. // This forces shorter trips to have higher priority aTime = aTime.AddMinutes(fromWarehouse.GetOrderDeliveryMinutes(a)); bTime = bTime.AddMinutes(fromWarehouse.GetOrderDeliveryMinutes(b)); return(DateTime.Compare(bTime, aTime)); }
public virtual void ProcessOrder() { // Artificially advance the time to the next order waiting to be created // This is a fail safe, just in case the the processing of orders don't advance time enough if (!warehouse.HasOrders) { orderStreamer.AdvanceTime(); } // Keep processing orders while there are orders if (warehouse.HasOrders) { Order order; // If there isn't time for delivery the order should be moved to next day delivery if (!warehouse.HasTimeToDeliverNextOrder(orderStreamer.CurrentTime)) { warehouse.MoveNextOrderToNextDay(); } else if (warehouse.TrySendNextOrder(out order)) // Try to send the order out of the warehouse { // Create a delivered order and track its status and times DeliveredOrder outboundOrder = new DeliveredOrder(order.Id); outboundOrder.OrderPlaced = order.Created; outboundOrder.DepartureTime = orderStreamer.CurrentTime; outboundOrder.DeliveredTime = outboundOrder.DepartureTime; // Time traveled to the destination double travelMinutes = warehouse.GetOrderDeliveryMinutes(order); outboundOrder.DeliveredTime = outboundOrder.DeliveredTime.AddMinutes(travelMinutes); // Total time traveled, includes to destination and returning back to the warehouse travelMinutes += warehouse.GetOrderReturnMinutes(order); completedOrders.Add(outboundOrder); deliveredOrderAction(outboundOrder); switch (outboundOrder.GetRating()) { case OrderHelper.RatingType.Detractor: detractors++; break; case OrderHelper.RatingType.Promoter: promoters++; break; } warehouse.DockDrone(); // Update the mock global time (will also bring more new orders depending on the time) orderStreamer.AddMinutesToTime(travelMinutes); } } }