Ejemplo n.º 1
0
 def test_delete_city_from_json_data(self):
   json_data = parse_json("mockJsonText.txt")
   print("\n\nCITY DELETION\n----------------")
   print("Before deletion:")
   print(json_data["metros"])
   result = jsonUtility.delete_city_from_json_data(json_data, 'LON')
   print("After deletion:")
   print(json_data["metros"])
   if not result:
     self.fail()
Ejemplo n.º 2
0
def handle_admin_option(json_data, dict_of_edges, dict_of_cities, graph):
    """
    Handles all admin stuff like add, delete, edit city/routes
    :param json_data:
    :param dict_of_edges:
    :param dict_of_cities:
    :param graph:
    :return: Nothing
    """
    print("\n---------------------------------------------\n")
    print("Admin Options")
    print("\n---------------------------------------------\n")
    print("\nCurrent options:")
    print("\n1. Add a City")
    print("\n2. Add a Route")
    print("\n3. Edit a City")
    print("\n4. Remove a City")
    print("\n5. Remove a Route")
    print("\n6. Go back to previous menu")

    admin_choice = input("\n\nPlease enter the number of your choice: ")

    if int(admin_choice) == 1:
        print("ADD A NEW CITY")
        print("---------------")
        code = input("Enter New City Code: (like NYC) ")
        name = input("Enter New City Name: (like New York) ")
        country = input("Enter New City Country: (like US) ")
        continent = input("Enter New City Continent: (like North America) ")
        timezone = input("Enter New City Time Zone: (like -5) ")
        coordinates = input("Enter New City Coordinates: (like {'N'' : 41, 'W' : 74}) ")
        population = input("Enter New City Population: (like 20000000) ")
        region = input("Enter New City Region: (like 3) ")

        if not str(population).isdigit() or int(population) < 0:
            print("Population invalid!")
            return

        # if not str(timezone).isnumeric():
        #     print("TimeZone invalid!")
        #     return

        if not str(region).isdigit() or int(region) < 0:
            print("Region invalid!")
            return

        City.add_city(dict_of_cities,code,name,country,continent,timezone,coordinates,population,region)
        jsonUtility.add_city_to_json_data(json_data,code,name,country,continent,timezone,coordinates,population,region)

    if int(admin_choice) == 2:
        print("ADD A NEW ROUTE")
        print("---------------")

        source = input("Enter Source Code or Name: (like NYC or New York City) ")
        source = validate_city_name_or_code(source, dict_of_cities)
        if not source:
            return

        destination = input("Enter Destination Code or Name: (like LON or London) ")
        destination = validate_city_name_or_code(destination, dict_of_cities)
        if not destination:
            return

        distance = input("Enter Distance (in mi): (like 12345) ")
        if not str(distance).isnumeric() or int(distance) < 0:
            print("Distance invalid!")
            return

        Edge.add_edge(dict_of_edges,source,destination,distance)
        jsonUtility.add_route_to_json_data(json_data,source,destination,distance)

    if int(admin_choice) == 3:
        print("EDIT CITY")
        print("---------------")
        city_to_edit = input("Enter City Code or Name: (like NYC or New York City) ")
        city_to_edit = validate_city_name_or_code(city_to_edit, dict_of_cities)
        if not city_to_edit:
            return
        print("For the following prompts, enter NA if you don't want to change.\n")
        name = input("Enter New City Name: (like New York) ")
        country = input("Enter New City Country: (like US) ")
        continent = input("Enter New City Continent: (like North America) ")
        timezone = input("Enter New City Time Zone: (like -5) ")
        coordinates = input("Enter New City Coordinates: (like {'N'' : 41, 'W' : 74}) ")
        population = input("Enter New City Population: (like 20000000) ")
        region = input("Enter New City Region: (like 3) ")

        city = dict_of_cities[city_to_edit]
        if not name == 'NA':
            city.name = name
        if not country == 'NA':
            city.country = country
        if not continent == 'NA':
            city.continent = continent
        if not timezone == 'NA':
            city.timezone = timezone
        if not coordinates == 'NA':
            city.coordinates = coordinates
        if not population == 'NA':
            city.population = population
        if not region == 'NA':
            city.region = region

        # change things in the json_data - by deleting old city and adding new one
        jsonUtility.delete_city_from_json_data(json_data,city_to_edit)
        jsonUtility.add_city_to_json_data(json_data,city.code,city.name,city.country,city.continent,city.timezone,city.coordinates,city.population,city.region)
        return

    if int(admin_choice) == 4:
        print("REMOVE CITY")
        print("---------------")
        city_to_remove = input("Enter City Code or Name: (like NYC or New York City) ")
        city_to_remove = validate_city_name_or_code(city_to_remove, dict_of_cities)
        if not city_to_remove:
            return
        jsonUtility.delete_city_from_json_data(json_data,city_to_remove)
        Edge.remove_city_edges(dict_of_edges, city_to_remove)
        City.remove_city(dict_of_cities,city_to_remove)

    if int(admin_choice) == 5:
        print("REMOVE ROUTE")
        print("---------------")
        edge_string = input("Enter SourceCode-DestinationCode: (like NYC-LON) ")
        result = Edge.remove_edge(dict_of_edges,edge_string)
        if not result:
            print("Either route format was invalid or route does not exist.")
        else:
            print("Route successfully deleted.")