private static void setSales() { //Connect to Database using (DatabaseClassDataContext database = new DatabaseClassDataContext()) { //Fetch Flights List <Flight> flightsFetched = database.Flights.ToList(); //Fetch Airlines List <Airline_Company> airlines = database.Airline_Companies.ToList(); //Loop through flights and randomly pick an airline to associate a airline_sale with that flight and airline. foreach (Flight f in flightsFetched) { Airline_Sale sale = new Airline_Sale { Name = airlines[_random.Next(0, airlines.Count)].company_name, Flight_id = f.Flight_id }; if (database.Airline_Sales.Where(x => x.Flight_id == f.Flight_id).ToList().Count == 0) { database.Airline_Sales.InsertOnSubmit(sale); database.SubmitChanges(); } } } }
private static void setSeats() { //Start Connection to Database using (DatabaseClassDataContext database = new DatabaseClassDataContext()) { //Fetch List of Flights List <Flight> flightsFetched = database.Flights.ToList(); int count = 0; foreach (Flight f in flightsFetched) { //Loop and generate five seats per flights for (int i = 0; i < 5; i++) { Seat s = new Seat() { seat_id = count.ToString(), flight_id = f.Flight_id, end_auction_date = DateTime.Now, }; database.Seats.InsertOnSubmit(s); database.SubmitChanges(); count++; } } } }
//Used to insert flgiths and generate airports associated with that major city (since there can exist two of them per city) public static void InsertFlightIntoDatabase(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); } PersonalPopuplation.Airport departureAirport = database.Airports.Where(x => x.cityName.Equals(c.name)).ToList()[fetch]; PersonalPopuplation.Airport arrivalAirport = database.Airports.Where(x => x.cityName.Equals(c2.name)).ToList()[fetch2]; string deptAirportName = departureAirport.Name; string arrivalAirportName = arrivalAirport.Name; //Boolean Check to ensure that no two flights are saved with the same city, this was to prevent //Overpopulating the database //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; //} //Generates Flight Number for Flight's ID string flightid = generateFlightNumber(); //Loops until a unique one is generated while (database.Flights.SingleOrDefault(x => x.Flight_id.Equals(flightid)) != null) { flightid = generateFlightNumber(); Console.WriteLine("Generating New Unique Flight Number"); } //Randomly generate a departure time DateTime deptTime = DateTime.Now.AddMinutes(30 * _random.Next(0, 4)); PersonalPopuplation.Flight flight = new PersonalPopuplation.Flight() { Flight_id = flightid, airport_arrival_name = arrivalAirportName, airport_departure_name = deptAirportName, departure_time = deptTime, arrival_time = deptTime.AddMinutes(((double)distance / (double)926) * _random.Next(600, 1000)), distance = distance }; database.Flights.InsertOnSubmit(flight); database.SubmitChanges(); }
//Used for taking read data from files and inserting airlines into Airline_Company Table public async static Task InsertAirlineInDatabase(string cityName, string airline) { await Task.Factory.StartNew(() => { using (DatabaseClassDataContext database = new DatabaseClassDataContext()) { PersonalPopuplation.Airline_Company companyToAdd = new PersonalPopuplation.Airline_Company() { cityName = cityName, company_name = airline }; PersonalPopuplation.Airline_Company check = database.Airline_Companies.SingleOrDefault(x => x.company_name.Equals(airline)); if (check == null) { database.Airline_Companies.InsertOnSubmit(companyToAdd); database.SubmitChanges(); } } }); }
//Used for taking read data from files and inserting major cities into MAJOR_CITY table public async static Task InsertMajorCityInDatabase(string cityName, string countryName) { await Task.Factory.StartNew(() => { using (DatabaseClassDataContext database = new DatabaseClassDataContext()) { PersonalPopuplation.Major_City city = new PersonalPopuplation.Major_City() { name = cityName, country_name = countryName }; PersonalPopuplation.Major_City check = database.Major_Cities.SingleOrDefault(x => x.name.Equals(cityName)); if (check == null) { database.Major_Cities.InsertOnSubmit(city); database.SubmitChanges(); } } }); }
//Used for taking read data from files and inserting airports into airport table public async static Task InsertAirportInDatabase(string setCityName, string airportName) { await Task.Factory.StartNew(() => { using (DatabaseClassDataContext database = new DatabaseClassDataContext()) { PersonalPopuplation.Airport Airport = new PersonalPopuplation.Airport() { cityName = setCityName, Name = airportName }; PersonalPopuplation.Airport check = database.Airports.SingleOrDefault(x => x.Name.Equals(airportName)); if (check == null) { database.Airports.InsertOnSubmit(Airport); database.SubmitChanges(); } } }); }
public static void setPaths() { //Get Flights using (DatabaseClassDataContext database = new DatabaseClassDataContext()) { //Initial Association of Paths with Flights List <Flight> flightsFetched = database.Flights.ToList(); // List<Path> pathsCreated = new List<Path>(); foreach (Flight f in flightsFetched) { Path path = new Path() { airport_name = f.airport_departure_name, airport2_name = f.airport_arrival_name, distance = f.distance, flight_id = f.Flight_id, }; Console.WriteLine(path.path_no); database.Paths.InsertOnSubmit(path); database.SubmitChanges(); } //Generate a few flights with more than one path. List <Airport> canadianAirports = database.Airports.Where(x => x.Major_City.country_name == "Canada").ToList(); List <Airport> usAirports = database.Airports.Where(x => x.Major_City.country_name == "US").ToList(); List <Airport> mexicoAirports = database.Airports.Where(x => x.Major_City.country_name == "Mexico").ToList(); //US -Canada Flight Generation for (int i = 0; i < 5; i++) { //Generate Flight with Random Departure from Canada and Random Arrival to the U.S //Distance is randomly generated within an reasonable range Flight flight1 = generateFlight(database, canadianAirports[_random.Next(0, canadianAirports.Count)], usAirports[_random.Next(0, usAirports.Count)], _random.Next(2000, 5000)); database.Flights.InsertOnSubmit(flight1); //Generate the Paths to save flight information Path p1 = new Path() { airport2_name = usAirports[_random.Next(0, usAirports.Count)].Name, airport_name = flight1.airport_departure_name, distance = flight1.distance - _random.Next(100, flight1.distance / 3), flight_id = flight1.Flight_id, }; Path p2 = new Path() { airport2_name = flight1.airport_arrival_name, airport_name = p1.airport2_name, distance = flight1.distance - p1.distance, flight_id = flight1.Flight_id, }; //Insert into canada database.Paths.InsertOnSubmit(p1); database.Paths.InsertOnSubmit(p2); } //Mexico Canada Flight Generation for (int i = 0; i < 2; i++) { Flight flight1 = generateFlight(database, canadianAirports[_random.Next(0, canadianAirports.Count)], mexicoAirports[_random.Next(0, mexicoAirports.Count)], _random.Next(4000, 6000)); database.Flights.InsertOnSubmit(flight1); Path p1 = new Path() { airport2_name = canadianAirports[_random.Next(0, canadianAirports.Count)].Name, airport_name = flight1.airport_departure_name, distance = flight1.distance - _random.Next(100, flight1.distance / 3), flight_id = flight1.Flight_id, }; Path p2 = new Path() { airport2_name = flight1.airport_arrival_name, airport_name = p1.airport2_name, distance = flight1.distance - p1.distance, flight_id = flight1.Flight_id, }; database.Paths.InsertOnSubmit(p1); database.Paths.InsertOnSubmit(p2); } //Mexico-US Flight Generation for (int i = 0; i < 3; i++) { Flight flight1 = generateFlight(database, usAirports[_random.Next(0, usAirports.Count)], usAirports[_random.Next(0, usAirports.Count)], _random.Next(3000, 4000)); database.Flights.InsertOnSubmit(flight1); Path p1 = new Path() { airport2_name = usAirports[_random.Next(0, usAirports.Count)].Name, airport_name = flight1.airport_departure_name, distance = flight1.distance - _random.Next(100, flight1.distance / 3), flight_id = flight1.Flight_id, }; Path p2 = new Path() { airport2_name = flight1.airport_arrival_name, airport_name = p1.airport2_name, distance = flight1.distance - p1.distance, flight_id = flight1.Flight_id, }; database.Paths.InsertOnSubmit(p1); database.Paths.InsertOnSubmit(p2); } database.SubmitChanges(); } }