-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathmain.py
More file actions
374 lines (333 loc) · 19.1 KB
/
main.py
File metadata and controls
374 lines (333 loc) · 19.1 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
from src.file_handler import load_itineraries
from src.manage_itineraries import add_itinerary, edit_itinerary, add_new_flight, add_new_attraction, view_itineraries, delete_itinerary, delete_itinerary_item, print_table
from pick import pick
def run_app():
"""
Main loop to run the travel planner.
"""
itineraries = load_itineraries()
app_running = True
while app_running:
print("Welcome to the Travel Itinerary Planner app!\n")
print("1. Add a new Itinerary")
print("2. Edit an existing Itinerary item")
print("3. Add a new flight/attraction to an existing Itinerary")
print("4. View existing Itineraries")
print("5. Delete an Itinerary/Itinerary item")
print("6. Log out\n")
# Check user_choice was a number
while True:
user_choice = input("Please select one of the above options (1-6): ")
if user_choice.isdigit():
choice = int(user_choice)
if 1 <= choice <= 6:
break
else:
print("Enter a number between 1-6.")
else:
print("Invalid input: Please enter a number between 1-6.")
# Add a new itinerary
if user_choice == "1":
print("How exciting! Please provide us information about the trip: \n")
name = input("Name of the itinerary (please choose a name you will remember for later): ")
location = input("Location (NA if not applicable): ")
description = input("Brief description of trip: ")
start_date = input("Start date in DD-MM-YYYY: ")
end_date = input("End date in DD-MM-YYYY: ")
flights = []
attractions = []
print("\nNow it is time to add flights!")
user_flight_choice = input("If you want to skip this step, type SKIP and press 'Enter'. Otherwise, press 'Enter'. ")
flights_list_done = False
if user_flight_choice == "SKIP":
flights = []
else:
while not flights_list_done:
departure_airport = input("Name of the airport you will depart from: ")
departure_date = input("Date & time of flight departure (Format: DD-MM-YYYY HH:MM): ")
arrival_airport = input("Name of the airport you will arrive at: ")
arrival_date = input("Date & time of flight arrival (Format: DD-MM-YYYY HH:MM): ")
flight_name = f"{departure_airport} to {arrival_airport}"
flights.append({
"flight name": flight_name,
"departure airport": departure_airport,
"departure date": departure_date,
"arrival airport": arrival_airport,
"arrival date": arrival_date
})
while True:
add_another_flight = input("Would you like to add another flight? Type Y (yes) or N (no): ")
if add_another_flight == "Y":
flights_list_done = False
break
elif add_another_flight == "N":
flights_list_done = True
break
else:
print("Invalid answer: Please type Y or N only.")
print("\nFinally: ATTRACTIONS!")
user_attractions_choice = input("If you want to skip this step, type SKIP and press 'Enter'. Otherwise, press 'Enter'. ")
attractions_list_done = False
if user_attractions_choice == "SKIP":
attractions = []
else:
while not attractions_list_done:
attraction_name = input("Name of attraction: ")
attraction_address = input("Address of attraction: ")
attraction_summary = input("Short description of attraction: ")
attraction_tags = input("(Optional) Provide some tags that categorise what kind of activity this involves.\nExample of format required: hike, exciting, views\n")
attractions.append({
"attraction name": attraction_name,
"address": attraction_address,
"summary": attraction_summary,
"tag(s)": attraction_tags
})
while True:
add_another_attraction = input("Would you like to add another attraction? Type Y or N:")
if add_another_attraction == "Y":
attractions_list_done = False
break
elif add_another_attraction == "N":
attractions_list_done = True
break
else:
print("Invalid answer: Please type Y or N only.")
add_itinerary(itineraries, name, location, description, start_date, end_date, flights, attractions)
# Edit existing itinerary
elif user_choice == "2":
# Use rich module to print all the itineraries available, THEN pick module to pick an itinerary to modify
print_table(itineraries)
itinerary_prompt = 'Here are all the available itineraries. Which would you like to edit?:'
itinerary_options = []
for itinerary in itineraries:
itinerary_options.append(itinerary["name"])
itinerary_option, itinerary_index = pick(itinerary_options, itinerary_prompt)
# Use pick module to select value to modify
edit_prompt = 'Please select the item you want to edit: '
edit_options = ['name', 'location', 'description', 'start_date', 'end_date', 'flights', 'attractions']
edit_option, edit_index = pick(edit_options, edit_prompt)
if edit_option == 'flights':
attraction_choice = "N/A"
flight_name_prompt = 'Which flight would you like to edit?'
flight_name_options = []
for itinerary in itineraries:
if itinerary["name"] == itinerary_option:
for flight in itinerary['flights']:
flight_name_options.append(flight["flight name"])
flight_choice, flight_name_index = pick(flight_name_options, flight_name_prompt)
flight_prompt = 'Finally, what about the flight would you like to edit?'
flight_options = ['departure airport', 'departure date', 'arrival airport', 'arrival date']
edit_option, flight_index = pick(flight_options, flight_prompt)
elif edit_option == 'attractions':
flight_choice = "N/A"
attraction_name_prompt = 'Which attraction would you like to edit?'
attractions_available = []
for itinerary in itineraries:
if itinerary["name"] == itinerary_option:
for attraction in itinerary['attractions']:
attractions_available.append(attraction["attraction name"])
attraction_choice, attraction_choice_index = pick(attractions_available, attraction_name_prompt)
attractions_prompt = 'Which attraction property would you like to edit?'
attraction_options = ['attraction name', 'address', 'summary', 'tag(s)']
edit_option, attraction_index = pick(attraction_options, attractions_prompt)
elif edit_option != 'flights' or edit_option != 'attractions':
flight_choice = "N/A"
attraction_choice = "N/A"
# Set new value
new_edit_value = input(f"What would you like to change {edit_option} to?\n(If a start/end date use DD-MM-YYYY format. If a flight date/time, use DD-MM-YYYY HH:MM)")
edit_itinerary(itineraries, itinerary_option, edit_option, flight_choice, attraction_choice, new_edit_value)
# Add flight or attraction to existing itinerary
elif user_choice == "3":
itinerary_prompt = 'Which itinerary would you like to add to?: '
itinerary_options = []
for itinerary in itineraries:
itinerary_options.append(itinerary["name"])
itinerary_option, itinerary_index = pick(itinerary_options, itinerary_prompt)
edit_prompt = 'Please select which item you want to add: '
edit_options = ['flights', 'attractions']
option, edit_index = pick(edit_options, edit_prompt)
# Add a flight
if option == 'flights':
flights = []
flights_list_done = False
while not flights_list_done:
departure_airport = input("Name of the airport you will depart from: ")
departure_date = input("Date & time of flight departure (Format: DD-MM-YYYY HH:MM): ")
arrival_airport = input("Name of the airport you will arrive at: ")
arrival_date = input("Date & time of flight arrival (Format: DD-MM-YYYY HH:MM): ")
flight_name = f"{departure_airport} to {arrival_airport}"
flights.append({
"flight name": flight_name,
"departure airport": departure_airport,
"departure date": departure_date,
"arrival airport": arrival_airport,
"arrival date": arrival_date
})
# Check if flight already exists
for itinerary in itineraries:
for flight in itinerary['flights']:
if flight_name == flight['flight name']:
print(f"Flight '{flight_name}' already exists!")
flights_list_done = True
break
if flights_list_done:
print("Returning to main menu...")
break
while True:
add_another_flight = input("Would you like to add another flight? Type Y (yes) or N (no): ")
if add_another_flight == "Y":
flights_list_done = False
break
elif add_another_flight == "N":
flights_list_done = True
break
else:
print("Invalid answer: Please type Y or N only.")
add_new_flight(itineraries, itinerary_option, flights)
# Add an attraction
elif option == 'attractions':
attractions = []
attractions_list_done = False
while not attractions_list_done:
attraction_name = input("Name of attraction: ")
attraction_address = input("Address of attraction: ")
attraction_summary = input("Short description of attraction: ")
attraction_tags = input(
"(Optional) Provide some tags that categorise what kind of activity this involves.\nExample of format required: hike, exciting, views\n")
attractions.append({
"attraction name": attraction_name,
"address": attraction_address,
"summary": attraction_summary,
"tag(s)": attraction_tags
})
# Check if attraction already exists
for itinerary in itineraries:
for attraction in itinerary['attractions']:
if attraction_name == attraction['attraction name']:
print(f"Attraction '{attraction_name}' already exists!")
attractions_list_done = True
break
if attractions_list_done:
print("Returning to main menu...")
break
while True:
add_another_attraction = input("Would you like to add another attraction? Type Y or N:")
if add_another_attraction == "Y":
attractions_list_done = False
break
elif add_another_attraction == "N":
attractions_list_done = True
break
else:
print("Invalid answer: Please type Y or N only.")
add_new_attraction(itineraries, itinerary_option, attractions)
# View itinerary
elif user_choice == "4":
if not itineraries:
print("No itineraries available to view!")
else:
# Use pick module: Ask if they would like to view all itineraries, or a specific one
view_prompt = 'Would you like to view all the existing itineraries?: '
view_options = ['View All', 'View One']
option, index = pick(view_options, view_prompt)
if option == 'View All':
print(f"\nFilter selected: {option}")
filter_option = "All"
elif option == 'View One':
# Use pick module to select an itinerary by name and location
print(f"\nFilter selected: {option}")
itinerary_choice = 'Which itinerary would you like to view?: '
itinerary_options = []
for trip in itineraries:
itinerary_options.append(trip["name"])
filter_option, filter_index = pick(itinerary_options, itinerary_choice)
view_itineraries(itineraries, filter_option)
# Delete itinerary
elif user_choice == "5":
# Use pick to choose delete options
delete_prompt = 'Would you like to delete a full itinerary, or a flight/attraction? \n(Note: You can only delete a flight or attraction if there is MORE THAN ONE available in the itinerary. \nIf there is only one, please select return.)'
delete_options = ['Entire itinerary', 'Itinerary flight', 'Itinerary attraction', 'Return']
delete_option, delete_index = pick(delete_options, delete_prompt)
print_table(itineraries)
if delete_option == 'Entire itinerary':
itinerary_prompt = 'Which itinerary would you like to delete? '
itinerary_options = []
for itinerary in itineraries:
itinerary_options.append(itinerary["name"])
itinerary_option, itinerary_index = pick(itinerary_options, itinerary_prompt)
if delete_itinerary(itineraries, itinerary_option):
print("Itinerary has been deleted.")
else:
print("Itinerary could not be found.")
elif delete_option == 'Itinerary flight':
while True:
multiple_flights = True
selected_type = "flights"
# Choose itinerary
itinerary_prompt = 'Which itinerary would you like to change? '
itinerary_options = []
for itinerary in itineraries:
itinerary_options.append(itinerary["name"])
itinerary_option, itinerary_index = pick(itinerary_options, itinerary_prompt)
# Choose flight
flight_prompt = 'Which flight would you like to delete? '
flight_options = []
for itinerary in itineraries:
if itinerary["name"] == itinerary_option:
if len(itinerary["flights"]) <= 1:
print("There is only one flight available, therefore you cannot delete it.")
multiple_flights = False
break
else:
for flight in itinerary["flights"]:
flight_options.append(flight["flight name"])
# Checks if 'while True' statement should be broken
if not multiple_flights:
print("Returning to main menu...")
break
flight_id, itinerary_index = pick(flight_options, flight_prompt)
if delete_itinerary_item(itineraries, selected_type, itinerary_option, flight_id):
print(f"Flight '{flight_id}' has been deleted.")
else:
print("Flight could not be found.")
break
elif delete_option == 'Itinerary attraction':
while True:
multiple_attractions = True
selected_type = "attractions"
# Choose itinerary
itinerary_prompt = 'Which itinerary would you like to change? '
itinerary_options = []
for itinerary in itineraries:
itinerary_options.append(itinerary["name"])
itinerary_option, itinerary_index = pick(itinerary_options, itinerary_prompt)
# Choose attraction
attraction_prompt = 'Which attraction would you like to delete? '
attraction_options = []
for itinerary in itineraries:
if itinerary["name"] == itinerary_option:
if len(itinerary["attractions"]) <= 1:
print("There is only one attraction available, therefore you cannot delete it.")
multiple_attractions = False
break
else:
for attraction in itinerary["attractions"]:
attraction_options.append(attraction["attraction name"])
# Checks if 'while True' statement should be broken
if not multiple_attractions:
print("Returning to main menu...")
break
attraction_id, itinerary_index = pick(attraction_options, attraction_prompt)
if delete_itinerary_item(itineraries, selected_type, itinerary_option, attraction_id):
print(f"Attraction '{attraction_id}' has been deleted.")
else:
print("Attraction could not be found.")
break
elif delete_option == 'Return':
print("Returning to previous menu...\n")
# Log out
elif user_choice == "6":
app_running = False
print("See you next time! 👋")
if __name__ == "__main__":
run_app()