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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
#!/usr/bin/env python3
# Create a class
class Plant():
# Create attributes of class Plant
def __init__(self: 'Plant', name: str, height: float, age: int) -> None:
if not all([name, age, height]):
self.anon()
else:
self.name_is = name
self._height_is = height
self._age_is = age
# instantiating an object of Stat by append brackets to Stat
self.stat = self.Stat()
# if None is passed as a parameter
def anon(self: 'Plant') -> None:
self.name_is = "Unknown plant"
self._height_is = 0
self._age_is = 0
self.stat = self.Stat()
# Create a nested class
class Stat:
def __init__(self) -> None:
# objects attributes
self._age_counter = 0
self._grow_counter = 0
self._show_counter = 0
self.produce_shade_counter = 0
def counter(self) -> None:
self._age_counter += 1
self._grow_counter += 1
self._show_counter += 1
self.produce_shade_counter += 1
# Method to grow the age
def age(self: 'Plant') -> None:
self._age_is += 1
self.stat._age_counter += 1
# Method to grow the height
def grow(self: 'Plant') -> None:
self._height_is *= 1.02
self.stat._grow_counter += 1
# Getter methods to access private properties
def get_age(self: 'Plant') -> int:
return self._age_is
def get_height(self: 'Plant') -> float:
return self._height_is
# Setter methods to modify properties
def set_age(self: 'Plant', age: int) -> None:
if age > 0:
self._age_is = age
print("Plant age was updated.")
else:
print("Plant age cannot be negative.")
def set_height(self: 'Plant', height: int) -> None:
if height > 0:
self._height_is = round(height)
print("Plant height was updated.")
else:
print("Plant height cannot be negative.")
# static method does not take object
@staticmethod
def check_age(a) -> bool:
return a >= 365
# Method to show the plant information
def show(self: 'Plant') -> None:
print(f" {self.name_is}: {str(round(self.get_height(), 1))}cm,"
f" {str(self.get_age())} days old")
self.stat._show_counter += 1
@classmethod
def create_anonymous(cls) -> 'Plant':
return cls("Unknown plant", 0, 0)
def show(stat) -> None:
print(f" Stats: {stat._grow_counter} grow,",
f"{stat._age_counter} age,",
f"{stat._show_counter} show")
class Flower(Plant):
def __init__(self: 'Flower', name: str, height: int,
age: int, color: str) -> None:
super(). __init__(name, height, age)
# attributes
self.color = color
self.isblooming = 0
def bloom(self: 'Flower') -> None:
self.isblooming = 1
def show(self: 'Flower') -> None:
super().show()
print(f" Color: {self.color}")
if self.isblooming == 1:
print(f" {self.name_is} is blooming beautifully!")
else:
print(f" {self.name_is} has not bloomed yet")
class Seed(Flower):
def __init__(self: 'Seed', name: str, height: int,
age: int, color: str) -> None:
super(). __init__(name, height, age, color)
self.color = color
self.has_seed = 0
def show(self: 'Seed') -> None:
super().show()
if self.isblooming == 1:
self.has_seed = 42
print(f" Seeds: {self.has_seed}")
else:
print(" Seeds: 0")
class Tree(Plant):
def __init__(self: 'Tree', name: str, height: int,
age: int, trunk_diameter: int) -> None:
super(). __init__(name, height, age)
self.trunk_diameter = trunk_diameter
self.is_producing_shade = 0
def produce_shade(self: 'Tree') -> None:
self.is_producing_shade = 1
self.stat.produce_shade_counter += 1
print(f" {self.name_is} now produces a shade of,",
f"{str(self.get_height())}cm long",
f"and {str(round(self.trunk_diameter, 1))}cm wide.")
def show(self: 'Tree') -> None:
super().show()
print(f" Trunk diameter: {self.trunk_diameter}cm")
def stats(self: 'Tree') -> None:
print(f" Stats: {self.stat._grow_counter} grow,",
f"{self.stat._age_counter} age,",
f"{self.stat._show_counter} show,",
f"{self.stat.produce_shade_counter} shade")
class Vegetable(Plant):
def __init__(self: 'Vegetable', name: str, height: int, age: int,
harvest_season: int, nutritional_value: int) -> None:
super(). __init__(name, height, age)
self.harvest_season_is = harvest_season
self.nutritional_value_is = nutritional_value
def update_nutritional_value(self: 'Vegetable') -> None:
self.nutritional_value_is = 20
def show(self: 'Vegetable') -> None:
super().show()
print(f" Harvest season: {self.harvest_season_is}")
if self.get_age() >= 20:
self.update_nutritional_value()
print(f" Nutritional value: {self.nutritional_value_is}")
if __name__ == "__main__":
print("=== Garden Statistics ===")
print("=== Check year-old")
rose = Flower("Rose", 5, 30, "red")
print(" Is 30 days more than a year?",
f"-> {Plant.check_age(30)}")
rose = Flower("Rose", 5, 400, "red")
print(" Is 400 days more than a year?",
f"-> {Plant.check_age(444)}")
print()
print("=== Flower")
rose = Flower("Rose", 5, 15, "red")
rose.show()
print(" [statistics for Rose]")
show(rose.stat)
print(" [asking the rose to grow and bloom]")
rose.bloom()
rose.grow()
rose.show()
print(" [statistics for Rose]")
show(rose.stat)
print()
oak = Tree("Oak", 200, 365, 5)
print("=== Tree")
oak.show()
print(" [statistics for Oak]")
oak.stats()
print(" [asking the oak to produce shade]")
oak.produce_shade()
print(" [statistics for Oak]")
oak.stats()
print()
print("=== Seed")
sunflower = Seed("Sunflower", 80, 45, "yellow")
sunflower.show()
print(" [statistics for Sunflower]")
show(sunflower.stat)
print(" [make sunflower grow, age and bloom]")
sunflower.grow()
sunflower.bloom()
sunflower.age()
sunflower.show()
show(sunflower.stat)
print()
print("=== Anonymous")
anonymous = Plant.create_anonymous()
anonymous.show()
print(" [statistics for Unknown plant]")
show(anonymous.stat)
|