Improving this implementation Implementation of delegates for flight operations
I borrowed the code from this [question][1]question. I'm trying to implement the delegate in that code. Below is my outcome so far.
EDIT
MyHere's my implementation of the ConductFlightOperations
. I'm not sure if this is proper way of doing it.
My ImplementationHere's my implementation of the Deconflict
. Right now it only check for someFlight.ArrivalTime
(which is for Landing flights), what should be proper way to reuse same code to check for someFlight.DepartureTime
(for Departing flights)? Should I pass some flag or create new method or something else?
Please advice for these 2 methods [1]: http://stackoverflow.com/questions/19405571/failed-to-understand-the-use-of-delegates-in-real-world-scenarios
Improving this implementation of delegates
I borrowed the code from this [question][1]. I'm trying to implement the delegate in that code. Below is my outcome so far.
EDIT
My implementation of the ConductFlightOperations
. I'm not sure if this is proper way of doing it.
My Implementation of the Deconflict
. Right now it only check for someFlight.ArrivalTime
(which is for Landing flights), what should be proper way to reuse same code to check for someFlight.DepartureTime
(for Departing flights)? Should I pass some flag or create new method or something else?
Please advice for these 2 methods [1]: http://stackoverflow.com/questions/19405571/failed-to-understand-the-use-of-delegates-in-real-world-scenarios
Implementation of delegates for flight operations
I borrowed the code from this question. I'm trying to implement the delegate in that code. Below is my outcome so far.
Here's my implementation of ConductFlightOperations
. I'm not sure if this is proper way of doing it.
Here's my implementation of Deconflict
. Right now it only check for someFlight.ArrivalTime
(which is for Landing flights), what should be proper way to reuse same code to check for someFlight.DepartureTime
(for Departing flights)? Should I pass some flag or create new method or something else?
I borrowed the code from this question [question][1]. I'm trying to implement the delegate in that code. Below is my outcome so far.
class Program
{
static void Main(string[] args)
{
var flights = new List<Flight>
{
new Flight {Number = "FL001", DepartureTime = DateTime.Now},
new Flight {Number = "FL002", DepartureTime = DateTime.Now.AddHours(1)},
new Flight {Number = "FL003", ArrivalTime = DateTime.Now.AddMinutes(30)},
new Flight {Number = "FL004", ArrivalTime = DateTime.Now.AddHours(1.30)},
};
var tower = new FlightTower(flights);
// Flight 003 asking for landing
var flightAskingForLanding= flights.FirstOrDefault(x => x.Number == "FL003");
if (flightAskingForLanding != null)
{
Console.WriteLine(string.Format("Flight {0} \t: Okay to land?", flightAskingForLanding.Number));
Console.WriteLine(string.Format("Flight Tower \t: {0}", tower.CanLand(flightAskingForLanding)));
}
Func<Flight, bool> checkSchedule = x => flightAskingForLanding.ArrivalTime.IsConflictWithFlightTime(x.ArrivalTime) && flightAskingForLanding.Number != x.Number;
checkSchedule += x => flightAskingForLanding.ArrivalTime.IsConflictWithFlightTime(x.DepartureTime) && flightAskingForLanding.Number != x.Number;
if (flightAskingForLanding != null)
{
Console.WriteLine(string.Format("Flight {0} \t: Okay to land?", flightAskingForLanding.Number));
Console.WriteLine(string.Format("Flight Tower \t: {0}", tower.CheckRunwayStatus(flightAskingForLanding,
checkSchedule,
x => x.ArrivalTime == flights.Min(f => f.ArrivalTime))));
}
Console.ReadKey();
}
}
public class FlightTower
{
private readonly List<Flight> _schedule;
public FlightTower(List<Flight> schedule)
{
_schedule = schedule;
}
//this method is to show my initial implementation
public bool CanTakeOff(Flight flight)
{
//todo: add code to check for both arrival time and departure time as in the CanLand method
var arrivingFlights = _schedule.Where(x => x.ArrivalTime == DateTime.Now);
if (arrivingFlights.ToList().Count == 0)
{
var flightInQueue = _schedule.FirstOrDefault(x => x.DepartureTime == _schedule.Min(c => c.DepartureTime));
if (flightInQueue != null && flightInQueue.Number == flight.Number)
{
return true;
}
}
return false;
}
//this method is to show my initial implementation
public bool CanLand(Flight flight)
{
var arrvingFlight = _schedule.Where(x => flight.ArrivalTime.IsConflictWithFlightTime(x.ArrivalTime) && flight.Number != x.Number);
var depatingFlight = _schedule.Where(x => flight.ArrivalTime.IsConflictWithFlightTime(x.DepartureTime) && flight.Number != x.Number);
if (arrvingFlight.ToList().Count == 0 && depatingFlight.ToList().Count == 0)
{
var flightInQueue = _schedule.FirstOrDefault(x => x.ArrivalTime == _schedule.Min(c => c.ArrivalTime));
if (flightInQueue != null && flightInQueue.Number == flight.Number)
return true;
}
return false;
}
//this will replace above CanLand and CanTakeOff methods
public bool CheckRunwayStatus(Flight flight, Func<Flight, bool> checkSchedule, Func<Flight, bool> checkFlightQueue)
{
var flightStatus = _schedule.Where(checkSchedule);
if (flightStatus.ToList().Count == 0 )
{
var flightInQueue = _schedule.FirstOrDefault(checkFlightQueue);
if (flightInQueue != null && flightInQueue.Number == flight.Number)
return true;
}
return false;
}
}
public class Flight
{
public string Number { get; set; }
public DateTime DepartureTime { get; set; }
public DateTime ArrivalTime { get; set; }
public int TotalCrew { get; set; }
}
public static class DateTimeExtension
{
public static bool IsConflictWithFlightTime(this DateTime currentFlightTime, DateTime nextFlightTime)
{
TimeSpan timeDiff= nextFlightTime - currentFlightTime;
double totalMinutes = timeDiff.TotalMinutes;
if (totalMinutes < 0)
totalMinutes = totalMinutes * -1;
totalMinutes = Math.Round(totalMinutes);
if (totalMinutes <= 30.0D)
return true;
else
return false;
}
}
EDIT
My implementation of the ConductFlightOperations
. I'm not sure if this is proper way of doing it.
public void ConductFlightOperations()
{
// Flight 003 asking for landing
var flightAskingForLanding = _schedule.FirstOrDefault(x => x.Number == "FL003");
if (flightAskingForLanding != null)
{
Console.WriteLine(string.Format("Flight {0} \t: Okay to land?", flightAskingForLanding.Number));
LandingClearance(flightAskingForLanding);
flightAskingForLanding.Land();
}
}
My Implementation of the Deconflict
. Right now it only check for someFlight.ArrivalTime
(which is for Landing flights), what should be proper way to reuse same code to check for someFlight.DepartureTime
(for Departing flights)? Should I pass some flag or create new method or something else?
protected bool Deconflict(Flight someFlight)
{
if (_schedule.Any(x => x.Number != someFlight.Number && Math.Abs((x.ArrivalTime - someFlight.ArrivalTime).TotalMinutes) < 20D))
return false;
if (_schedule.Any(x => x.Number != someFlight.Number && Math.Abs((x.DepartureTime - someFlight.ArrivalTime).TotalMinutes) < 20D))
return false;
return true;
}
Please advice for these 2 methods [1]: http://stackoverflow.com/questions/19405571/failed-to-understand-the-use-of-delegates-in-real-world-scenarios
I borrowed the code from this question. I'm trying to implement the delegate in that code. Below is my outcome so far.
class Program
{
static void Main(string[] args)
{
var flights = new List<Flight>
{
new Flight {Number = "FL001", DepartureTime = DateTime.Now},
new Flight {Number = "FL002", DepartureTime = DateTime.Now.AddHours(1)},
new Flight {Number = "FL003", ArrivalTime = DateTime.Now.AddMinutes(30)},
new Flight {Number = "FL004", ArrivalTime = DateTime.Now.AddHours(1.30)},
};
var tower = new FlightTower(flights);
// Flight 003 asking for landing
var flightAskingForLanding= flights.FirstOrDefault(x => x.Number == "FL003");
if (flightAskingForLanding != null)
{
Console.WriteLine(string.Format("Flight {0} \t: Okay to land?", flightAskingForLanding.Number));
Console.WriteLine(string.Format("Flight Tower \t: {0}", tower.CanLand(flightAskingForLanding)));
}
Func<Flight, bool> checkSchedule = x => flightAskingForLanding.ArrivalTime.IsConflictWithFlightTime(x.ArrivalTime) && flightAskingForLanding.Number != x.Number;
checkSchedule += x => flightAskingForLanding.ArrivalTime.IsConflictWithFlightTime(x.DepartureTime) && flightAskingForLanding.Number != x.Number;
if (flightAskingForLanding != null)
{
Console.WriteLine(string.Format("Flight {0} \t: Okay to land?", flightAskingForLanding.Number));
Console.WriteLine(string.Format("Flight Tower \t: {0}", tower.CheckRunwayStatus(flightAskingForLanding,
checkSchedule,
x => x.ArrivalTime == flights.Min(f => f.ArrivalTime))));
}
Console.ReadKey();
}
}
public class FlightTower
{
private readonly List<Flight> _schedule;
public FlightTower(List<Flight> schedule)
{
_schedule = schedule;
}
//this method is to show my initial implementation
public bool CanTakeOff(Flight flight)
{
//todo: add code to check for both arrival time and departure time as in the CanLand method
var arrivingFlights = _schedule.Where(x => x.ArrivalTime == DateTime.Now);
if (arrivingFlights.ToList().Count == 0)
{
var flightInQueue = _schedule.FirstOrDefault(x => x.DepartureTime == _schedule.Min(c => c.DepartureTime));
if (flightInQueue != null && flightInQueue.Number == flight.Number)
{
return true;
}
}
return false;
}
//this method is to show my initial implementation
public bool CanLand(Flight flight)
{
var arrvingFlight = _schedule.Where(x => flight.ArrivalTime.IsConflictWithFlightTime(x.ArrivalTime) && flight.Number != x.Number);
var depatingFlight = _schedule.Where(x => flight.ArrivalTime.IsConflictWithFlightTime(x.DepartureTime) && flight.Number != x.Number);
if (arrvingFlight.ToList().Count == 0 && depatingFlight.ToList().Count == 0)
{
var flightInQueue = _schedule.FirstOrDefault(x => x.ArrivalTime == _schedule.Min(c => c.ArrivalTime));
if (flightInQueue != null && flightInQueue.Number == flight.Number)
return true;
}
return false;
}
//this will replace above CanLand and CanTakeOff methods
public bool CheckRunwayStatus(Flight flight, Func<Flight, bool> checkSchedule, Func<Flight, bool> checkFlightQueue)
{
var flightStatus = _schedule.Where(checkSchedule);
if (flightStatus.ToList().Count == 0 )
{
var flightInQueue = _schedule.FirstOrDefault(checkFlightQueue);
if (flightInQueue != null && flightInQueue.Number == flight.Number)
return true;
}
return false;
}
}
public class Flight
{
public string Number { get; set; }
public DateTime DepartureTime { get; set; }
public DateTime ArrivalTime { get; set; }
public int TotalCrew { get; set; }
}
public static class DateTimeExtension
{
public static bool IsConflictWithFlightTime(this DateTime currentFlightTime, DateTime nextFlightTime)
{
TimeSpan timeDiff= nextFlightTime - currentFlightTime;
double totalMinutes = timeDiff.TotalMinutes;
if (totalMinutes < 0)
totalMinutes = totalMinutes * -1;
totalMinutes = Math.Round(totalMinutes);
if (totalMinutes <= 30.0D)
return true;
else
return false;
}
}
I borrowed the code from this [question][1]. I'm trying to implement the delegate in that code. Below is my outcome so far.
class Program
{
static void Main(string[] args)
{
var flights = new List<Flight>
{
new Flight {Number = "FL001", DepartureTime = DateTime.Now},
new Flight {Number = "FL002", DepartureTime = DateTime.Now.AddHours(1)},
new Flight {Number = "FL003", ArrivalTime = DateTime.Now.AddMinutes(30)},
new Flight {Number = "FL004", ArrivalTime = DateTime.Now.AddHours(1.30)},
};
var tower = new FlightTower(flights);
// Flight 003 asking for landing
var flightAskingForLanding= flights.FirstOrDefault(x => x.Number == "FL003");
if (flightAskingForLanding != null)
{
Console.WriteLine(string.Format("Flight {0} \t: Okay to land?", flightAskingForLanding.Number));
Console.WriteLine(string.Format("Flight Tower \t: {0}", tower.CanLand(flightAskingForLanding)));
}
Func<Flight, bool> checkSchedule = x => flightAskingForLanding.ArrivalTime.IsConflictWithFlightTime(x.ArrivalTime) && flightAskingForLanding.Number != x.Number;
checkSchedule += x => flightAskingForLanding.ArrivalTime.IsConflictWithFlightTime(x.DepartureTime) && flightAskingForLanding.Number != x.Number;
if (flightAskingForLanding != null)
{
Console.WriteLine(string.Format("Flight {0} \t: Okay to land?", flightAskingForLanding.Number));
Console.WriteLine(string.Format("Flight Tower \t: {0}", tower.CheckRunwayStatus(flightAskingForLanding,
checkSchedule,
x => x.ArrivalTime == flights.Min(f => f.ArrivalTime))));
}
Console.ReadKey();
}
}
public class FlightTower
{
private readonly List<Flight> _schedule;
public FlightTower(List<Flight> schedule)
{
_schedule = schedule;
}
//this method is to show my initial implementation
public bool CanTakeOff(Flight flight)
{
//todo: add code to check for both arrival time and departure time as in the CanLand method
var arrivingFlights = _schedule.Where(x => x.ArrivalTime == DateTime.Now);
if (arrivingFlights.ToList().Count == 0)
{
var flightInQueue = _schedule.FirstOrDefault(x => x.DepartureTime == _schedule.Min(c => c.DepartureTime));
if (flightInQueue != null && flightInQueue.Number == flight.Number)
{
return true;
}
}
return false;
}
//this method is to show my initial implementation
public bool CanLand(Flight flight)
{
var arrvingFlight = _schedule.Where(x => flight.ArrivalTime.IsConflictWithFlightTime(x.ArrivalTime) && flight.Number != x.Number);
var depatingFlight = _schedule.Where(x => flight.ArrivalTime.IsConflictWithFlightTime(x.DepartureTime) && flight.Number != x.Number);
if (arrvingFlight.ToList().Count == 0 && depatingFlight.ToList().Count == 0)
{
var flightInQueue = _schedule.FirstOrDefault(x => x.ArrivalTime == _schedule.Min(c => c.ArrivalTime));
if (flightInQueue != null && flightInQueue.Number == flight.Number)
return true;
}
return false;
}
//this will replace above CanLand and CanTakeOff methods
public bool CheckRunwayStatus(Flight flight, Func<Flight, bool> checkSchedule, Func<Flight, bool> checkFlightQueue)
{
var flightStatus = _schedule.Where(checkSchedule);
if (flightStatus.ToList().Count == 0 )
{
var flightInQueue = _schedule.FirstOrDefault(checkFlightQueue);
if (flightInQueue != null && flightInQueue.Number == flight.Number)
return true;
}
return false;
}
}
public class Flight
{
public string Number { get; set; }
public DateTime DepartureTime { get; set; }
public DateTime ArrivalTime { get; set; }
public int TotalCrew { get; set; }
}
public static class DateTimeExtension
{
public static bool IsConflictWithFlightTime(this DateTime currentFlightTime, DateTime nextFlightTime)
{
TimeSpan timeDiff= nextFlightTime - currentFlightTime;
double totalMinutes = timeDiff.TotalMinutes;
if (totalMinutes < 0)
totalMinutes = totalMinutes * -1;
totalMinutes = Math.Round(totalMinutes);
if (totalMinutes <= 30.0D)
return true;
else
return false;
}
}
EDIT
My implementation of the ConductFlightOperations
. I'm not sure if this is proper way of doing it.
public void ConductFlightOperations()
{
// Flight 003 asking for landing
var flightAskingForLanding = _schedule.FirstOrDefault(x => x.Number == "FL003");
if (flightAskingForLanding != null)
{
Console.WriteLine(string.Format("Flight {0} \t: Okay to land?", flightAskingForLanding.Number));
LandingClearance(flightAskingForLanding);
flightAskingForLanding.Land();
}
}
My Implementation of the Deconflict
. Right now it only check for someFlight.ArrivalTime
(which is for Landing flights), what should be proper way to reuse same code to check for someFlight.DepartureTime
(for Departing flights)? Should I pass some flag or create new method or something else?
protected bool Deconflict(Flight someFlight)
{
if (_schedule.Any(x => x.Number != someFlight.Number && Math.Abs((x.ArrivalTime - someFlight.ArrivalTime).TotalMinutes) < 20D))
return false;
if (_schedule.Any(x => x.Number != someFlight.Number && Math.Abs((x.DepartureTime - someFlight.ArrivalTime).TotalMinutes) < 20D))
return false;
return true;
}
Please advice for these 2 methods [1]: http://stackoverflow.com/questions/19405571/failed-to-understand-the-use-of-delegates-in-real-world-scenarios