218 lines
5.5 kB
1class Plant:
2 class _Stats:
3 def __init__(self) -> None:
4 self._grow_calls = 0
5 self._age_calls = 0
6 self._show_calls = 0
7
8 def add_grow(self) -> None:
9 self._grow_calls += 1
10
11 def add_age(self) -> None:
12 self._age_calls += 1
13
14 def add_show(self) -> None:
15 self._show_calls += 1
16
17 def display(self) -> None:
18 print(
19 f"Stats: {self._grow_calls} grow, "
20 f"{self._age_calls} age, "
21 f"{self._show_calls} show"
22 )
23
24 def __init__(self, name: str, height: float, age: int) -> None:
25 self._name = name
26
27 if height < 0:
28 print(f"{self._name}: Error, height can't be negative")
29 self._height = 0.0
30 else:
31 self._height = height
32
33 if age < 0:
34 print(f"{self._name}: Error, age can't be negative")
35 self._age = 0
36 else:
37 self._age = age
38
39 self._stats = self._Stats()
40
41 @staticmethod
42 def older_than_year(days: int) -> bool:
43 return days > 365
44
45 @classmethod
46 def anonymous(cls):
47 return cls("Unknown plant", 0.0, 0)
48
49 def get_name(self) -> str:
50 return self._name
51
52 def get_height(self) -> float:
53 return self._height
54
55 def set_height(self, height: float) -> bool:
56 if height < 0:
57 print(f"{self._name}: Error, height can't be negative")
58 print("Height update rejected")
59 return False
60 self._height = height
61 return True
62
63 def get_age(self) -> int:
64 return self._age
65
66 def set_age(self, age: int) -> bool:
67 if age < 0:
68 print(f"{self._name}: Error, age can't be negative")
69 print("Age update rejected")
70 return False
71 self._age = age
72 return True
73
74 def grow(self, growth_rate: float) -> None:
75 if growth_rate > 0:
76 self._height += growth_rate
77 self._stats.add_grow()
78
79 def age(self, days: int) -> None:
80 if days > 0:
81 self._age += days
82 self._stats.add_age()
83
84 def show(self) -> None:
85 self._stats.add_show()
86 print(f"{self._name}: {self._height:.1f}cm, {self._age} days old")
87
88 def display_stats(self) -> None:
89 self._stats.display()
90
91
92class Flower(Plant):
93 def __init__(self, name: str, height: float, age: int, color: str) -> None:
94 super().__init__(name, height, age)
95 self._color = color
96 self._has_bloomed = False
97
98 def bloom(self) -> None:
99 self._has_bloomed = True
100
101 def show(self) -> None:
102 super().show()
103 print(f" Color: {self._color}")
104
105 if self._has_bloomed:
106 print(f" {self._name} is blooming beautifully!")
107 else:
108 print(f" {self._name} has not bloomed yet")
109
110
111class Seed(Flower):
112 def __init__(self, name: str, height: float, age: int, color: str) -> None:
113 super().__init__(name, height, age, color)
114 self._seeds = 0
115
116 def bloom(self) -> None:
117 super().bloom()
118 self._seeds = 42
119
120 def show(self) -> None:
121 super().show()
122 print(f" Seeds: {self._seeds}")
123
124
125class Tree(Plant):
126 _stats: "_TreeStats"
127
128 class _TreeStats(Plant._Stats):
129 def __init__(self) -> None:
130 super().__init__()
131 self._shade_calls = 0
132
133 def add_shade(self) -> None:
134 self._shade_calls += 1
135
136 def display(self) -> None:
137 super().display()
138 print(f" {self._shade_calls} shade")
139
140 def __init__(self, name: str, height: float,
141 age: int, trunk_diameter: float) -> None:
142 super().__init__(name, height, age)
143 self._trunk_diameter = trunk_diameter
144 self._stats = self._TreeStats()
145
146 def produce_shade(self) -> None:
147 self._stats.add_shade()
148 print(
149 f"Tree {self._name} now produces a shade "
150 f"of {self._height:.1f}cm long and "
151 f"{self._trunk_diameter:.1f}cm wide."
152 )
153
154 def show(self) -> None:
155 super().show()
156 print(f" Trunk diameter: {self._trunk_diameter:.1f}cm")
157
158
159def display_plant_statistics(plant: Plant) -> None:
160 print(f"[statistics for {plant._name}]")
161 plant.display_stats()
162
163
164def main():
165 print("=== Garden statistics ===")
166 print("=== Check year-old")
167 print(f"Is 30 days more than a year? -> {Plant.older_than_year(30)}")
168 print(f"Is 400 days more than a year? -> {Plant.older_than_year(400)}")
169
170 # Flower
171 print("\n=== Flower")
172 rose = Flower("Rose", 15, 10, "red")
173 rose.show()
174
175 display_plant_statistics(rose)
176
177 print("[asking the rose to grow and bloom]")
178 rose.grow(8)
179 rose.bloom()
180 rose.show()
181
182 display_plant_statistics(rose)
183
184 # Tree
185 print("\n=== Tree")
186 oak = Tree("Oak", 200, 365, 5)
187 oak.show()
188
189 display_plant_statistics(oak)
190
191 print("[asking the oak to produce shade]")
192 oak.produce_shade()
193
194 display_plant_statistics(oak)
195
196 # Seed
197 print("\n=== Seed")
198 sunflower = Seed("Sunflower", 80, 45, "yellow")
199 sunflower.show()
200
201 print("[make sunflower grow, age and bloom]")
202 sunflower.grow(30)
203 sunflower.age(20)
204 sunflower.bloom()
205 sunflower.show()
206
207 display_plant_statistics(sunflower)
208
209 # Anonymous
210 print("\n=== Anonymous")
211 unknown = Plant.anonymous()
212
213 unknown.show()
214 display_plant_statistics(unknown)
215
216
217if __name__ == "__main__":
218 main()