-
Notifications
You must be signed in to change notification settings - Fork 0
/
PricesDatabase.cs
242 lines (236 loc) · 11.1 KB
/
PricesDatabase.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
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace Project_Airline_info
{
interface IPriceable
{
void AddPrices();
void EditPrices();
void ShowPricesForFlight();
}
class PricesDatabase : IPriceable
{
SqlConnection sqlConnectionPrice;
internal PricesDatabase()
{
sqlConnectionPrice = new SqlConnection(ConfigurationManager.ConnectionStrings["AirportConnection"].ConnectionString);
ConsoleKeyInfo KeyReader;
do
{
Console.WriteLine("1.Add prices for flight\n2.Edit prices\n3.Show prices for flight\n4.Buy Ticket");
KeyReader = Console.ReadKey();
Console.WriteLine();
switch (KeyReader.KeyChar)
{
case '1':
AddPrices();
break;
case '2':
EditPrices();
break;
case '3':
ShowPricesForFlight();
break;
case '4':
BuyTicket();
break;
default:
break;
}
Console.WriteLine("If you want to exit press escape otherwise press anykey");
KeyReader = Console.ReadKey();
Console.WriteLine();
}
while (KeyReader.Key != ConsoleKey.Escape);
}
public void AddPrices()
{
Console.Write("Enter Flight=");
string tempNameOfFlight = Console.ReadLine();
sqlConnectionPrice.Open();
SqlCommand CheckFlightNumberInPrices = new SqlCommand(@"SELECT FlightNumber, ID,
City_of_arrival, City_of_departure FROM Prices WHERE ID LIKE @NameOfFLight
OR FlightNumber LIKE @NameOfFlight", sqlConnectionPrice);
CheckFlightNumberInPrices.Parameters.AddWithValue("@NameOfFlight", tempNameOfFlight);
SqlDataReader GetDataForFlightInPrices = CheckFlightNumberInPrices.ExecuteReader();
bool isPriceHasFlight = GetDataForFlightInPrices.HasRows;
GetDataForFlightInPrices.Close();
SqlCommand CheckFlightNumberInFlights = new SqlCommand(@"SELECT Flight_Number,ID,
City_of_arrival,City_of_departure,Arrival,Departure FROM Flights WHERE ID LIKE @NameOfFLight
OR Flight_Number LIKE @NameOfFlight", sqlConnectionPrice);
CheckFlightNumberInFlights.Parameters.AddWithValue("@NameOfFlight", tempNameOfFlight);
SqlDataReader GetDataForFlightInFlights = CheckFlightNumberInFlights.ExecuteReader();
SqlCommand WritePriceData = new SqlCommand(@"INSERT INTO Prices(City_of_departure,
City_of_arrival,FlightNumber,Price_For_Econom,Price_For_Business,Price_For_First,Arrival,Departure,ID)
VALUES (@City_of_departure,@City_of_arrival,@FlightNumber,@Price_For_Econom,@Price_For_Business,
@Price_For_First,@Arrival,@Departure,@ID)", sqlConnectionPrice);
#region Write data to prices table according to if statement
if (GetDataForFlightInFlights.HasRows && !isPriceHasFlight)
{
while (GetDataForFlightInFlights.Read())
{
WritePriceData.Parameters.AddWithValue("@FlightNumber",
GetDataForFlightInFlights.GetValue(0).ToString());
WritePriceData.Parameters.AddWithValue("@ID",
(int)GetDataForFlightInFlights.GetValue(1));
WritePriceData.Parameters.AddWithValue("@City_of_arrival",
GetDataForFlightInFlights.GetValue(2).ToString());
WritePriceData.Parameters.AddWithValue("@City_of_departure",
GetDataForFlightInFlights.GetValue(3).ToString());
WritePriceData.Parameters.AddWithValue("@Arrival",
GetDataForFlightInFlights.GetValue(4).ToString());
WritePriceData.Parameters.AddWithValue("@Departure",
GetDataForFlightInFlights.GetValue(5).ToString());
}
GetDataForFlightInFlights.Close();
SqlCommand ReadColumnsName = new SqlCommand();
ReadColumnsName.CommandText = "SELECT * FROM Prices";
ReadColumnsName.Connection = sqlConnectionPrice;
SqlDataReader GetName = ReadColumnsName.ExecuteReader();
List<string> Columnnames = Enumerable.Range(0, GetName.FieldCount).
Select(GetName.GetName).ToList();
for (int i = 3; i < Columnnames.Count - 3; i++)
{
Console.Write(Columnnames[i] + "=");
WritePriceData.Parameters.AddWithValue(Columnnames[i], Console.ReadLine());
}
GetName.Close();
WritePriceData.ExecuteNonQuery();
}
else
{
Console.WriteLine("Your flight doesn't exist");
}
#endregion
sqlConnectionPrice.Close();
}
public void EditPrices()
{
string selectedFlight;
sqlConnectionPrice.Open();
SqlCommand CheckFlightNumber = new SqlCommand(@"SELECT City_of_departure,City_of_arrival,
FlightNumber,Price_For_Econom,Price_For_Business,Price_For_First,ID FROM Prices
WHERE ID LIKE @NameOfFLight OR FlightNumber LIKE @NameOfFlight", sqlConnectionPrice);
Console.Write("Enter Flight=");
selectedFlight = Console.ReadLine();
CheckFlightNumber.Parameters.AddWithValue("@NameOfFlight", selectedFlight);
SqlDataReader GetDataForFlight = CheckFlightNumber.ExecuteReader();
SqlCommand WritePriceData = new SqlCommand(@"UPDATE Prices SET
Price_For_Econom=@Price_For_Econom,
Price_For_Business= @Price_For_Business,
Price_For_First=@Price_For_First
WHERE ID LIKE @SelectedID OR FlightNumber LIKE @SelectedID", sqlConnectionPrice);
if (GetDataForFlight.HasRows)
{
GetDataForFlight.Close();
SqlCommand ReadColumnsName = new SqlCommand("SELECT * FROM Prices", sqlConnectionPrice);
SqlDataReader GetName = ReadColumnsName.ExecuteReader();
List<string> Columnnames = Enumerable.Range(0, GetName.FieldCount).Select(GetName.GetName).ToList();
for (int i = 3; i < Columnnames.Count - 1; i++)
{
Console.Write(Columnnames[i] + "=");
WritePriceData.Parameters.AddWithValue(Columnnames[i], Console.ReadLine());
}
GetName.Close();
WritePriceData.Parameters.AddWithValue("@SelectedID", selectedFlight);
try
{
WritePriceData.ExecuteNonQuery();
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
}
else
{
Console.WriteLine("Your flight doesn't exist");
}
sqlConnectionPrice.Close();
}
public void ShowPricesForFlight()
{
sqlConnectionPrice.Open();
SqlCommand ShowFlightByFilter = new SqlCommand(@"SELECT * FROM Prices WHERE ID LIKE @SelectedID OR FlightNumber LIKE @SelectedID", sqlConnectionPrice);
Console.Write("Enter Flight=");
ShowFlightByFilter.Parameters.AddWithValue("@SelectedID", Console.ReadLine());
SqlDataReader ReadFlightFromDataBase = ShowFlightByFilter.ExecuteReader();
StringBuilder writeIntoFile = new StringBuilder();
if (ReadFlightFromDataBase.HasRows)
{
while (ReadFlightFromDataBase.Read())
{
for (int i = 0; i < ReadFlightFromDataBase.FieldCount; i++)
{
writeIntoFile.AppendLine(ReadFlightFromDataBase.GetName(i) +
":" + ReadFlightFromDataBase.GetValue(i));
}
}
Console.WriteLine(writeIntoFile);
File.WriteAllText("PriceByFilter.txt", writeIntoFile.ToString());
ReadFlightFromDataBase.Close();
}
else
{
Console.WriteLine("Your flight doesn't exist");
}
sqlConnectionPrice.Close();
}
public void BuyTicket()
{
sqlConnectionPrice.Open();
SqlCommand findPlane = new SqlCommand(@"SELECT * FROM Prices
WHERE City_of_departure LIKE @City_of_departure AND
City_of_arrival LIKE @City_of_arrival OR
Arrival LIKE @Arrival OR
Departure LIKE @Departure", sqlConnectionPrice);
Console.WriteLine("Enter Departure Date");
findPlane.Parameters.AddWithValue("@Departure", PassengerDataBase.AddDateOnly());
Console.WriteLine("Enter Arrival Date=");
findPlane.Parameters.AddWithValue("@Arrival", PassengerDataBase.AddDateOnly());
Console.Write("Enter city of departure=");
findPlane.Parameters.AddWithValue("@City_of_departure", Console.ReadLine());
Console.Write("Enter city of arrival=");
findPlane.Parameters.AddWithValue("@City_of_arrival", Console.ReadLine());
SqlDataReader getplane = findPlane.ExecuteReader();
Console.WriteLine();
#region Get Buyed Plane
if (getplane.HasRows)
{
while (getplane.Read())
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine(getplane.GetName(i) + ":" + getplane.GetValue(i));
}
for (int i = 3; i < 6; i++)
{
Console.WriteLine(getplane.GetName(i) + ":" + getplane.GetValue(i)+"$");
}
for (int i = 6; i < getplane.FieldCount-1; i++)
{
Console.WriteLine(getplane.GetName(i) + ":" + getplane.GetValue(i));
}
Console.WriteLine("_____________________");
}
}
else
{
Console.WriteLine("There is no plane on your date,sorry");
}
#endregion
sqlConnectionPrice.Close();
}
}
}