Skip to content

Commit 00864d2

Browse files
Feat: add_itinerary feature completed
1 parent f836acd commit 00864d2

4 files changed

Lines changed: 169 additions & 164 deletions

File tree

Travel Itinerary Planner/main.py

Lines changed: 120 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,12 @@
1-
from src.manage_itineraries import add_itinerary, edit_itinerary, view_itineraries, delete_itinerary, export_itinerary
2-
from pick import pick
3-
4-
5-
def user_task_request(request, itinerary_list):
6-
'''
7-
Executes a specific request based on user's choice.
8-
'''
9-
# Add a new itinerary
10-
if request == "1":
11-
flights_list_done = False
12-
attractions_list_done = False
13-
14-
print("How exciting! Please provide us information about the trip: \n")
15-
name = input("Title of the itinerary: ")
16-
location = input("Location (NA if not applicable): ")
17-
summary = input("Brief description of trip: ")
18-
start_date = input("Start date in DD-MM-YYYY: ")
19-
end_date = input("End date in DD-MM-YYYY: ")
20-
flights = {}
21-
attractions = {}
22-
23-
print("\nNow it is time to add flights!")
24-
user_flight_choice = input("If you want to skip this step, type SKIP and press 'Enter'. Otherwise, press 'Enter'. ")
25-
flights_list_done = False
26-
27-
if user_flight_choice == "SKIP":
28-
flights = {}
29-
else:
30-
while not flights_list_done:
31-
flight_details = {}
32-
33-
departure_airport = input("Name of the airport you will depart from: ")
34-
departure_date = input("Date & time of flight departure (Format: DD-MM-YYYY HH:MM): ")
35-
arrival_airport = input("Name of the airport you will arrive at: ")
36-
arrival_date = input("Date & time of flight arrival (Format: DD-MM-YYYY HH:MM): ")
37-
flight_name = f"{departure_airport} to {arrival_airport}"
38-
39-
flight_details.update({
40-
"departure airport": departure_airport,
41-
"departure date": departure_date,
42-
"arrival airport": arrival_airport,
43-
"arrival date": arrival_date
44-
})
45-
flights[flight_name] = flight_details
46-
47-
add_another_flight = input("Would you like to add another flight? Type Y (yes) or N (no): ")
48-
while True:
49-
if add_another_flight == "Y":
50-
flights_list_done = True
51-
break
52-
elif add_another_flight == "N":
53-
flights_list_done = False
54-
break
55-
else:
56-
print("Invalid answer: Please type Y or N only.")
57-
58-
print("\nFinally: ATTRACTIONS!")
59-
user_attractions_choice = input("If you want to skip this step, type SKIP and press 'Enter'. Otherwise, press 'Enter'. ")
60-
attractions_list_done = False
61-
62-
if user_attractions_choice == "SKIP":
63-
attractions = {}
64-
else:
65-
while not attractions_list_done:
66-
attraction_details = {}
67-
68-
attraction_name = input("Name of attraction: ")
69-
attraction_address = input("Address of attraction: ")
70-
attraction_summary = input("Short description of attraction: ")
71-
attraction_type = input("(Optional) Provide some tags that categorise what kind of activity this involves.\nExample of format required: hike, exciting, views\n")
72-
attraction_tags = attraction_type.split(", ")
73-
74-
attraction_details.update({
75-
"address": attraction_address,
76-
"summary": attraction_summary,
77-
"tag(s)": attraction_tags
78-
})
79-
attractions[attraction_name] = attraction_details
80-
81-
add_another_attraction = input("Would you like to add another attraction? Type Y or N:")
82-
while True:
83-
if add_another_attraction == "Y":
84-
attractions_list_done = True
85-
break
86-
elif add_another_attraction == "N":
87-
attractions_list_done = False
88-
break
89-
else:
90-
print("Invalid answer: Please type Y or N only.")
91-
92-
add_itinerary(itinerary_list, name, location, summary, start_date, end_date, flights, attractions)
93-
94-
# Edit existing itinerary
95-
elif request == "2":
96-
# Use pickpack module (https://github.com/anafvana/pickpack#map-function-for-nested-lists)
97-
chosen_itinerary = input("")
98-
edit_itinerary(itinerary_list, chosen_itinerary)
99-
pass
100-
101-
# View itinerary
102-
elif request == "3":
103-
chosen_itinerary = input("")
104-
view_itineraries(itinerary_list, chosen_itinerary)
105-
pass
106-
107-
# Delete itinerary
108-
elif request == "4":
109-
chosen_itinerary = input("")
110-
delete_itinerary(itinerary_list, chosen_itinerary)
111-
pass
112-
113-
# Export itinerary
114-
elif request == "5":
115-
chosen_itinerary = input("")
116-
export_itinerary(itinerary_list, chosen_itinerary)
117-
pass
118-
119-
else:
120-
print("Invalid answer, please type a number between 1-5.")
121-
return
1+
from src.file_handler import load_itineraries
2+
from src.manage_itineraries import add_itinerary, edit_itinerary, view_itineraries, delete_itinerary
1223

