-
Notifications
You must be signed in to change notification settings - Fork 0
/
roundtrip.py
327 lines (323 loc) · 15.4 KB
/
roundtrip.py
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import main
import search
def roundTrip(email, CONN_STRING):
source = input("Source: ").upper()
dest = input("Destination: ").upper()
dep_date = input("Departure Date (DD/MM/YYYY): ")
ret_date = input("Return Date (DD/MM/YYYY): ")
printRoundInfo(email, CONN_STRING, source, dest, dep_date, ret_date)
def printRoundInfo(email, CONN_STRING, source, dest, dep_date, ret_date, sortBy="1"):
sortByPrice = """
select t1.flightno1, t1.flightno2, t1.src, t1.dst, t1.dep_date,
t1.dep_time, t1.arr_time, t1.layover, t1.fare1,
t1.fare2, t2.flightno1 as flightno3, t2.flightno2 as flightno4,
t2.dep_date as ret_date, t2.dep_time, t2.arr_time,
t2.layover, t2.fare1 as fare3, t2.fare2 as fare4,
t1.stops + t2.stops as stops,
(t1.seats + t2.seats) / 2 - abs(t1.seats - t2.seats) / 2 as seats,
t1.price + t2.price as price
from
(select flightno1, flightno2, src, dst, to_char(dep_date) as dep_date,
to_char(dep_time, 'HH24:MI') as dep_time,
to_char(arr_time, 'HH24:MI') as arr_time,
stops, 60 * layover as layover
, price, fare1, fare2,
(seats1 + seats2) / 2 - abs(seats1 - seats2) / 2 as seats
from
(select flightno1, flightno2, src, dst, dep_time, arr_time,
1 stops, layover, price, seats1, seats2, dep_date, fare1, fare2
from good_connections
where to_char(dep_date,'DD/MM/YYYY')='{2}' and src='{0}' and dst='{1}'
union
select flightno flightno1, '' flightno2, src, dst, dep_time,
arr_time, 0 stops, 0 layover, price, seats seats1,
seats+1 as seats2, dep_date, fare as fare1, null fare2
from available_flights
where to_char(dep_date,'DD/MM/YYYY')='{2}' and src='{0}' and dst='{1}'
order by price asc)) t1,
(select flightno1, flightno2, src, dst, to_char(dep_date) as dep_date,
to_char(dep_time, 'HH24:MI') as dep_time,
to_char(arr_time, 'HH24:MI') as arr_time,
stops, 60 * layover as layover
, price, fare1, fare2,
(seats1 + seats2) / 2 - abs(seats1 - seats2) / 2 as seats
from
(select flightno1, flightno2, src, dst, dep_time, arr_time,
1 stops, layover, price, seats1, seats2, dep_date, fare1, fare2
from good_connections
where to_char(dep_date,'DD/MM/YYYY')='{3}' and src='{1}' and dst='{0}'
union
select flightno flightno1, '' flightno2, src, dst, dep_time,
arr_time, 0 stops, 0 layover, price, seats seats1,
seats+1 as seats2, dep_date, fare as fare1, null fare2
from available_flights
where to_char(dep_date,'DD/MM/YYYY')='{3}' and src='{1}' and dst='{0}'
order by price asc)) t2
order by price asc
""".format(source, dest, dep_date, ret_date)
sortByStops = """
select t1.flightno1, t1.flightno2, t1.src, t1.dst, t1.dep_date,
t1.dep_time, t1.arr_time, t1.layover, t1.fare1,
t1.fare2, t2.flightno1 as flightno3, t2.flightno2 as flightno4,
t2.dep_date as ret_date, t2.dep_time, t2.arr_time,
t2.layover, t2.fare1 as fare3, t2.fare2 as fare4,
t1.stops + t2.stops as stops,
(t1.seats + t2.seats) / 2 - abs(t1.seats - t2.seats) / 2 as seats,
t1.price + t2.price as price
from
(select flightno1, flightno2, src, dst, to_char(dep_date) as dep_date,
to_char(dep_time, 'HH24:MI') as dep_time,
to_char(arr_time, 'HH24:MI') as arr_time,
stops, 60 * layover as layover
, price, fare1, fare2,
(seats1 + seats2) / 2 - abs(seats1 - seats2) / 2 as seats
from
(select flightno1, flightno2, src, dst, dep_time, arr_time,
1 stops, layover, price, seats1, seats2, dep_date, fare1, fare2
from good_connections
where to_char(dep_date,'DD/MM/YYYY')='{2}' and src='{0}' and dst='{1}'
union
select flightno flightno1, '' flightno2, src, dst, dep_time,
arr_time, 0 stops, 0 layover, price, seats seats1,
seats+1 as seats2, dep_date, fare as fare1, null fare2
from available_flights
where to_char(dep_date,'DD/MM/YYYY')='{2}' and src='{0}' and dst='{1}'
order by price asc)) t1,
(select flightno1, flightno2, src, dst, to_char(dep_date) as dep_date,
to_char(dep_time, 'HH24:MI') as dep_time,
to_char(arr_time, 'HH24:MI') as arr_time,
stops, 60 * layover as layover
, price, fare1, fare2,
(seats1 + seats2) / 2 - abs(seats1 - seats2) / 2 as seats
from
(select flightno1, flightno2, src, dst, dep_time, arr_time,
1 stops, layover, price, seats1, seats2, dep_date, fare1, fare2
from good_connections
where to_char(dep_date,'DD/MM/YYYY')='{3}' and src='{1}' and dst='{0}'
union
select flightno flightno1, '' flightno2, src, dst, dep_time,
arr_time, 0 stops, 0 layover, price, seats seats1,
seats+1 as seats2, dep_date, fare as fare1, null fare2
from available_flights
where to_char(dep_date,'DD/MM/YYYY')='{3}' and src='{1}' and dst='{0}'
order by price asc)) t2
order by stops, price asc
""".format(source, dest, dep_date, ret_date)
if sortBy == "1":
try:
rs, desc = main.sqlWithReturnDesc(sortByPrice, CONN_STRING)
except:
print("no match found")
return main.menu(email, CONN_STRING)
else:
try:
rs, desc = main.sqlWithReturnDesc(sortByStops, CONN_STRING)
except:
print("no match found")
return main.menu(email, CONN_STRING)
if len(rs) != 0:
i = 1
for row in desc:
print(row[0], end=" ")
print("")
for row in rs:
print(str(i)+".",row)
i+=1
else:
sortByPrice = """
select t1.flightno1, t1.flightno2, t1.src, t1.dst, t1.dep_date,
t1.dep_time, t1.arr_time, t1.layover, t1.fare1,
t1.fare2, t2.flightno1 as flightno3, t2.flightno2 as flightno4,
t2.dep_date as ret_date, t2.dep_time, t2.arr_time,
t2.layover, t2.fare1 as fare3, t2.fare2 as fare4,
t1.stops + t2.stops as stops,
(t1.seats + t2.seats) / 2 - abs(t1.seats - t2.seats) / 2 as seats,
t1.price + t2.price as price
from airports a1, airports a2,
(select flightno1, flightno2, src, dst, to_char(dep_date) as dep_date,
to_char(dep_time, 'HH24:MI') as dep_time,
to_char(arr_time, 'HH24:MI') as arr_time,
stops, 60 * layover as layover
, price, fare1, fare2,
(seats1 + seats2) / 2 - abs(seats1 - seats2) / 2 as seats
from
(select flightno1, flightno2, src, dst, dep_time, arr_time,
1 stops, layover, price, seats1, seats2, dep_date, fare1, fare2
from good_connections
where to_char(dep_date,'DD/MM/YYYY')='{2}'
union
select flightno flightno1, '' flightno2, src, dst, dep_time,
arr_time, 0 stops, 0 layover, price, seats seats1,
seats+1 as seats2, dep_date, fare as fare1, null fare2
from available_flights
where to_char(dep_date,'DD/MM/YYYY')='{2}'
order by price asc)) t1,
(select flightno1, flightno2, src, dst, to_char(dep_date) as dep_date,
to_char(dep_time, 'HH24:MI') as dep_time,
to_char(arr_time, 'HH24:MI') as arr_time,
stops, 60 * layover as layover
, price, fare1, fare2,
(seats1 + seats2) / 2 - abs(seats1 - seats2) / 2 as seats
from
(select flightno1, flightno2, src, dst, dep_time, arr_time,
1 stops, layover, price, seats1, seats2, dep_date, fare1, fare2
from good_connections
where to_char(dep_date,'DD/MM/YYYY')='{3}'
union
select flightno flightno1, '' flightno2, src, dst, dep_time,
arr_time, 0 stops, 0 layover, price, seats seats1,
seats+1 as seats2, dep_date, fare as fare1, null fare2
from available_flights
where to_char(dep_date,'DD/MM/YYYY')='{3}'
order by price asc)) t2
where (lower(a1.city) like '%{0}%'
or lower(a1.name) like '%{0}%')
and a1.acode = t1.src and a2.acode = t1.dst
and (lower(a2.city) like '%{1}%' or
lower(a2.name) like '%{1}%') and
t1.src = t2.dst and t1.dst = t2.src
order by price asc
""".format(source.lower(), dest.lower(), dep_date, ret_date)
sortByStops = """
select t1.flightno1, t1.flightno2, t1.src, t1.dst, t1.dep_date,
t1.dep_time, t1.arr_time, t1.layover, t1.fare1,
t1.fare2, t2.flightno1 as flightno3, t2.flightno2 as flightno4,
t2.dep_date as ret_date, t2.dep_time, t2.arr_time,
t2.layover, t2.fare1 as fare3, t2.fare2 as fare4,
t1.stops + t2.stops as stops,
(t1.seats + t2.seats) / 2 - abs(t1.seats - t2.seats) / 2 as seats,
t1.price + t2.price as price
from airports a1, airports a2,
(select flightno1, flightno2, src, dst, to_char(dep_date) as dep_date,
to_char(dep_time, 'HH24:MI') as dep_time,
to_char(arr_time, 'HH24:MI') as arr_time,
stops, 60 * layover as layover
, price, fare1, fare2,
(seats1 + seats2) / 2 - abs(seats1 - seats2) / 2 as seats
from
(select flightno1, flightno2, src, dst, dep_time, arr_time,
1 stops, layover, price, seats1, seats2, dep_date, fare1, fare2
from good_connections
where to_char(dep_date,'DD/MM/YYYY')='{2}'
union
select flightno flightno1, '' flightno2, src, dst, dep_time,
arr_time, 0 stops, 0 layover, price, seats seats1,
seats+1 as seats2, dep_date, fare as fare1, null fare2
from available_flights
where to_char(dep_date,'DD/MM/YYYY')='{2}'
order by price asc)) t1,
(select flightno1, flightno2, src, dst, to_char(dep_date) as dep_date,
to_char(dep_time, 'HH24:MI') as dep_time,
to_char(arr_time, 'HH24:MI') as arr_time,
stops, 60 * layover as layover
, price, fare1, fare2,
(seats1 + seats2) / 2 - abs(seats1 - seats2) / 2 as seats
from
(select flightno1, flightno2, src, dst, dep_time, arr_time,
1 stops, layover, price, seats1, seats2, dep_date, fare1, fare2
from good_connections
where to_char(dep_date,'DD/MM/YYYY')='{3}'
union
select flightno flightno1, '' flightno2, src, dst, dep_time,
arr_time, 0 stops, 0 layover, price, seats seats1,
seats+1 as seats2, dep_date, fare as fare1, null fare2
from available_flights
where to_char(dep_date,'DD/MM/YYYY')='{3}'
order by price asc)) t2
where (lower(a1.city) like '%{0}%'
or lower(a1.name) like '%{0}%')
and a1.acode = t1.src and a2.acode = t1.dst
and (lower(a2.city) like '%{1}%' or
lower(a2.name) like '%{1}%') and
t1.src = t2.dst and t1.dst = t2.src
order by price asc
""".format(source.lower(), dest.lower(), dep_date, ret_date)
if sortBy == "1":
try:
rs, desc = main.sqlWithReturnDesc(sortByPrice, CONN_STRING)
except:
print("There was an unexpected error while trying to find flights. Please try again.")
return main.menu(email, CONN_STRING)
else:
try:
rs, desc = main.sqlWithReturnDesc(sortByStops, CONN_STRING)
except:
print("There was an unexpected error while trying to find flights. Please try again.")
return main.menu(email, CONN_STRING)
if len(rs) != 0:
i = 1
for row in desc:
print(row[0], end=" ")
print("")
for row in rs:
print(str(i)+".", row)
i+=1
else:
print("no results")
return main.menu(email, CONN_STRING)
print(str(len(rs)+1)+".", "Sort by number of connections")
print(str(len(rs)+2)+".", "Make a booking")
print(str(len(rs)+3)+".", "Go back to menu")
option = input("Enter the number of an option: ")
try:
optNum = int(option)
if optNum<0 or optNum>len(rs)+3:
print("Not valid number")
elif optNum == len(rs)+1:
printRoundInfo(email, CONN_STRING, source, dest, dep_date, ret_date, "2")
elif optNum == len(rs)+2:
flightno = int(input("Enter the number before the flight you want to book: "))
if sortBy == "1":
rs = main.sqlWithReturn(sortByPrice, CONN_STRING)
else:
newRs = main.sqlWithReturn(sortByStops, CONN_STRING)
# compare each row in new result set with user selected row and
# record the index
selected = rs[flightno-1]
i = 0
for row in newRs:
if row[0] == selected[0] and row[1] == selected[1] and row[8] == selected[8] and row[9] == selected[9] and row[10] == selected[10] and row[11] == selected[11] and row[-5] == selected[-5] and row[-4] == selected[-4]:
break
else:
i += 1
# if not found, print error message and go back
if i == len(newRs):
print("tickets for your selected flight has run out")
return printRoundInfo(email, CONN_STRING, source, dest, dep_date, ret_date, sortBy)
# call booking function
return roundBooking(email, CONN_STRING, i+1, newRs, dep_date, ret_date, source, dest)
elif optNum == len(rs)+3:
main.menu(email, CONN_STRING)
else:
print("Not valid number")
return main.menu(email, CONN_STRING)
except:
print("Not valid number")
return main.menu(email, CONN_STRING)
def roundBooking(email, CONN_STRING, flightno, rs, dep_date, ret_date, source, dest):
row = rs[flightno-1]
if row[-2] <= 0:
print("tickets not exists, please choose another flight")
printInfo(email, CONN_STRING, source, dest, dep_date, "1")
try:
sql = "select max(tno) from tickets"
maxTno = main.sqlWithReturn(sql, CONN_STRING)[0][0]
sql = "select name from passengers where email = '{0}'".format(email)
name = main.sqlWithReturn(sql, CONN_STRING)[0][0]
sql = "insert into tickets values({0}, '{1}', '{2}', '{3}')".format(maxTno+1, name, email, row[-1])
main.sqlWithNoReturn(sql, CONN_STRING)
sql = "insert into bookings values({0}, '{1}', '{2}', to_date('{3}', 'DD/MM/YYYY'), null)".format(maxTno+1, row[0], row[8], dep_date)
main.sqlWithNoReturn(sql, CONN_STRING)
sql = "insert into bookings values({0}, '{1}', '{2}', to_date('{3}', 'DD/MM/YYYY'), null)".format(maxTno+1, row[10], row[-5], ret_date)
main.sqlWithNoReturn(sql, CONN_STRING)
if row[1] is not None:
sql = "insert into bookings values({0}, '{1}', '{2}', to_date('{3}', 'DD/MM/YYYY'), null)".format(maxTno+1, row[1], row[9], dep_date)
main.sqlWithNoReturn(sql, CONN_STRING)
if row[11] is not None:
sql = "insert into bookings values({0}, '{1}', '{2}', to_date('{3}', 'DD/MM/YYYY'), null)".format(maxTno+1, row[11], row[-4], ret_date)
main.sqlWithNoReturn(sql, CONN_STRING)
print("success")
except:
print("There was an error during booking. Please try again")
main.menu(email, CONN_STRING)
printRoundInfo(email, CONN_STRING, source, dest, dep_date, ret_date)