--- /dev/null
+class SecurePlant:
+ __name: str
+ __height_cm: int = 0
+ __age_days: int = 0
+
+ def __init__(self, name: str, height_cm: int, age_days: int) -> None:
+ self.__name = name.capitalize()
+ print(f"Plant created: {self.__name}")
+ self.set_height(height_cm)
+ self.set_age(age_days)
+
+ def __repr__(self) -> str:
+ return f"{self.__name}: ({self.__height_cm}cm, {self.__age_days} days)"
+
+ @staticmethod
+ def __reject_with_message(operation: str, msg: str):
+ print(f"\nInvalid operation attempted: {operation} [REJECTED]")
+ print(f"Security: {msg}")
+
+ @staticmethod
+ def __accept_update(field: str, value: str):
+ print(f"{field} updated: {value} [OK]")
+
+ def set_height(self, height_cm: int):
+ if height_cm < 0:
+ self.__reject_with_message(
+ f"height {height_cm}cm", "Negative height rejected"
+ )
+ else:
+ self.__accept_update("Height", f"{height_cm}cm")
+ self.__height_cm = height_cm
+
+ def set_age(self, age_days: int):
+ if age_days < 0:
+ self.__reject_with_message(
+ f"age {age_days} days", "Negative age rejected"
+ )
+ else:
+ self.__accept_update("Age", f"{age_days} days")
+ self.__age_days = age_days
+
+
+if __name__ == "__main__":
+ print("=== Garden Security System ===")
+ plant = SecurePlant("rose", 25, 30)
+ plant.set_height(-5)
+ print(f"\nCurrent plant: {plant}")
--- /dev/null
+class Plant:
+ name: str
+ height_cm: int
+ age_days: int
+
+ def __init__(self, name: str, height_cm: int, age_days: int) -> None:
+ self.name = name.capitalize()
+ self.height_cm = height_cm
+ self.age_days = age_days
+
+ def get_kind(self) -> str:
+ return "Plant"
+
+ def get_attributes(self) -> list[str]:
+ return [f"{self.height_cm}cm", f"{self.age_days} days"]
+
+ def __repr__(self) -> str:
+ def join(sep: str, elements: list[str]) -> str:
+ res: str | None = None
+ for s in elements:
+ if res is None:
+ res = s
+ else:
+ res += sep + s
+ if res is not None:
+ return res
+ return ""
+
+ return f"{self.name} ({self.get_kind()}): " + join(
+ ", ", self.get_attributes()
+ )
+
+ def action(self):
+ pass
+
+
+class Flower(Plant):
+ color: str
+
+ def __init__(self, name: str, height_cm: int, age_days: int, color: str):
+ super().__init__(name, height_cm, age_days)
+ self.color = color
+
+ def get_kind(self) -> str:
+ return "Flower"
+
+ def get_attributes(self) -> list[str]:
+ return super().get_attributes() + [f"{self.color} color"]
+
+ def bloom(self):
+ print(f"{self.name} is blooming beautifully!")
+
+ def action(self):
+ self.bloom()
+
+
+class Tree(Plant):
+ trunk_diameter_cm: int
+
+ def __init__(
+ self, name: str, height_cm: int, age_days: int, trunk_diameter_cm: int
+ ):
+ super().__init__(name, height_cm, age_days)
+ self.trunk_diameter_cm = trunk_diameter_cm
+
+ def get_kind(self) -> str:
+ return "Tree"
+
+ def get_attributes(self) -> list[str]:
+ return super().get_attributes() + [
+ f"{self.trunk_diameter_cm}cm diameter"
+ ]
+
+ def produce_shade(self):
+ shade = round(self.trunk_diameter_cm * self.height_cm / 3.2 / 100)
+ print(f"{self.name} provides {shade} square meters of shade")
+
+ def action(self):
+ self.produce_shade()
+
+
+class Vegetable(Plant):
+ harvest_season: str
+ nutritional_value: str
+
+ def __init__(
+ self,
+ name: str,
+ height_cm: int,
+ age_days: int,
+ harvest_season: str,
+ nutritional_value: str,
+ ):
+ super().__init__(name, height_cm, age_days)
+ self.harvest_season = harvest_season
+ self.nutritional_value = nutritional_value
+
+ def get_kind(self) -> str:
+ return "Vegetable"
+
+ def get_attributes(self) -> list[str]:
+ return super().get_attributes() + [f"{self.harvest_season} harvest"]
+
+ def nutritional_info(self):
+ print(f"{self.name} is rich in {self.nutritional_value}")
+
+ def action(self):
+ self.nutritional_info()
+
+
+if __name__ == "__main__":
+ print("=== Garden Plant Types ===")
+ plants: list[Plant] = [
+ Flower("rose", 25, 30, "red"),
+ Tree("oak", 500, 1825, 50),
+ Vegetable("tomato", 80, 90, "summer", "vitamin C"),
+ Flower("dandelion", 10, 20, "yellow"),
+ Tree("cherry tree", 410, 2450, 27),
+ Vegetable("pumkin", 95, 110, "fall", "vitamin A"),
+ ]
+ for plant in plants:
+ print()
+ print(plant)
+ plant.action()