1234

1245
def run_app():
1256
"""
1267
Main loop to run the travel planner.
1278
"""
128-
itineraries = []
9+
itineraries = load_itineraries()
12910
app_running = True
13011

13112
while app_running:
@@ -134,26 +15,129 @@ def run_app():
13415
print("2. Edit an existing Itinerary")
13516
print("3. View existing Itineraries")
13617
print("4. Delete an Itinerary")
137-
print("5. Export an Itinerary")
138-
print("6. Log out\n")
139-
140-
user_choice = input("Please select one of the above options (1-5): ")
18+
print("5. Log out\n")
14119

14220
# Check user_choice was a number
143-
if user_choice.isdigit():
144-
choice = int(user_choice)
145-
if 1 <= choice <= 6:
146-
if user_choice == "6":
147-
app_running = False
21+
while True:
22+
user_choice = input("Please select one of the above options (1-5): ")
23+
if user_choice.isdigit():
24+
choice = int(user_choice)
25+
if 1 <= choice <= 5:
26+
break
14827
else:
149-
user_task_request(user_choice, itineraries)
28+
print("Enter a number between 1-5.")
29+
else:
30+
("Invalid input: Please enter a number between 1-5.")
31+
32+
# Add a new itinerary
33+
if user_choice == "1":
34+
flights_list_done = False
35+
attractions_list_done = False
36+
37+
print("How exciting! Please provide us information about the trip: \n")
38+
name = input("Title of the itinerary: ")
39+
location = input("Location (NA if not applicable): ")
40+
summary = input("Brief description of trip: ")
41+
start_date = input("Start date in DD-MM-YYYY: ")
42+
end_date = input("End date in DD-MM-YYYY: ")
43+
flights = {}
44+
attractions = {}
45+
46+
print("\nNow it is time to add flights!")
47+
user_flight_choice = input("If you want to skip this step, type SKIP and press 'Enter'. Otherwise, press 'Enter'. ")
48+
flights_list_done = False
49+
50+
if user_flight_choice == "SKIP":
51+
flights = {}
52+
else:
53+
while not flights_list_done:
54+
flight_details = {}
55+
56+
departure_airport = input("Name of the airport you will depart from: ")
57+
departure_date = input("Date & time of flight departure (Format: DD-MM-YYYY HH:MM): ")
58+
arrival_airport = input("Name of the airport you will arrive at: ")
59+
arrival_date = input("Date & time of flight arrival (Format: DD-MM-YYYY HH:MM): ")
60+
flight_name = f"{departure_airport} to {arrival_airport}"
61+
62+
flight_details.update({
63+
"departure airport": departure_airport,
64+
"departure date": departure_date,
65+
"arrival airport": arrival_airport,
66+
"arrival date": arrival_date
67+
})
68+
flights[flight_name] = flight_details
69+
70+
while True:
71+
add_another_flight = input("Would you like to add another flight? Type Y (yes) or N (no): ")
72+
if add_another_flight == "Y":
73+
flights_list_done = False
74+
break
75+
elif add_another_flight == "N":
76+
flights_list_done = True
77+
break
78+
else:
79+
print("Invalid answer: Please type Y or N only.")
80+
81+
print("\nFinally: ATTRACTIONS!")
82+
user_attractions_choice = input("If you want to skip this step, type SKIP and press 'Enter'. Otherwise, press 'Enter'. ")
83+
attractions_list_done = False
84+
85+
if user_attractions_choice == "SKIP":
86+
attractions = {}
15087
else:
151-
print("Enter a number between 1-6.")
152-
else:
153-
("Invalid input: Please enter a number between 1-6.")
88+
while not attractions_list_done:
89+
attraction_details = {}
90+
91+
attraction_name = input("Name of attraction: ")
92+
attraction_address = input("Address of attraction: ")
93+
attraction_summary = input("Short description of attraction: ")
94+
attraction_type = input("(Optional) Provide some tags that categorise what kind of activity this involves.\nExample of format required: hike, exciting, views\n")
95+
attraction_tags = attraction_type.split(", ")
96+
97+
attraction_details.update({
98+
"address": attraction_address,
99+
"summary": attraction_summary,
100+
"tag(s)": attraction_tags
101+
})
102+
attractions[attraction_name] = attraction_details
103+
104+
while True:
105+
add_another_attraction = input("Would you like to add another attraction? Type Y or N:")
106+
if add_another_attraction == "Y":
107+
attractions_list_done = False
108+
break
109+
elif add_another_attraction == "N":
110+
attractions_list_done = True
111+
break
112+
else:
113+
print("Invalid answer: Please type Y or N only.")
114+
115+
add_itinerary(itineraries, name, location, summary, start_date, end_date, flights, attractions)
116+
117+
# Edit existing itinerary
118+
elif user_choice == "2":
119+
# Use pickpack module (https://github.com/anafvana/pickpack#map-function-for-nested-lists)
120+
chosen_itinerary = input("")
121+
edit_itinerary(chosen_itinerary)
122+
pass
123+
124+
# View itinerary
125+
elif user_choice == "3":
126+
chosen_itinerary = input("Please choose a trip")
127+
view_itineraries(itineraries, chosen_itinerary)
128+
pass
129+
130+
# Delete itinerary
131+
elif user_choice == "4":
132+
chosen_itinerary = input("")
133+
delete_itinerary(chosen_itinerary)
134+
pass
135+
136+
# Log out
137+
elif user_choice == "5":
138+
app_running = False
154139

