private void btUpdate_Click(object sender, EventArgs e) { if (selectedAirplane != null) { selectedAirplane.ChangeFuel(Convert.ToInt32(nUDChangeFuel.Value)); selectedAirplane.ChangeSpeed(Convert.ToInt32(nUDChangeSpeed.Value)); string destination = cbChangeDestination.Text; if (!destination.ToUpper().Equals(selectedAirplane.Flight.DestinationAirport.Name.ToUpper())) { bool found = false; foreach (Airport a in airNetwork.Airports) { if (a.Name.ToUpper() == destination.ToUpper()) { selectedAirplane.ChangeRoute(selectedAirplane.CurrentLocation, a); found = true; } } if (!found) { MessageBox.Show("No such airport was found. Please enter another one"); } } } }
public void RerouteAirplaneToNearestAirport(Airplane airplaneToReroute) { PointF currentLoc = airplaneToReroute.CurrentLocation; double minTotalDistance = 100000000000000; Airport closestAirport = null; foreach (Airport a in Airports) { //check only if the airport is not the one from the current destination and if there are no problems in the airport if (!(a.Name.ToUpper().Equals(airplaneToReroute.DestinationAirport.Name.ToUpper())) && a.Problems.Count == 0) { double totaldistance = Math.Sqrt(Math.Pow(a.Location.X - currentLoc.X, 2) + Math.Pow(a.Location.Y - currentLoc.Y, 2)); if (totaldistance < minTotalDistance) { minTotalDistance = totaldistance; closestAirport = a; } } } airplaneToReroute.ChangeRoute(currentLoc, closestAirport); }