summaryrefslogtreecommitdiff
path: root/py01/ex4/ft_garden_security.py
blob: ef983b9c418a475560fc23945352a9c1c629a220 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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()