-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpoo_ex1.py
More file actions
147 lines (107 loc) · 4.03 KB
/
Copy pathpoo_ex1.py
File metadata and controls
147 lines (107 loc) · 4.03 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from abc import ABC, abstractmethod
# ------------------------------------------------------------
# Stage 1 – Base Class: Vehicle (Abstract)
# ------------------------------------------------------------
class Vehicle(ABC):
def __init__(self, brand, model, year, base_price):
self.brand = brand
self.model = model
self.year = year
self.base_price = base_price
def describe(self):
return f"{self.__class__.__name__} - {self.brand} {self.model} ({self.year})"
@abstractmethod
def compute_value(self):
pass
@abstractmethod
def maintenance_cost(self):
pass
# ------------------------------------------------------------
# Stage 2 – Car, Motorcycle, Truck
# Depreciation logic:
# Car: 5% per year
# Motorcycle: 7% per year
# Truck: 3% per year
# ------------------------------------------------------------
class Car(Vehicle):
def compute_value(self):
age = 2025 - self.year
return max(0, self.base_price * ((1 - 0.05) ** age))
def maintenance_cost(self):
return 2000 # simple fixed estimate
class Motorcycle(Vehicle):
def compute_value(self):
age = 2025 - self.year
return max(0, self.base_price * ((1 - 0.07) ** age))
def maintenance_cost(self):
return 1000
class Truck(Vehicle):
def compute_value(self):
age = 2025 - self.year
return max(0, self.base_price * ((1 - 0.03) ** age))
def maintenance_cost(self):
return 3000
# ------------------------------------------------------------
# Stage 3 – Polymorphism: total fleet value
# ------------------------------------------------------------
def calculate_total_fleet_value(fleet_list):
total = 0
for v in fleet_list:
total += v.compute_value()
return total
# ------------------------------------------------------------
# Stage 4 – Advanced: ElectricCar subclass
# Depreciation: normal car depreciation + battery factor
# ------------------------------------------------------------
class ElectricCar(Car):
def __init__(self, brand, model, year, base_price, battery_health):
super().__init__(brand, model, year, base_price)
self.battery_health = battery_health # from 0 to 100
def compute_value(self):
base_value = super().compute_value()
battery_factor = self.battery_health / 100
return base_value * battery_factor
def maintenance_cost(self):
return 1500 # EV maintenance is lower
# ------------------------------------------------------------
# Stage 5 – Optional Extension: FleetManager
# ------------------------------------------------------------
class FleetManager:
def __init__(self):
self.vehicles = []
def add_vehicle(self, vehicle):
self.vehicles.append(vehicle)
def remove_vehicle(self, vehicle):
if vehicle in self.vehicles:
self.vehicles.remove(vehicle)
def list_fleet(self):
for v in self.vehicles:
print(v.describe())
def search(self, brand):
return [v for v in self.vehicles if v.brand.lower() == brand.lower()]
def total_value(self):
return calculate_total_fleet_value(self.vehicles)
# ------------------------------------------------------------
# Test Code
# ------------------------------------------------------------
if __name__ == "__main__":
# Vehicles
car1 = Car("Toyota", "Corolla", 2020, 150000)
moto1 = Motorcycle("Yamaha", "R3", 2022, 70000)
truck1 = Truck("Volvo", "FH16", 2018, 500000)
ev1 = ElectricCar("Tesla", "Model 3", 2021, 300000, battery_health=85)
# Fleet manager
fm = FleetManager()
fm.add_vehicle(car1)
fm.add_vehicle(moto1)
fm.add_vehicle(truck1)
fm.add_vehicle(ev1)
print("Fleet list:")
fm.list_fleet()
print("\nTotal fleet value:", fm.total_value())
print("\nSearch for 'Tesla':")
for v in fm.search("Tesla"):
print(v.describe())
print("\nMaintenance costs:")
for v in fm.vehicles:
print(v.describe(), "->", v.maintenance_cost())