private Cab findbubbleUpcab(PickupRequest pickupReq) { Cab result = null; int closest = int.MaxValue; foreach (var cab in cabs) { if (cab.isCabGoingLeft() == pickupReq.isGoingLeft() && cab.isReqGoingLeft() == pickupReq.isGoingLeft()) { if (cab.isstopInBetween(pickupReq.getStop())) { if (cab.getDistTostop(pickupReq.getStop()) < closest) { result = cab; } } } } return(result); }
public void pickup(int stop, int destination) { checkstopValidity(stop); PickupRequest pickupReq = new PickupRequest(stop, destination, noOfStops); Cab cab = findbubbleUpcab(pickupReq); if (cab != null) { cab.addDestStop(pickupReq.getStop()); Console.WriteLine("\bubbleUp possible for stop: " + pickupReq.getStop() + " isGoingLeft: " + pickupReq.isGoingLeft() + " added to cabID: " + cab.getId()); } else { requests.Add(pickupReq); Console.WriteLine("\nAdding to FIFO queue. bubbleUp NOT possible for stop: " + pickupReq.getStop() + " isGoingLeft: " + pickupReq.isGoingLeft()); } }
public void step() { // Move all cabs by one step and update idle logic foreach (var cab in cabs) { cab.move(); } List <int> processed = new List <int>(); // Process FIFO queue for (int i = 0; i < requests.Count; i++) { PickupRequest req = requests[i]; Cab closestcab = findIdlecab(req.getStop()); if (closestcab != null) { closestcab.addPickupReq(req); Console.WriteLine("\nFIFO req: " + req.getStop() + " isGoingLeft: " + req.isGoingLeft() + " assigned to cabID: " + closestcab.getId()); processed.Add(i); } else { closestcab = findbubbleUpcab(req); if (closestcab != null) { closestcab.addDestStop(req.getStop()); Console.WriteLine("\nbubbleUp possible for stop: " + req.getStop() + " isGoingLeft: " + req.isGoingLeft() + " added to cabID: " + closestcab.getId()); processed.Add(i); } } } // Remove all processed requests for (int i = processed.Count - 1; i >= 0; i--) { requests.Remove(requests[i]); } }