-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
77 lines (49 loc) · 1.84 KB
/
main.py
File metadata and controls
77 lines (49 loc) · 1.84 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
from typing import List, TypedDict, cast
import requests
class BattleShip(TypedDict):
name: str
numberOfGuns: float
lengthInMeters: int
helipad: bool
class CruiseShip(TypedDict):
name: str
numberOfEmergencyRIBs: int
lengthInMeters: int
features: List[str]
class CollectionOfShips(TypedDict):
battleships: List[BattleShip]
cruiseShips: List[CruiseShip]
class ShipManager:
__ships: CollectionOfShips
def __init__(self, ships: CollectionOfShips) -> None:
self.__ships = ships
def calculate_total_length_of_ships(self) -> int:
cumulative_length: int = 0
for ship in self.__ships["battleships"]:
cumulative_length += ship["lengthInMeters"]
for ship in self.__ships["cruiseShips"]:
cumulative_length += ship["lengthInMeters"]
return cumulative_length
# Finds the longest battleship
def long_ship(self) -> str:
max_length: int = 0
found_thing: str = ""
for ship in self.__ships["battleships"]:
if ship["lengthInMeters"] > max_length:
max_length = ship["lengthInMeters"]
found_thing = ship["name"]
return found_thing
class ShipManagerBuilder:
__url: str
def __init__(self, url: str) -> None:
self.__url = url
def create_ship_manager(self) -> ShipManager:
response = requests.get(self.__url)
ships: CollectionOfShips = cast(CollectionOfShips, response.json())
return ShipManager(ships)
def run():
smb = ShipManagerBuilder("https://lemon-pond-0c20b7303.1.azurestaticapps.net/ships.json")
shipManager: ShipManager = smb.create_ship_manager()
print(f"The longest battleship is {shipManager.long_ship()}")
print(f"The total length of all the ships is {shipManager.calculate_total_length_of_ships()}")
run()