Skip to content

Commit aa82bf9

Browse files
Feat: Added itinerary editing functionality (edit_itinerary(), add_new_flight(), AND add_new_attraction) to main.py and manage_itineraries_py, + tidied up itinerary nesting.
1 parent 5dcea41 commit aa82bf9

2 files changed

Lines changed: 294 additions & 75 deletions

File tree

Travel Itinerary Planner/main.py

Lines changed: 136 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from src.file_handler import load_itineraries
2-
from src.manage_itineraries import add_itinerary, edit_itinerary, view_itineraries, delete_itinerary
2+
from src.manage_itineraries import add_itinerary, edit_itinerary, add_new_flight, add_new_attraction, view_itineraries, delete_itinerary, print_table
3+
from pick import pick
34

45

56
def run_app():
@@ -12,57 +13,57 @@ def run_app():
1213
while app_running:
1314
print("Welcome to the Travel Itinerary Planner app!\n")
1415
print("1. Add a new Itinerary")
15-
print("2. Edit an existing Itinerary")
16-
print("3. View existing Itineraries")
17-
print("4. Delete an Itinerary")
18-
print("5. Log out\n")
16+
print("2. Edit an existing Itinerary item")
17+
print("3. Add a new flight/attraction to an existing Itinerary")
18+
print("4. View existing Itineraries")
19+
print("5. Delete an Itinerary/Itinerary item")
20+
print("6. Log out\n")
1921

2022
# Check user_choice was a number
2123
while True:
22-
user_choice = input("Please select one of the above options (1-5): ")
24+
user_choice = input("Please select one of the above options (1-6): ")
2325
if user_choice.isdigit():
2426
choice = int(user_choice)
25-
if 1 <= choice <= 5:
27+
if 1 <= choice <= 6:
2628
break
2729
else:
28-
print("Enter a number between 1-5.")
30+
print("Enter a number between 1-6.")
2931
else:
30-
("Invalid input: Please enter a number between 1-5.")
32+
print("Invalid input: Please enter a number between 1-6.")
3133

3234
# Add a new itinerary
3335
if user_choice == "1":
3436
print("How exciting! Please provide us information about the trip: \n")
3537
name = input("Name of the itinerary (please choose a name you will remember for later): ")
3638
location = input("Location (NA if not applicable): ")
37-
summary = input("Brief description of trip: ")
39+
description = input("Brief description of trip: ")
3840
start_date = input("Start date in DD-MM-YYYY: ")
3941
end_date = input("End date in DD-MM-YYYY: ")
40-
flights = {}
41-
attractions = {}
42+
flights = []
43+
attractions = []
4244

4345
print("\nNow it is time to add flights!")
4446
user_flight_choice = input("If you want to skip this step, type SKIP and press 'Enter'. Otherwise, press 'Enter'. ")
4547
flights_list_done = False
4648

4749
if user_flight_choice == "SKIP":
48-
flights = {}
50+
flights = []
4951
else:
5052
while not flights_list_done:
51-
flight_details = {}
5253

5354
departure_airport = input("Name of the airport you will depart from: ")
5455
departure_date = input("Date & time of flight departure (Format: DD-MM-YYYY HH:MM): ")
5556
arrival_airport = input("Name of the airport you will arrive at: ")
5657
arrival_date = input("Date & time of flight arrival (Format: DD-MM-YYYY HH:MM): ")
5758
flight_name = f"{departure_airport} to {arrival_airport}"
5859

