-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcar.py
More file actions
93 lines (66 loc) · 3.13 KB
/
car.py
File metadata and controls
93 lines (66 loc) · 3.13 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
from abc import ABC, abstractmethod
class Transport(ABC):
def __init__(self, name: str, speed: int, capacity: int):
self.name = name
self.speed = speed
self.capacity = capacity
@abstractmethod
def move(self, distance: int) -> float:
pass
@abstractmethod
def fuel_consumption(self, distance: int) -> float:
pass
@abstractmethod
def info(self) -> str:
pass
def calculate_cost(self, distance: int, price_per_unit: float) -> float:
total_units = self.fuel_consumption(distance)
return total_units * price_per_unit
class Car(Transport):
def __init__(self, name: str, speed: int, capacity: int):
super().__init__(name, speed, capacity)
self.fuel_rate = 0.07
def move(self, distance: int) -> float:
return distance / self.speed
def fuel_consumption(self, distance: int) -> float:
return distance * self.fuel_rate
def info(self) -> str:
return f"{self.name} - Легковий автомобіль. Шв: {self.speed} км/год, Місця: {self.capacity}."
class Bus(Transport):
def __init__(self, name: str, speed: int, capacity: int, passengers: int):
super().__init__(name, speed, capacity)
self.passengers = passengers
self.fuel_rate = 0.15
def move(self, distance: int) -> float:
return distance / self.speed
def fuel_consumption(self, distance: int) -> float:
if self.passengers > self.capacity:
print(f" {self.name}: Перевантажено! (Пасажирів: {self.passengers}, Місць: {self.capacity})")
return 0.0
return distance * self.fuel_rate
def info(self) -> str:
status = "Перевантажений" if self.passengers > self.capacity else "Норма"
return f"{self.name} - Автобус. Шв: {self.speed} км/год, Пасажири: {self.passengers}/{self.capacity} ({status})."
class Bicycle(Transport):
MAX_SPEED = 20
def move(self, distance: int) -> float:
actual_speed = min(self.speed, self.MAX_SPEED)
return distance / actual_speed
def fuel_consumption(self, distance: int) -> float:
return 0.0
def info(self) -> str:
speed_limit = min(self.speed, self.MAX_SPEED)
return f"{self.name} - Велосипед. Макс. шв: {speed_limit} км/год. Пального не потребує."
class ElectricCar(Car):
def __init__(self, name: str, speed: int, capacity: int):
super().__init__(name, speed, capacity)
self.battery_rate = 0.2
def fuel_consumption(self, distance: int) -> float:
return 0.0
def battery_usage(self, distance: int) -> float:
return distance * self.battery_rate
def info(self) -> str:
return f"{self.name} - Електромобіль. Шв: {self.speed} км/год. Витрати: {self.battery_rate} кВт⋅год/км."
def calculate_cost(self, distance: int, price_per_unit: float) -> float:
total_units = self.battery_usage(distance)
return total_units * price_per_unit