diff options
Diffstat (limited to 'py01/ex4')
| -rwxr-xr-x | py01/ex4/ft_garden_security.py | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/py01/ex4/ft_garden_security.py b/py01/ex4/ft_garden_security.py new file mode 100755 index 0000000..ef983b9 --- /dev/null +++ b/py01/ex4/ft_garden_security.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 + + +# Create a class +class Plant(): + + def __init__(self: 'Plant', name: str, height: float, age: int) -> None: + if age < 0: + print("Plant age cannot be negative.") + print("Plant age rejected.") + print("Plant age was set to 0.") + self.name_is = name + self._height_is = height + self._age_is = 0 + if height < 0: + print("Plant height cannot be negative.") + print("Plant height rejected.") + print("Plant height was set to 0.") + self.name_is = name + self._height_is = 0 + self._age_is = age + if height > 0 and age > 0: + self.name_is = name + self._height_is = height + self._age_is = age + +# Method to grow the age + def grow_age(self: 'Plant') -> None: + self._age_is += 1 + +# Method to grow the height + def grow_height(self: 'Plant') -> None: + self._height_is *= 1.04 + +# 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.") + print("Plant age rejected.") + + def set_height(self: 'Plant', height: float) -> None: + if height > 0: + self._height_is = round(height) + print("Plant height was updated.") + else: + print("Plant height cannot be negative.") + print("Plant height rejected.") + +# Method to show the plant information + def show(self: 'Plant') -> None: + print(self.name_is + ": " + str(self.get_height()) + "cm, " + + str(self.get_age()) + " days old") + + +def ft_plant_security() -> None: + print("=== Garden Security System ===") + rose = Plant("Rose", -10, 5) + print("Plant created: ", end='') + rose.show() + print() + new_age = 10 + new_height = 50 + print(f"Trying to update height to {new_height} and age to {new_age}...") + print() + rose.set_height(new_height) + rose.set_age(new_age) + print() + print("Current state: ", end='') + rose.show() + print() + print("Updating height to 15 and age to 20...") + print() + rose.set_height(15) + rose.set_age(20) + print() + print("Current state: ", end='') + rose.show() + + +if __name__ == "__main__": + ft_plant_security() |
