diff options
Diffstat (limited to 'py01/ex6')
| -rwxr-xr-x | py01/ex6/ft_garden_analytics.py | 227 |
1 files changed, 227 insertions, 0 deletions
diff --git a/py01/ex6/ft_garden_analytics.py b/py01/ex6/ft_garden_analytics.py new file mode 100755 index 0000000..8cd7ef0 --- /dev/null +++ b/py01/ex6/ft_garden_analytics.py @@ -0,0 +1,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) |