59-
flight_details.update({
60+
flights.append({
61+
"flight name": flight_name,
6062
"departure airport": departure_airport,
6163
"departure date": departure_date,
6264
"arrival airport": arrival_airport,
6365
"arrival date": arrival_date
6466
})
65-
flights[flight_name] = flight_details
6667

6768
while True:
6869
add_another_flight = input("Would you like to add another flight? Type Y (yes) or N (no): ")
@@ -80,23 +81,21 @@ def run_app():
8081
attractions_list_done = False
8182

8283
if user_attractions_choice == "SKIP":
83-
attractions = {}
84+
attractions = []
8485
else:
8586
while not attractions_list_done:
86-
attraction_details = {}
8787

8888
attraction_name = input("Name of attraction: ")
8989
attraction_address = input("Address of attraction: ")
9090
attraction_summary = input("Short description of attraction: ")
91-
attraction_type = input("(Optional) Provide some tags that categorise what kind of activity this involves.\nExample of format required: hike, exciting, views\n")
92-
attraction_tags = attraction_type.split(", ")
91+
attraction_tags = input("(Optional) Provide some tags that categorise what kind of activity this involves.\nExample of format required: hike, exciting, views\n")
9392

94-
attraction_details.update({
93+
attractions.append({
94+
"attraction name": attraction_name,
9595
"address": attraction_address,
9696
"summary": attraction_summary,
9797
"tag(s)": attraction_tags
9898
})
99-
attractions[attraction_name] = attraction_details
10099

101100
while True:
102101
add_another_attraction = input("Would you like to add another attraction? Type Y or N:")
@@ -109,27 +108,134 @@ def run_app():
109108
else:
110109
print("Invalid answer: Please type Y or N only.")
111110

112-
add_itinerary(itineraries, name, location, summary, start_date, end_date, flights, attractions)
111+
add_itinerary(itineraries, name, location, description, start_date, end_date, flights, attractions)
113112

114113
# Edit existing itinerary
115114
elif user_choice == "2":
116-
# Use pickpack module (https://github.com/anafvana/pickpack#map-function-for-nested-lists)
117-
chosen_itinerary = input("")
118-
edit_itinerary(chosen_itinerary)
119-
pass
115+
# Use rich module to print all the itineraries available, THEN pick module to pick an itinerary to modify
116+
print_table(itineraries)
117+
118+
itinerary_prompt = 'Here are all the available itineraries. Which would you like to edit?:'
119+
itinerary_options = []
120+
for itinerary in itineraries:
121+
itinerary_options.append(itinerary["name"])
122+
itinerary_option, itinerary_index = pick(itinerary_options, itinerary_prompt)
123+
124+
# Use pick module to select value to modify
125+
edit_prompt = 'Please select the item you want to edit: '
126+
edit_options = ['name', 'location', 'description', 'start_date', 'end_date', 'flights', 'attractions']
127+
edit_option, edit_index = pick(edit_options, edit_prompt)
128+
129+
if edit_option == 'flights':
130+
flight_name_prompt = 'Which flight would you like to edit?'
131+
flight_name_options = []
132+
for item in itineraries:
133+
for flight in item['flights']:
134+
flight_name_options.append(flight["flight name"])
135+
flight_choice, flight_name_index = pick(flight_name_options, flight_name_prompt)
136+
flight_prompt = 'Finally, what about the flight would you like to edit?'
137+
flight_options = ['departure airport', 'departure date', 'arrival airport', 'arrival date']
138+
edit_option, flight_index = pick(flight_options, flight_prompt)
139+
elif edit_option == 'attractions':
140+
attraction_name_prompt = 'Which attraction would you like to edit?'
141+
attractions_available = []
142+
for item in itineraries:
143+
for attraction in item['attractions']:
144+
attractions_available.append(attraction["attraction name"])
145+
attraction_choice, attraction_choice_index = pick(attractions_available, attraction_name_prompt)
146+
attractions_prompt = 'Which attraction property would you like to edit?'
147+
attraction_options = ['attraction_name', 'address', 'summary', 'tag(s)']
148+
edit_option, attraction_index = pick(attraction_options, attractions_prompt)
149+
elif edit_option != 'flights' or edit_option != 'attractions':
150+
flight_choice = "N/A"
151+
attraction_choice = "N/A"
152+
print(flight_choice)
153+
154+
edit_itinerary(itineraries, itinerary_option, edit_option, flight_choice, attraction_choice)
155+
156+
# Add flight or attraction to existing itinerary
157+
elif user_choice == "3":
158+
itinerary_prompt = 'Which itinerary would you like to add to?: '
159+
itinerary_options = []
160+
for itinerary in itineraries:
161+
itinerary_options.append(itinerary["name"])
162+
itinerary_option, itinerary_index = pick(itinerary_options, itinerary_prompt)
163+
164+
edit_prompt = 'Please select which item you want to add: '
165+
edit_options = ['flights', 'attractions']
166+
option, edit_index = pick(edit_options, edit_prompt)
167+
168+
# Add a flight
169+
if option == 'flights':
170+
flights = []
171+
flights_list_done = False
172+
while not flights_list_done:
173+
departure_airport = input("Name of the airport you will depart from: ")
174+
departure_date = input("Date & time of flight departure (Format: DD-MM-YYYY HH:MM): ")
175+
arrival_airport = input("Name of the airport you will arrive at: ")
176+
arrival_date = input("Date & time of flight arrival (Format: DD-MM-YYYY HH:MM): ")
177+
flight_name = f"{departure_airport} to {arrival_airport}"
178+
flights.append({
179+
"flight name": flight_name,
180+
"departure airport": departure_airport,
181+
"departure date": departure_date,
182+
"arrival airport": arrival_airport,
183+
"arrival date": arrival_date
184+
})
185+
186+
while True:
187+
add_another_flight = input("Would you like to add another flight? Type Y (yes) or N (no): ")
188+
if add_another_flight == "Y":
189+
flights_list_done = False
190+
break
191+
elif add_another_flight == "N":
192+
flights_list_done = True
193+
break
194+
else:
195+
print("Invalid answer: Please type Y or N only.")
196+
add_new_flight(itineraries, itinerary_option, flights)
197+
198+
# Add an attraction
199+
elif option == 'attractions':
200+
attractions = []
201+
attractions_list_done = False
202+
while not attractions_list_done:
203+
attraction_name = input("Name of attraction: ")
204+
attraction_address = input("Address of attraction: ")
205+
attraction_summary = input("Short description of attraction: ")
206+
attraction_tags = input(
207+
"(Optional) Provide some tags that categorise what kind of activity this involves.\nExample of format required: hike, exciting, views\n")
208+
attractions.append({
209+
"attraction name": attraction_name,
210+
"address": attraction_address,
211+
"summary": attraction_summary,
212+
"tag(s)": attraction_tags
213+
})
214+
215+
while True:
216+
add_another_attraction = input("Would you like to add another attraction? Type Y or N:")
217+
if add_another_attraction == "Y":
218+
attractions_list_done = False
219+
break
220+
elif add_another_attraction == "N":
221+
attractions_list_done = True
222+
break
223+
else:
224+
print("Invalid answer: Please type Y or N only.")
225+
add_new_attraction(itineraries, itinerary_option, attractions)
120226

121227
# View itinerary
122-
elif user_choice == "3":
228+
elif user_choice == "4":
123229
view_itineraries(itineraries)
124230

125231
# Delete itinerary
126-
elif user_choice == "4":
232+
elif user_choice == "5":
127233
chosen_itinerary = input("")
128234
delete_itinerary(chosen_itinerary)
129235
pass
130236

131237
# Log out
132-
elif user_choice == "5":
238+
elif user_choice == "6":
133239
app_running = False
134240

135241
print("See you next time! 👋")

0 commit comments

Comments
 (0)