From a54a579c84bbaf9b4e92c5fc354fc0ac29396989 Mon Sep 17 00:00:00 2001 From: Axy Date: Tue, 6 Jan 2026 18:42:46 +0100 Subject: [PATCH] all except last --- ex1/ft_garden_data.py | 4 ++ ex2/ft_plant_growth.py | 4 ++ ex3/ft_plant_factory.py | 6 ++ ex4/ft_garden_security.py | 47 +++++++++++++++ ex5/ft_plant_types.py | 124 ++++++++++++++++++++++++++++++++++++++ pyproject.toml | 2 + 6 files changed, 187 insertions(+) create mode 100644 ex4/ft_garden_security.py create mode 100644 ex5/ft_plant_types.py create mode 100644 pyproject.toml diff --git a/ex1/ft_garden_data.py b/ex1/ft_garden_data.py index 206d32f..cb016e8 100644 --- a/ex1/ft_garden_data.py +++ b/ex1/ft_garden_data.py @@ -1,4 +1,8 @@ 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 self.height_cm = height_cm diff --git a/ex2/ft_plant_growth.py b/ex2/ft_plant_growth.py index 1fefef0..5769595 100644 --- a/ex2/ft_plant_growth.py +++ b/ex2/ft_plant_growth.py @@ -1,4 +1,8 @@ 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 self.height_cm = height_cm diff --git a/ex3/ft_plant_factory.py b/ex3/ft_plant_factory.py index 5f1bb25..f64a217 100644 --- a/ex3/ft_plant_factory.py +++ b/ex3/ft_plant_factory.py @@ -1,4 +1,8 @@ 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 self.height_cm = height_cm @@ -21,6 +25,8 @@ class Plant: class PlantFactory: + plants: list[Plant] + def __init__(self): self.plants = [] diff --git a/ex4/ft_garden_security.py b/ex4/ft_garden_security.py new file mode 100644 index 0000000..ae69fb5 --- /dev/null +++ b/ex4/ft_garden_security.py @@ -0,0 +1,47 @@ +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}") diff --git a/ex5/ft_plant_types.py b/ex5/ft_plant_types.py new file mode 100644 index 0000000..d94d822 --- /dev/null +++ b/ex5/ft_plant_types.py @@ -0,0 +1,124 @@ +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() diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..a8f43fe --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,2 @@ +[tool.black] +line-length = 79 -- 2.51.0