106 lines
3.1 KiB
Python
106 lines
3.1 KiB
Python
class RouteManager:
|
|
def __init__(self,file="t_connections.txt"):
|
|
self.cities = []
|
|
self.connections: dict[str, list] = dict()
|
|
self.journey = []
|
|
self.f_length = 0
|
|
self.setup(file)
|
|
|
|
def add_connection(self, city, connection):
|
|
self.connections[city] = connection
|
|
|
|
def setup(self,file):
|
|
with open(file, "r") as f:
|
|
for line in f:
|
|
self.f_length += 1
|
|
|
|
f.seek(0)
|
|
for _ in range(self.f_length):
|
|
line = f.readline().strip()
|
|
split_line = line.split()
|
|
|
|
if split_line[0] not in self.cities: self.cities.append(split_line[0])
|
|
if split_line[2] not in self.cities: self.cities.append(split_line[2])
|
|
|
|
for _ in range(len(self.cities)):
|
|
self.add_connection(self.cities[_], [])
|
|
|
|
f.seek(0)
|
|
for _ in range(self.f_length):
|
|
line = f.readline().strip()
|
|
split_line = line.split()
|
|
if split_line[2] not in self.connections[split_line[0]]:
|
|
self.connections[split_line[0]].append(split_line[2])
|
|
|
|
def get_directions(self,origin,):
|
|
try:
|
|
return self.connections[origin]
|
|
except KeyError:
|
|
return "Origin not Found"
|
|
|
|
def create_route(self,origin):
|
|
if origin in self.cities:
|
|
directions = self.get_directions(origin)
|
|
l = len(directions)
|
|
|
|
if l != 0:
|
|
for _ in range(l):
|
|
print(f"{origin} -> {directions[_]}")
|
|
print()
|
|
else:
|
|
print(f"No connections from {origin}")
|
|
else:
|
|
print("Origin not Found")
|
|
|
|
def add_to_journey(self,origin,destination):
|
|
if not self.journey:
|
|
self.journey.append(origin)
|
|
self.journey.append(destination)
|
|
else: self.journey.append(destination)
|
|
|
|
def save_journey(self,journey_name):
|
|
with open(f"{journey_name}.txt", "a") as f:
|
|
f.writelines(f" > {self.journey[_]}" for _ in range (len(self.journey)))
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
rm = RouteManager()
|
|
print("Welcome to Route-Manager")
|
|
print(f"Known origins {rm.cities}")
|
|
|
|
while True:
|
|
print("1. New journey")
|
|
print("2. Load journey")
|
|
print("3. quit")
|
|
usr_input = input("1 / 2 / 3?: ")
|
|
|
|
if usr_input == "1":
|
|
while True:
|
|
usr_origin = input("Set your origin: ")
|
|
print()
|
|
rm.create_route(usr_origin)
|
|
usr_destination = input("Type in your destination: ")
|
|
rm.add_to_journey(usr_origin, usr_destination)
|
|
print()
|
|
|
|
_usr_input = input("Continue Journey? (y/n): ")
|
|
|
|
if _usr_input == "n":
|
|
usr_save = input("Save journey? (y/n): ")
|
|
|
|
if usr_save == "y":
|
|
file_name = input("Enter file name: ")
|
|
rm.save_journey(file_name)
|
|
break
|
|
|
|
if usr_input == "2":
|
|
pass
|
|
|
|
if usr_input == "3":
|
|
break
|
|
|
|
|
|
main() |