-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomDataGenerator.cs
275 lines (243 loc) · 10.6 KB
/
RandomDataGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using FlyCenterProject;
using System.IO;
using System.Data.SqlClient;
using Newtonsoft.Json;
using static DB_Generator.ApiModel;
using System.Data;
namespace DB_Generator
{
internal class RandomDataGenerator
{
private HttpClient client = new HttpClient();
private string url = "https://randomuser.me/api";
public static List<string> countryList;
private List<string> AddCountriesToList;
private Random _random;
private int _randomNum;
private static Customer[] customersArray;
private static string[] countriesToAddArray;
private static List<string> airlinesList;
public static string[] airlineCompaniesArray;
private string _charasters = "abcdefghijklmnoprst1234567890";
public string Message { get; set; }
public int ProgressValue { get; set; }
public int Total { get; set; }
private CountryDAOMSSQL _countryDAO;
private CustomerDAOMSSQL _customerDAO;
private AirlineDAOMSSQL _airlineDAO;
private FlightDAOMSSQL _flightDAO;
private TicketDAOMSSQL _ticketDAO;
private List<Country> _countries;
private List<AirlineCompany> _airlineCompanies;
private List<Flight> _flights;
private List<Ticket> _tickets;
private List<Customer> _customers;
public RandomDataGenerator()
{
_random = new Random();
_countryDAO = new CountryDAOMSSQL();
_customerDAO = new CustomerDAOMSSQL();
_airlineDAO = new AirlineDAOMSSQL();
_flightDAO = new FlightDAOMSSQL();
_ticketDAO = new TicketDAOMSSQL();
_countries = new List<Country>();
_customers = new List<Customer>();
_airlineCompanies = new List<AirlineCompany>();
_flights = new List<Flight>();
_tickets = new List<Ticket>();
}
internal void AddCustomerToDB(int number)
{
int counter = 0;
try
{
List<Customer> customerFromDB = _customerDAO.GetAll().ToList();
HttpResponseMessage httpResponse = client.GetAsync(url + "?results=" + number).Result;
for (int i = 0; i < number; i++)
{
// HttpResponseMessage httpResponse = client.GetAsync(url + "?results=" + number).Result;
if (httpResponse.IsSuccessStatusCode)
{
Customer c = new Customer();
var content = httpResponse.Content.ReadAsStringAsync().Result;
APIUser r = JsonConvert.DeserializeObject<APIUser>(content);
if(!customerFromDB.Any(customer => customer.UserName == r.results[i].login.username))
c.FirstName = r.results[i].name.first;
c.LastName = r.results[i].name.last;
c.Password = r.results[i].login.password;
c.UserName = r.results[i].login.username;
c.PhoneNo = r.results[i].phone;
c.Address = r.results[i].location.city;
c.CreditCardNumber = r.results[i].cell;
_customerDAO.Add(c);
_customers.Add(c);
counter++;
ProgressValue += Convert.ToInt32(1.0 / Total * 100);
}
}
Message += $"Created Customers {counter}/{number}\n";
}
catch(Exception e)
{
Message += $"{e.ToString()}\n";
}
}
internal void AddCountriesFromApi(int number)
{
int counter = 0;
try
{
_countries = _countryDAO.GetAll().ToList();
HttpResponseMessage httpResponse = client.GetAsync(url + "?results=" + number).Result;
for (int i = 0; i < number; i++)
{
if(httpResponse.IsSuccessStatusCode)
{
Country c = new Country();
var content = httpResponse.Content.ReadAsStringAsync().Result;
APIUser aPIUser = JsonConvert.DeserializeObject<APIUser>(content);
if(!_countries.Any(country => country.CountryName == aPIUser.results[i].location.city))
{
c.CountryName = aPIUser.results[i].location.city;
_countryDAO.Add(c);
counter++;
}
ProgressValue += Convert.ToInt32(1.0 / Total * 100);
}
_countries = _countryDAO.GetAll().ToList();
Message = $"Created Countries {counter}/ {number}\n";
}
}
catch(Exception e)
{
Message += $"{e.ToString()}\n";
}
}
internal void AddRandomAirlines( int number)
{
int counter = 0;
try
{
long LastId = _airlineDAO.GetAll().Any() ? _airlineDAO.GetAll().ToList().Last().ID : 1;
for (int i = 0; i < number; i++)
{
AirlineCompany airline = new AirlineCompany();
_randomNum = _random.Next(1, 6);
StringBuilder stringBuilder = new StringBuilder(_randomNum);
for (int j = 0; j < _randomNum; j++)
{
stringBuilder.Append(_charasters[_random.Next(_charasters.Length)]);
}
airline.AirlineName = stringBuilder.ToString() + LastId.ToString();
_randomNum = _random.Next(1, 6);
for (int j = 0; j < _randomNum; j++)
{
stringBuilder.Append(_charasters[_random.Next(_charasters.Length)]);
}
airline.UserName = stringBuilder.ToString();
airline.Password = stringBuilder.ToString();
_randomNum = _random.Next(0, _countries.Count);
airline.CountryCode = _countries[_randomNum].Id;
_airlineDAO.Add(airline);
_airlineCompanies.Add(airline);
counter++;
LastId++;
ProgressValue += Convert.ToInt32(1.0 / Total * 100);
}
Message += $"Created AirlineCompanies {counter}/{number}\n";
}
catch(Exception e)
{
Message += $"{e.ToString()}\n";
}
}
internal void AddRandomFlights(int number)
{
int counter = 0;
try
{
_flights.AddRange(_flightDAO.GetAll().ToList());
for (int i = 0; i < _airlineCompanies.Count; i++)
{
AirlineCompany c = _airlineDAO.GetAirLineByUserName(_airlineCompanies[i].UserName);
Flight f = new Flight();
f.Departure_Time = AddRandomDate();
f.Landing_Time = AddRandomDate();
_randomNum = _random.Next(100,350);
f.Remaining_Tickets = _randomNum;
_randomNum = _random.Next(0, _countries.Count);
f.Destination_Country_Code = _countries[_randomNum].Id;
_randomNum = _random.Next(0, _countries.Count);
f.Origin_Country_Code = _countries[_randomNum].Id;
f.AirlineCompany_Id = c.ID;
_flightDAO.Add(f);
_flights.Add(f);
counter++;
ProgressValue += Convert.ToInt32(1.0 / Total * 100);
Message += $"Created Flights {counter}/{number} per airline company\n";
}
}
catch (Exception e)
{
Message += $"{e.ToString()}\n";
}
}
internal void AddRandomTickets(int number)
{
int counter = 0;
try
{
for (int i = 0; i < _customers.Count; i++)
{
Customer c = _customerDAO.GetCustomerByUserName(_customers[i].UserName);
for (int j = 0; j < number; j++)
{
Ticket t = new Ticket();
t.CustomerID = c.Id;
_randomNum = _random.Next(0, _flights.Count);
t.FlightID = _flights[_randomNum].ID;
if(! _tickets.Any(ticket => ticket.FlightID == t.FlightID && ticket.CustomerID == t.CustomerID))
{
_ticketDAO.Add(t);
_tickets.Add(t);
counter++;
}
}
ProgressValue += Convert.ToInt32(1.0 / Total * 100);
}
Message += $"Created Airline Tickets {counter}/ {number} per customer\n";
}
catch(Exception e)
{
Message += $"{e.ToString()}\n";
}
}
public class RootObject
{
public int ID { get; set; }
public string First_Name { get; set; }
public string Last_Name { get; set; }
public string User_Name { get; set; }
public string Password { get; set; }
public string Address { get; set; }
public int Phono_Number { get; set; }
public int Credit_Card_Number { get; set; }
}
private DateTime AddRandomDate()
{
return DateTime.Now.AddDays(_random.Next(1000));
}
internal void DeleteAll()
{
Message += "Delete the DataBase";
DeleteDataDAO deleteDataDAO = new DeleteDataDAO();
deleteDataDAO.CleanAllDatabase();
}
}
}