コード例 #1
0
        /// <summary>
        /// Add generated flight info into database
        /// </summary>
        /// <param name="distance">Distance of Flight generateed</param>
        /// <param name="c">Major City Start</param>
        /// <param name="c2">Major City End</param>
        /// <param name="database">Database Connection Reference</param>
        public static void AddFlight(int distance, MAJOR_CITY c, MAJOR_CITY c2, DatabaseClassDataContext database)
        {
            int fetch  = 0;
            int fetch2 = 0;

            if (database.AIRPORTs.Where(x => x.CityName.Equals(c.Name)).ToList().Count > 1)
            {
                fetch = _random.Next(0, 2);
            }

            if (database.AIRPORTs.Where(x => x.CityName.Equals(c2.Name)).ToList().Count > 1)
            {
                fetch2 = _random.Next(0, 2);
            }
            DataScriptsCPSC471.AIRPORT departureAirport = database.AIRPORTs.Where(x => x.CityName.Equals(c.Name)).ToList()[fetch];


            DataScriptsCPSC471.AIRPORT arrivalAirport = database.AIRPORTs.Where(x => x.CityName.Equals(c2.Name)).ToList()[fetch2];

            string deptAirportName    = departureAirport.Name;
            string arrivalAirportName = arrivalAirport.Name;

            //Debugging Boolean Check to ensure the flight does not already exist
            //if (database.FLIGHTs.Where(x => x.departure_airport.Equals(deptAirportName) && x.arrival_airport.Equals(arrivalAirportName)).ToList().Count> 0 )
            //{
            //    FLIGHT f = database.FLIGHTs.Where(x => x.departure_airport.Equals(deptAirportName) && x.arrival_airport.Equals(arrivalAirportName)).ToList()[0];
            //    Console.WriteLine("Flight Exists!");
            //    Console.WriteLine(f.arrival_time);
            //    Console.WriteLine(f.departure_time);
            //    return;
            //}



            string flightid = GenerateFlightNumber();

            while (database.FLIGHTs.SingleOrDefault(x => x.Flight_id.Equals(flightid)) != null)
            {
                flightid = GenerateFlightNumber();
                Console.WriteLine("Generating New Unique Flight Number");
            }
            DateTime deptTime = DateTime.Now.AddMinutes(30 * _random.Next(0, 4));

            DataScriptsCPSC471.FLIGHT flight = new DataScriptsCPSC471.FLIGHT()
            {
                Flight_id         = flightid,
                arrival_airport   = arrivalAirportName,
                departure_airport = deptAirportName,
                departure_time    = deptTime,
                arrival_time      = deptTime.AddMinutes(((double)distance / (double)926) * _random.Next(600, 1000)),
                distance          = distance,
                base_price        = 600
            };
            database.FLIGHTs.InsertOnSubmit(flight);
            database.SubmitChanges();
        }
コード例 #2
0
 /// <summary>
 /// Insert Airport into Database given read city and airport from file
 /// </summary>
 /// <param name="cityName">City containing Airport</param>
 /// <param name="airportName">Airport Name</param>
 /// <returns></returns>
 public async static Task InsertAirportInDatabase(string cityName, string airportName)
 {
     await Task.Factory.StartNew(() =>
     {
         using (DatabaseClassDataContext database = new DatabaseClassDataContext())
         {
             DataScriptsCPSC471.AIRPORT Airport = new DataScriptsCPSC471.AIRPORT()
             {
                 CityName = cityName,
                 Name     = airportName
             };
             DataScriptsCPSC471.AIRPORT check = database.AIRPORTs.SingleOrDefault(x => x.Name.Equals(airportName));
             if (check == null)
             {
                 database.AIRPORTs.InsertOnSubmit(Airport);
                 database.SubmitChanges();
             }
         }
     });
 }