-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
221 lines (179 loc) · 8.03 KB
/
main.py
File metadata and controls
221 lines (179 loc) · 8.03 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
"""
Name: Max Sealey || Student ID: 010332991
WGU C950 Final Project: GPS Routing Program
"""
import csv
import datetime
import dist_utility
import time_utility
from custom_classes import Package
from custom_classes import Truck
from custom_classes import HashTable
"""
load_packages()
Params: list of packages from csv file, hash table
Returns: N/A
Loops through the list of packages, assigns each attribute to a temporary
variable, creates a new Package object using that data, and inserts into the
hash table
Called in main.py run_program()
Time Complexity: O(n)
"""
def load_packages(pkgs, table):
# Loop through list
for p in pkgs:
# temp variables
id = int(p[0])
address = p[1]
city = p[2]
state = p[3]
zip = p[4]
deadline = p[5]
weight = p[6]
notes = p[7]
# Create new Package object and insert into the custom hash table
pkg = Package.Package(id, address, city, state, zip, deadline, weight, notes)
table.insert(id, pkg)
"""
run program()
Params: N/A
Returns: N/A
Core algorithm called to execute the program. Opens the csv files, creates the hash table,
loads the hash table with package data, creates and loads the truck, calls the function to deliver
the packages, runs the CLI
Calls:
- load_packages()
- time_utility.determine_pkg_status()
- time_utility.get_status_time()
"""
def run_program():
# Open each of the csv files, extract lists of data and assign to variables
with open('./csv_data/packages.csv') as pkg_data:
package_list = list(csv.reader(pkg_data, delimiter=','))
with open('./csv_data/addresses.csv') as addresses:
address_list = list(csv.reader(addresses, delimiter=','))
with open('./csv_data/distances.csv') as distances:
distance_list = list(csv.reader(distances, delimiter=','))
# Create the chaining hash table used to hold package data
hash_table = HashTable.HashTable()
# Load hash table with packages
load_packages(package_list, hash_table)
# Created truck objects
truck1 = Truck.Truck(1, "HUB", datetime.timedelta(hours=8),
[1, 13, 14, 15, 16, 17, 19, 20, 21, 29, 30, 34, 37, 39, 40])
truck2 = Truck.Truck(2, "HUB", datetime.timedelta(hours=9, minutes=5),
[3, 4, 5, 6, 18, 24, 25, 26, 31, 36, 38])
truck3 = Truck.Truck(3, "HUB", datetime.timedelta(hours=10, minutes=30),
[2, 7, 8, 9, 10, 11, 12, 22, 23, 27, 28, 32, 33, 35])
# Execute functions to deliver the packages for each truck
dist_utility.deliver_packages(hash_table, distance_list, address_list, truck1)
dist_utility.deliver_packages(hash_table, distance_list, address_list, truck2)
dist_utility.deliver_packages(hash_table, distance_list, address_list, truck3)
# Loops while user is operating the program
while True:
res = input(
'''
--------------------------------------------------------------
Welcome to the WGUPS Routing System
--------------------------------------------------------------
If you would like to view the delivery times of all packages,
type '1' and hit enter.
If you would like to get the status of all packages at a
particular time, type '2' and hit enter.
If you would like to get the status of a single package at a
particular time, type '3' and hit enter.
If you would like to view the mileage of our delivery trucks,
type '4' and hit enter.
Type 'q' and hit enter to quit.
'''
)
# Exits program
if res == 'q':
break
# Continues loop if input invalid, back to main menu
elif res != '1' and res != '2' and res != '3' and res != '4':
print("Please enter a valid input.")
continue
# See status of all packages; goes through all packages in the hash table; O(n^2)
elif res == '1':
try:
for i in range(len(package_list)):
p = hash_table.look_up(i + 1)
status = time_utility.determine_pkg_status(hash_table, time, i + 1)
print(f"Package ID: {p.id}\n"
f"Truck ID: {p.truck_id}\n"
f"Deadline: {p.deadline}\n"
f"Delivered to: {p.address}, {p.city}, {p.state} {p.zip}\n"
f"Delivered at: {p.delivered_time}\n")
# Back to main menu or quit
if input("Press any key and 'enter' to continue or 'q' to quit: ") == 'q':
exit()
except ValueError:
print("Please enter a valid input.")
continue
# View status of all packages at a certain time; O(n^2)
elif res == '2':
try:
time = input(
'''
Please enter the time at which you would like to check
the package status (format: HH:MM:SS). Time be within the
bounds of the workday (8:00:00 - 17:00:00).
''')
(h, m, s) = time.split(":")
time = datetime.timedelta(hours=int(h), minutes=int(m), seconds=int(s))
# Loop through and print data for each
for i in range(len(package_list)):
p = hash_table.look_up(i + 1)
status = time_utility.determine_pkg_status(hash_table, time, i + 1)
print(f"Package ID: {p.id} || "
f"Address: {p.address}, {p.city}, {p.state} {p.zip} || "
f"Truck ID: {p.truck_id} || "
f"Status: {status}")
print(time_utility.get_status_time(status, time, hash_table.look_up(i + 1).delivered_time))
# Back to main menu or quit
if input("Press any key and 'enter' to continue or 'q' to quit: ") == 'q':
exit()
except ValueError:
print("Please enter a valid input.")
continue
# See status of a single package at a certain time; O(n)
elif res == '3':
try:
pkg_id = int(input(
'''
Please enter the id number of the package as an integer 1-40.
'''))
time = input(
'''
Please enter the time at which you would like to check
the package status (format: HH:MM:SS). Time be within the
bounds of the workday (8:00:00 - 17:00:00).
''')
(h, m, s) = time.split(":")
time = datetime.timedelta(hours=int(h), minutes=int(m), seconds=int(s))
status = time_utility.determine_pkg_status(hash_table, time, pkg_id)
print(f"Status of Package {pkg_id}: {status}\n"
f"Truck ID: {hash_table.look_up(pkg_id).truck_id}\n"
f"Address: {hash_table.look_up(pkg_id).address}, {hash_table.look_up(pkg_id).city}, {hash_table.look_up(pkg_id).state} {hash_table.look_up(pkg_id).zip}")
print(time_utility.get_status_time(status, time, hash_table.look_up(pkg_id).delivered_time))
# Back to main menu or quit
if input("Press any key and 'enter' to continue or 'q' to quit: ") == 'q':
exit()
except ValueError:
print("Please enter a valid input.")
continue
# View individual and total truck mileage
else:
print(f"Truck ID: {truck1.truck_id}\n"
f"Mileage: {truck1.miles}\n\n"
f"Truck ID: {truck2.truck_id}\n"
f"Mileage: {truck2.miles}\n\n"
f"Truck ID: {truck3.truck_id}\n"
f"Mileage: {truck3.miles}\n"
f"--------------------------------------------\n"
f"Total Miles Traveled: {round(truck1.miles + truck2.miles + truck3.miles, 2)}")
# Back to main menu or quit
if input("Press any key and 'enter' to continue or 'q' to quit: ") == 'q':
exit()
run_program()