155140
print("See you next time! 👋")
156-
pass
157141

158142

159143
if __name__ == "__main__":
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
flake8==7.3.0
2+
anytree==2.13.0
23
pick==v2.4.0
3-
pickpack==2.0.0
4+
pickpack==2.0.0

Travel Itinerary Planner/src/file_handler.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import pickle
21
import os
2+
import pickle
33
from pprint import pprint
4-
from src.itinerary import Itinerary
54

65
ITINERARY_FILE = "itineraries.bin"
7-
6+
# Used \\ because of this warning: https://stackoverflow.com/questions/52335970/how-to-fix-syntaxwarning-invalid-escape-sequence-in-python
7+
current_directory = f"{os.getcwd()}\\{ITINERARY_FILE}"
8+
print(current_directory)
89

910
def load_itineraries():
1011
"""
@@ -14,10 +15,10 @@ def load_itineraries():
1415
dictionary: A dictionary of Itinerary objects loaded from the binary file.
1516
If the file does not exist, an empty dictionary is returned.
1617
"""
17-
if os.path.exists(ITINERARY_FILE):
18+
if os.path.exists(current_directory):
1819
with open(ITINERARY_FILE, "rb") as file:
1920
return pickle.load(file)
20-
return {}
21+
return []
2122

2223

2324
def save_itineraries(itineraries):
@@ -31,5 +32,5 @@ def save_itineraries(itineraries):
3132
- Writes the serialized itinerary dictionary to ITINERARY_FILE.
3233
- Overwrites the file if it already exists.
3334
"""
34-
with open(ITINERARY_FILE, "wb") as file:
35+
with open(current_directory, "wb") as file:
3536
pickle.dump(itineraries, file)

0 commit comments

Comments
 (0)