Random rand = new Random(); //Used to randomize numbers /// <summary> /// CreateFlights method can be called by an outside program to create flights for all known airports. /// Values needed are hard coded into this and other methods in the FlightFactory class. /// Possible future enhancements could be to accept the values needed as parameters allowing for more flexibility. /// </summary> public void CreateFlights() { //Create flights from June to July 2017 int startFlightNumber = 100; //Initial flight number DateTime startDate = new DateTime(2017, 06, 01); //Date to start the flights int days = DateTime.DaysInMonth(2017, 06); //Get the days in the month of June 2017 DateTime endDate = startDate.AddDays(days); //Add the days in June to the start date to get the end date. List <Airport> Airports = Airport.GetAirports(); //Create a list to hold the Airports and get the list from the Airport class. //Loop through for each day, each airport, each scheudled time and create flights to all known airports. for (DateTime currentDate = startDate; currentDate <= endDate; currentDate = currentDate.AddDays(1)) { //Create a variable to hold the Initial flight number. Needed so flight number is the same each day. int flightNumber = startFlightNumber; //Loop through each airportin the Airports list and create an airport object which will hold the source airport. foreach (Airport airp in Airports) { //Create a flight schedule and assign it to the temporary airport object airp.FlightSchedule = CreateFlightSchedule(); //loop through each flight time in the fligth schedule foreach (TimeSpan tme in airp.FlightSchedule) { //Loop through each airportin the Airports list and create an airport object which will hold the destination airport. foreach (Airport arp2 in Airports) { string test1 = airp.Location; //Get the source airport name string test2 = arp2.Location; //Get the Destination airport name //Create a flight to each airport. if (test1 != test2) //do not create flight for same airport { //Get the price for the flight using the source(test1), destination(test2) airports and the time of day(tme) FareGenerator fareGenerator = new FareGenerator(test1, test2, tme, rand); //Create a flight object Flight temp = new Flight(flightNumber, currentDate + tme, test1, test2, fareGenerator.Fare, 10); //Add the flight to the List Flights.Add(temp); //Increment the flight number //(Alternatively flight number could have been created as a randomized list) flightNumber++; } } } } } //Sort the flights by date and time. List <Flight> SortedList = Flights.OrderBy(o => o.DateAndTime.DayOfYear).ThenBy(o => o.SourceAirport).ThenBy(o => o.DestAirport).ToList(); //Assign the sorted to Flights Flights = SortedList; }
public static List <Airport> Airports() { return(Airport.GetAirports()); }