75 lines
2.1 KiB
Python
75 lines
2.1 KiB
Python
class Trains:
|
|
def __init__(self):
|
|
self.cities = []
|
|
self.connections: dict[str, list] = dict()
|
|
self.f_length = 0
|
|
self.setup()
|
|
|
|
def add_connection(self, city, connection):
|
|
self.connections[city] = connection
|
|
|
|
def setup(self):
|
|
with open("t_connections.txt", "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 main():
|
|
trains = Trains()
|
|
print(trains.cities)
|
|
print(trains.connections)
|
|
|
|
print("Welcome to Route-Manager")
|
|
print(f"Known origins {trains.cities}")
|
|
|
|
while True:
|
|
print("1. Proceed to route search")
|
|
print("2. quit")
|
|
usr_input = input("1 / 2 ?: ")
|
|
|
|
if usr_input == "1":
|
|
usr_origin = input("Set your origin: ")
|
|
|
|
if usr_origin in trains.cities:
|
|
directions = trains.get_directions(usr_origin)
|
|
l = len(directions)
|
|
|
|
if l != 0:
|
|
for _ in range(l):
|
|
print(f"{usr_origin} -> {directions[_]}")
|
|
|
|
else:
|
|
print(f"No connections from {usr_origin}")
|
|
|
|
else:
|
|
print("Origin not Found")
|
|
|
|
if usr_input == "2":
|
|
break
|
|
|
|
|
|
main() |