Esempio n. 1
0
        public List <Flight> GetAllFlights()
        {
            //String sql = "SELECT `flight`.idFlight, `tf`.Name AS takingOff, `ar`.Name AS Arrival, `flight`.EstimatedDuration, `flight`.DepartureTime, `flight`.ArrivalTime, `airplane`.idAirplane, `airplane`.Type, `flight`.NbOfPassengers, `flight`.CargoWeight FROM `flight` JOIN `airplane` ON(`flight`.Airplane_idAirplane=`airplane`.idAirplane) JOIN `airport` AS tf ON(`flight`.takesOffFrom=`tf`.idAirport) JOIN `airport` AS ar ON(`flight`.LandsTo=`ar`.idAirport)";
            //String sql = "SELECT `flight`.idFlight, `tf`.Name AS takingOff, `ar`.Name AS Arrival, `flight`.EstimatedDuration, `flight`.DepartureTime, `flight`.ArrivalTime, `airplane`.idAirplane, `airplane`.Type, `flight`.`Loaded` FROM `flight` JOIN `airplane` ON(`flight`.Airplane_idAirplane=`airplane`.idAirplane) JOIN `airport` AS tf ON(`flight`.takesOffFrom=`tf`.idAirport) JOIN `airport` AS ar ON(`flight`.LandsTo=`ar`.idAirport)";
            String       sql     = "SELECT `flight`.idFlight, `tf`.Name AS takingOff, `ar`.Name AS Arrival, `flight`.EstimatedDuration, `flight`.DepartureTime, `flight`.ArrivalTime, `airplane`.idAirplane, `airplane`.Type, `flight`.`Loaded` FROM `flight` JOIN `airplane` ON(`flight`.Airplane_idAirplane=`airplane`.idAirplane) JOIN `airport` AS tf ON(`flight`.takesOffFrom=`tf`.idAirport) JOIN `airport` AS ar ON(`flight`.LandsTo=`ar`.idAirport) JOIN region ON (tf.Region_idRegion = region.idRegion) WHERE `region`.Name = '" + this.Network.Region.ToString() + "';";
            MySqlCommand command = new MySqlCommand(sql, connection);

            List <Flight> tempFlights = new List <Flight>();


            try
            {
                connection.Open();
                MySqlDataReader reader = command.ExecuteReader();


                String   id;
                Airport  destination, takingOff;
                TimeSpan duration;
                DateTime departureTime, arrivalTime;
                Airplane airplane;
                string   typeAirplane;
                int      loaded;


                while (reader.Read())
                {
                    id          = Convert.ToString(reader["idFlight"]);
                    takingOff   = Network.FindAirport(Convert.ToString(reader["takingOff"]));
                    destination = Network.FindAirport(Convert.ToString(reader["Arrival"]));
                    string[] s = Convert.ToString(reader["EstimatedDuration"]).Split(' ');
                    s = s[1].Split(':');
                    string[] sx = s[2].Split('.');
                    duration = new TimeSpan(Convert.ToInt32(s[0]), Convert.ToInt32(s[1]), Convert.ToInt32(sx[0]));

                    string[] s1  = Convert.ToString(reader["DepartureTime"]).Split(' ');
                    string[] s1a = s1[0].Split('-');
                    string[] s1b = s1[1].Split(':');
                    departureTime = new DateTime(Convert.ToInt32(s1a[0]), Convert.ToInt32(s1a[1]), Convert.ToInt32(s1a[2]), Convert.ToInt32(s1b[0]), Convert.ToInt32(s1b[1]), Convert.ToInt32(s1b[2]));


                    string[] s2  = Convert.ToString(reader["ArrivalTime"]).Split(' ');
                    string[] s2a = s2[0].Split('-');
                    string[] s2b = s2[1].Split(':');

                    arrivalTime = new DateTime(Convert.ToInt32(s2a[0]), Convert.ToInt32(s2a[1]), Convert.ToInt32(s2a[2]), Convert.ToInt32(s2b[0]), Convert.ToInt32(s2b[1]), Convert.ToInt32(s2b[2]));

                    airplane     = Network.FindAirplane(Convert.ToString(reader["idAirplane"]));
                    typeAirplane = Convert.ToString(reader["Type"]);
                    loaded       = Convert.ToInt32(reader["Loaded"]);


                    //Check whether the plane is a passenger or cargo plane and create a flight accordingly
                    if (typeAirplane == "Passenger")
                    {
                        FlightPassenger fp = new FlightPassenger(id, takingOff, destination, duration, departureTime, arrivalTime, loaded);
                        airplane.AddFlight(fp);
                        tempFlights.Add(fp);
                    }
                    else
                    {
                        FlightCargo fc = new FlightCargo(id, takingOff, destination, duration, departureTime, arrivalTime, loaded);
                        airplane.AddFlight(fc);
                        tempFlights.Add(fc);
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error while loading the flights");
            }
            finally
            {
                connection.Close();
            }
            return(tempFlights);
        }
Esempio n. 2
0
        private void btn_create_plane_Click(object sender, EventArgs e)
        {
            created = false;
            //Time Helpers
            TimeSpan estimatedDuration = new TimeSpan(02, 25, 00);
            DateTime currentTime       = GlobalVariables.globalTime;

            //Airplane properties
            int    capacity = Convert.ToInt32(nud_capacity.Value);
            double speed    = Convert.ToDouble(nud_speed.Value);
            double fuel     = Convert.ToDouble(nud_fuel.Value);

            //Find the next ID for the plane
            int id_plane = airNetwork.nextAvailableAirplaneId();

            //Flight properties
            string origin_countryName      = cb_auto_orig.Text;
            string destination_countryName = cb_auto_dest.Text;
            int    loaded = Convert.ToInt32(numericUpDown4.Value);

            //Find the next ID for the flight
            int id_flight = airNetwork.nextAvailableFlightId();

            //Initializing Airport and Airplane objects to be passed
            Airport  origin_airport      = null;
            Airport  destination_airport = null;
            Airplane airplane            = null;
            Flight   flight = null;

            //Bulk properties
            int bulkAmount = Convert.ToInt32(nud_blk_amount.Value);

            origin_airport      = returnAirportObject(origin_countryName);
            destination_airport = returnAirportObject(destination_countryName);

            if (origin_airport != null && destination_airport != null)
            {
                //Check if there are problems in the airports
                if (origin_airport.Problems.Count > 0 || destination_airport.Problems.Count > 0)
                {
                    MessageBox.Show("Airplane was not created since there was a problem in one of the airports.");
                    return;
                }
                //Check if it should be a bulk create
                if (cb_bulkCreate.Checked)
                {
                    if (speed > 0 && fuel > 0)
                    {
                        for (int i = 0; i < bulkAmount; i++)
                        {
                            id_flight++;
                            id_plane++;
                            if (capacity > loaded)
                            {
                                if (speed > 0 && fuel > 0)
                                {
                                    if (rb_cargo.Checked)
                                    {
                                        // Adding a cargo Airplane and Flight
                                        //Creating the plane and flight
                                        flight   = new FlightCargo(id_flight.ToString(), origin_airport, destination_airport, estimatedDuration, currentTime, currentTime + estimatedDuration, loaded);
                                        airplane = new AirplaneCargo(id_plane.ToString(), capacity, speed, fuel, origin_airport.Location);
                                    }
                                    else if (rb_passanger.Checked)
                                    {
                                        //Adding a passanger Airplane and Flight
                                        //Creating the plane and flight
                                        flight   = new FlightPassenger(id_flight.ToString(), origin_airport, destination_airport, estimatedDuration, currentTime, currentTime + estimatedDuration, loaded);
                                        airplane = new AirplanePassenger(id_plane.ToString(), capacity, speed, fuel, origin_airport.Location);
                                    }

                                    if (flight != null && airplane != null)
                                    {
                                        created = true;
                                        //Adding the flight to the network
                                        airNetwork.Airplanes.Add(airplane);
                                        //Adding the plane to the flight
                                        airplane.AddFlight(flight);
                                    }
                                }
                                else //If speed is <=0
                                {
                                    MessageBox.Show("Please make sure both speed and fuel are more than 0");
                                }
                            }
                            else
                            {
                                MessageBox.Show("There is more cargo on the plane than the capacity would allow!");
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please make sure both speed and fuel are more than 0");
                    }
                    if (created)
                    {
                        MessageBox.Show(bulkAmount + " airplanes from " + origin_countryName + " to " + destination_countryName + " were succesffully created!");
                        resetAllBoxes();
                        panelDrawing.Invalidate();
                    }
                }
                else //Proceed with a single airplane creation
                {
                    if (capacity > loaded)
                    {
                        if (speed > 0 && fuel > 0)
                        {
                            if (rb_cargo.Checked)
                            {
                                // Adding a cargo Airplane and Flight
                                //Creating the plane and flight
                                flight   = new FlightCargo(id_flight.ToString(), origin_airport, destination_airport, estimatedDuration, currentTime, currentTime + estimatedDuration, loaded);
                                airplane = new AirplaneCargo(id_plane.ToString(), capacity, speed, fuel, origin_airport.Location);
                            }
                            else if (rb_passanger.Checked)
                            {
                                //Adding a passanger Airplane and Flight
                                //Creating the plane and flight
                                flight   = new FlightPassenger(id_flight.ToString(), origin_airport, destination_airport, estimatedDuration, currentTime, currentTime + estimatedDuration, loaded);
                                airplane = new AirplanePassenger(id_plane.ToString(), capacity, speed, fuel, origin_airport.Location);
                            }

                            if (flight != null && airplane != null)
                            {
                                created = true;
                                //Adding the flight to the network
                                airNetwork.Airplanes.Add(airplane);
                                //Adding the plane to the flight
                                airplane.AddFlight(flight);
                            }
                        }
                        else //If speed is <=0
                        {
                            MessageBox.Show("Please make sure both speed and fuel are more than 0");
                        }
                    }
                    else
                    {
                        MessageBox.Show("There is more cargo on the plane than the capacity would allow!");
                    }
                    if (created)
                    {
                        MessageBox.Show("Airplane from " + origin_countryName + " to " + destination_countryName + " was succesffully created!");
                        resetAllBoxes();
                        panelDrawing.Invalidate();
                    }
                }
            }
            else
            {
                MessageBox.Show("Please select both origin and destination airports");
            }
            autoCompleteAirport(cb_auto_orig);
            autoCompleteAirport(cb_auto_dest);
        }