mirror of
https://github.com/DavidGailleton/42-Piscine_Python.git
synced 2026-03-14 05:06:55 +01:00
Corrected ex1, ready to push
This commit is contained in:
@@ -5,7 +5,7 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
print("=== Welcome to My Garden ===")
|
print("=== Welcome to My Garden ===")
|
||||||
print("Plant:", name)
|
print("Plant:", name)
|
||||||
print("Height:", height, "cm")
|
print("Height: ", height, "cm", sep="")
|
||||||
print("Age:", age, "days")
|
print("Age:", age, "days")
|
||||||
print("")
|
print("")
|
||||||
print("=== End of Program ===")
|
print("=== End of Program ===")
|
||||||
|
|||||||
@@ -1,22 +1,24 @@
|
|||||||
class Plants:
|
class Plant:
|
||||||
name: str
|
name: str
|
||||||
height: int
|
height: int
|
||||||
days: int
|
days: int
|
||||||
|
|
||||||
def __init__(self, name, height, days):
|
def __init__(self, name: str, height: int, days: int) -> None:
|
||||||
|
"""Init plant with his value"""
|
||||||
self.name = name
|
self.name = name
|
||||||
self.height = height
|
self.height = height
|
||||||
self.days = days
|
self.days = days
|
||||||
|
|
||||||
def print_plant(self):
|
def print_plant(self) -> None:
|
||||||
print(self.name.capitalize() + ":", self.height, end="")
|
"""Display plant informations"""
|
||||||
print("cm,", self.days, "days old")
|
print(self.name, ": ", self.height,
|
||||||
|
"cm, ", self.days, " days old", sep="")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
x = Plants("rose", 25, 30)
|
x = Plant("rose", 25, 30)
|
||||||
y = Plants("sunflower", 80, 45)
|
y = Plant("sunflower", 80, 45)
|
||||||
z = Plants("cactus", 15, 120)
|
z = Plant("cactus", 15, 120)
|
||||||
print("=== Garden Plant Registry ===")
|
print("=== Garden Plant Registry ===")
|
||||||
x.print_plant()
|
x.print_plant()
|
||||||
y.print_plant()
|
y.print_plant()
|
||||||
|
|||||||
@@ -3,28 +3,35 @@ class Plants:
|
|||||||
height: int
|
height: int
|
||||||
days: int
|
days: int
|
||||||
|
|
||||||
def __init__(self, name, height, days):
|
def __init__(self, name: str, height: int, days: int) -> None:
|
||||||
|
"""Init plant with his value"""
|
||||||
self.name = name
|
self.name = name
|
||||||
self.height = height
|
self.height = height
|
||||||
self.days = days
|
self.days = days
|
||||||
|
|
||||||
def get_info(self):
|
def get_info(self) -> None:
|
||||||
print(self.name.capitalize() + ":", self.height, end="")
|
"""Display plant informations"""
|
||||||
print("cm,", self.days, "days old")
|
print(self.name, ": ", self.height,
|
||||||
|
"cm, ", self.days, " days old", sep="")
|
||||||
|
|
||||||
def grow(self):
|
def grow(self) -> None:
|
||||||
|
"""Increase height"""
|
||||||
self.height = self.height + 1
|
self.height = self.height + 1
|
||||||
|
|
||||||
def age(self):
|
def age(self) -> None:
|
||||||
|
"""Increase days by one and grow plant"""
|
||||||
self.days = self.days + 1
|
self.days = self.days + 1
|
||||||
self.grow()
|
self.grow()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
x = Plants("rose", 25, 1)
|
x = Plants("rose", 25, 30)
|
||||||
start = x.days;
|
start = x.days
|
||||||
|
print("=== Day 1 ===")
|
||||||
x.get_info()
|
x.get_info()
|
||||||
for n in range(1, 8):
|
for n in range(1, 7):
|
||||||
x.age()
|
x.age()
|
||||||
|
print(f"=== Day {x.days - start + 1} ===")
|
||||||
x.get_info()
|
x.get_info()
|
||||||
print("Growth this week: +", x.days - start, "cm");
|
print("Growth this week: +",
|
||||||
|
x.days - start, "cm", sep="")
|
||||||
|
|||||||
@@ -1,36 +1,56 @@
|
|||||||
class Plants:
|
class Plant:
|
||||||
name: str
|
name: str
|
||||||
height: int
|
height: int
|
||||||
p_age: int
|
days: int
|
||||||
|
|
||||||
def __init__(self, name, height, age):
|
def get_info(self) -> None:
|
||||||
self.name = name
|
"""Display plant informations"""
|
||||||
self.height = height
|
print(self.name, " (", self.height,
|
||||||
self.p_age = age
|
"cm, ", self.days, " days)", sep="")
|
||||||
|
|
||||||
def get_info(self):
|
def grow(self) -> None:
|
||||||
print(self.name.capitalize() + ":", self.height, end="")
|
"""Increase height"""
|
||||||
print("cm,", self.p_age, "days old")
|
|
||||||
|
|
||||||
def grow(self):
|
|
||||||
self.height = self.height + 1
|
self.height = self.height + 1
|
||||||
|
|
||||||
def age(self):
|
def age(self) -> None:
|
||||||
self.p_age = self.p_age + 1
|
"""Increase days by one and grow plant"""
|
||||||
|
self.days = self.days + 1
|
||||||
self.grow()
|
self.grow()
|
||||||
|
|
||||||
|
def __init__(self, name: str, height: int, age: int) -> None:
|
||||||
|
"""Init plant with his values and display his values"""
|
||||||
|
self.name = name
|
||||||
|
self.height = height
|
||||||
|
self.days = age
|
||||||
|
print("Created: ", end="")
|
||||||
|
self.get_info()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
plant_1 = Plants("Rose", 50, 2)
|
class PlantFactory:
|
||||||
plant_2 = Plants("Chrysanthem", 30, 1)
|
@staticmethod
|
||||||
plant_3 = Plants("Rosemary", 60, 5)
|
def create_plants(plants: list[tuple[str,
|
||||||
plant_4 = Plants("Cucumber", 40, 3)
|
int, int]]) -> list[Plant | None]:
|
||||||
plant_5 = Plants("Salade", 15, 4)
|
"""Create list of plants by list of arguments"""
|
||||||
plants = [plant_1, plant_2, plant_3, plant_4, plant_5]
|
i = 0
|
||||||
|
for n in plants:
|
||||||
|
i = i + 1
|
||||||
|
new_plants: list[Plant | None] = [None] * i
|
||||||
|
i = 0
|
||||||
|
for n in plants:
|
||||||
|
new_plants[i] = Plant(n[0], n[1], n[2])
|
||||||
|
i = i + 1
|
||||||
|
return new_plants
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
plants = PlantFactory.create_plants([
|
||||||
|
("Rose", 50, 2),
|
||||||
|
("Chrysanthem", 30, 1),
|
||||||
|
("Rosemary", 60, 5),
|
||||||
|
("Cucumber", 40, 3),
|
||||||
|
("Salade", 15, 4)
|
||||||
|
])
|
||||||
i = 0
|
i = 0
|
||||||
for n in plants:
|
for n in plants:
|
||||||
print("Created:", end=" ")
|
|
||||||
n.get_info()
|
|
||||||
i = i + 1
|
i = i + 1
|
||||||
print("\nTotal plants created:", i)
|
print("\nTotal plants created:", i)
|
||||||
|
|
||||||
|
|||||||
@@ -4,33 +4,44 @@ class SecurePlant:
|
|||||||
__age: int
|
__age: int
|
||||||
|
|
||||||
def set_height(self, height: int) -> None:
|
def set_height(self, height: int) -> None:
|
||||||
|
"""Set height to plant"""
|
||||||
if height < 0:
|
if height < 0:
|
||||||
print("Invalide operation attempted: height", height, "cm [REJECTED]")
|
print("Invalide operation attempted: height ",
|
||||||
|
height, "cm [REJECTED]", sep="")
|
||||||
|
print("Secrity: Negative height rejected")
|
||||||
else:
|
else:
|
||||||
self.__height = height
|
self.__height = height
|
||||||
|
print("Height updated: ", height, "cm [OK]", sep="")
|
||||||
|
|
||||||
def set_age(self, age: int) -> None:
|
def set_age(self, age: int) -> None:
|
||||||
|
"""Set age to plant"""
|
||||||
if age < 0:
|
if age < 0:
|
||||||
print("Invalide operation attempted: age", age, "days [REJECTED]")
|
print("Invalide operation attempted: age", age, "days [REJECTED]")
|
||||||
|
print("Security: Negative age rejected")
|
||||||
else:
|
else:
|
||||||
self.__age = age
|
self.__age = age
|
||||||
|
print("Age updated:", age, "days [OK]")
|
||||||
|
|
||||||
def get_height(self) -> int:
|
def get_height(self) -> int:
|
||||||
|
"""Get plant height"""
|
||||||
return self.__height
|
return self.__height
|
||||||
|
|
||||||
def get_age(self) -> int:
|
def get_age(self) -> int:
|
||||||
|
"""Get plant age"""
|
||||||
return self.__age
|
return self.__age
|
||||||
|
|
||||||
def __init__(self, name: str, height: int, age: int):
|
def __init__(self, name: str, height: int, age: int) -> None:
|
||||||
|
"""Init plant with his value"""
|
||||||
self.name = name
|
self.name = name
|
||||||
self.__height = height
|
self.set_height(height)
|
||||||
self.__age = age
|
self.set_age(age)
|
||||||
|
print("Plant created:", name)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
plant = SecurePlant("Rose", 10, 3)
|
plant = SecurePlant("Rose", 10, 3)
|
||||||
print(plant.name, plant.get_height(), plant.get_age())
|
plant.set_height(25)
|
||||||
plant.set_height(-10)
|
|
||||||
plant.set_age(30)
|
plant.set_age(30)
|
||||||
print(plant.name, plant.get_height(), plant.get_age())
|
plant.set_height(-10)
|
||||||
|
print("Current plant: ", plant.name, " (", plant.get_height(),
|
||||||
|
"cm, ", plant.get_age(), " days)", sep="")
|
||||||
|
|||||||
@@ -1,37 +1,58 @@
|
|||||||
|
from typing_extensions import override
|
||||||
|
|
||||||
|
|
||||||
class Plant:
|
class Plant:
|
||||||
name: str
|
name: str
|
||||||
height: int
|
height: int
|
||||||
age: int
|
age: int
|
||||||
|
|
||||||
def __init__(self, name, height, age):
|
def __init__(self, name: str, height: int, age: int) -> None:
|
||||||
|
"""Init plant with his value"""
|
||||||
self.name = name
|
self.name = name
|
||||||
self.height = height
|
self.height = height
|
||||||
self.age = age
|
self.age = age
|
||||||
|
|
||||||
def get_info(self):
|
def get_info(self) -> None:
|
||||||
print(self.name.capitalize() + ":", self.height, end="")
|
"""Display plant informations"""
|
||||||
print("cm,", self.age, "days old")
|
print(f"{self.name}: {self.height}cm, {self.age} days")
|
||||||
|
|
||||||
|
|
||||||
class Flower(Plant):
|
class Flower(Plant):
|
||||||
color: str
|
color: str
|
||||||
|
|
||||||
def bloom(self):
|
def bloom(self) -> None:
|
||||||
|
"""Make flower blooming"""
|
||||||
print(self.name + " is blooming beautifully !")
|
print(self.name + " is blooming beautifully !")
|
||||||
|
|
||||||
def __init__(self, name, height, age, color):
|
@override
|
||||||
|
def get_info(self) -> None:
|
||||||
|
"""Display Flower info"""
|
||||||
|
print(f"{self.name} (Flower): {self.height}cm,",
|
||||||
|
f"{self.age} days, {self.color} color")
|
||||||
|
|
||||||
|
def __init__(self, name: str, height: int, age: int, color: str) -> None:
|
||||||
|
"""Init flower with his value"""
|
||||||
super().__init__(name=name, height=height, age=age)
|
super().__init__(name=name, height=height, age=age)
|
||||||
self.color = color
|
self.color = color
|
||||||
|
|
||||||
|
|
||||||
class Tree(Plant):
|
class Tree(Plant):
|
||||||
trunk_diameter: str
|
trunk_diameter: int
|
||||||
|
|
||||||
def produce_shade(self):
|
def produce_shade(self) -> None:
|
||||||
|
"""Produce shade and display his size"""
|
||||||
shade_size = self.height * self.trunk_diameter / 1000
|
shade_size = self.height * self.trunk_diameter / 1000
|
||||||
print(self.name, "provides", shade_size, "square meter")
|
print(self.name, "provides", shade_size, "square meter of shade")
|
||||||
|
|
||||||
def __init__(self, name, height, age, trunk_diameter):
|
@override
|
||||||
|
def get_info(self) -> None:
|
||||||
|
"""Display Tree info"""
|
||||||
|
print(f"{self.name} (Tree): {self.height}cm,",
|
||||||
|
f"{self.age} days, {self.trunk_diameter}cm diameter")
|
||||||
|
|
||||||
|
def __init__(self, name: str, height: int,
|
||||||
|
age: int, trunk_diameter: int) -> None:
|
||||||
|
"""Init tree with his value"""
|
||||||
super().__init__(name=name, height=height, age=age)
|
super().__init__(name=name, height=height, age=age)
|
||||||
self.trunk_diameter = trunk_diameter
|
self.trunk_diameter = trunk_diameter
|
||||||
|
|
||||||
@@ -40,7 +61,19 @@ class Vegetable(Plant):
|
|||||||
harvest_season: str
|
harvest_season: str
|
||||||
nutritional_value: str
|
nutritional_value: str
|
||||||
|
|
||||||
def __init__(self, name, height, age, hs, nv):
|
def print_nutritional_value(self) -> None:
|
||||||
|
"""Display nutritional value"""
|
||||||
|
print(f"{self.name} is rich in {self.nutritional_value}")
|
||||||
|
|
||||||
|
@override
|
||||||
|
def get_info(self) -> None:
|
||||||
|
"""Display Vegetable info info"""
|
||||||
|
print(f"{self.name} (Vegetable): {self.height}cm,",
|
||||||
|
f"{self.age} days, {self.harvest_season} harvest")
|
||||||
|
|
||||||
|
def __init__(self, name: str, height: int,
|
||||||
|
age: int, hs: str, nv: str) -> None:
|
||||||
|
"""Init vegetable with his value"""
|
||||||
super().__init__(name=name, height=height, age=age)
|
super().__init__(name=name, height=height, age=age)
|
||||||
self.harvest_season = hs
|
self.harvest_season = hs
|
||||||
self.nutritional_value = nv
|
self.nutritional_value = nv
|
||||||
@@ -49,11 +82,16 @@ class Vegetable(Plant):
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
flower0 = Flower("Rose", 30, 5, "red")
|
flower0 = Flower("Rose", 30, 5, "red")
|
||||||
flower1 = Flower("Chrysanthem", 50, 1, "yellow")
|
flower1 = Flower("Chrysanthem", 50, 1, "yellow")
|
||||||
tree0 = Tree("Platane", 300, 6, 30)
|
tree0 = Tree("Spruce", 300, 6, 30)
|
||||||
tree1 = Tree("Sapin", 700, 15, 50)
|
tree1 = Tree("Oak", 700, 15, 50)
|
||||||
vegetable0 = Vegetable("Salade", 30, 1, "Summer", "Vitamine c")
|
vegetable0 = Vegetable("Salade", 30, 1, "Summer", "Vitamine c")
|
||||||
vegetable1 = Vegetable("tomato", 200, 2, "Autumn", "Vitamine d")
|
vegetable1 = Vegetable("Tomato", 200, 2, "Autumn", "Vitamine d")
|
||||||
print(flower0.name, flower0.height, flower0.age, flower0.color)
|
|
||||||
|
flower0.get_info()
|
||||||
flower0.bloom()
|
flower0.bloom()
|
||||||
|
print("")
|
||||||
|
tree0.get_info()
|
||||||
tree0.produce_shade()
|
tree0.produce_shade()
|
||||||
print(vegetable0.name, "is rich in", vegetable0.nutritional_value)
|
print("")
|
||||||
|
vegetable0.get_info()
|
||||||
|
vegetable0.print_nutritional_value()
|
||||||
|
|||||||
@@ -1,90 +1,179 @@
|
|||||||
class Plant:
|
class Plant:
|
||||||
name: str
|
name: str
|
||||||
height: int
|
height: int
|
||||||
kind: str
|
total_growth: int
|
||||||
|
__kind: str
|
||||||
|
|
||||||
def __init__(self, name: str, height: int):
|
def __init__(self, name: str, height: int) -> None:
|
||||||
|
"""Init plant with his value"""
|
||||||
self.name = name
|
self.name = name
|
||||||
self.height = height
|
self.height = height
|
||||||
self.kind = "regular"
|
self.__kind = "regular"
|
||||||
|
self.total_growth = 0
|
||||||
|
|
||||||
@classmethod
|
def get_kind(self) -> str:
|
||||||
def grow(self):
|
"""Return __kind value"""
|
||||||
|
return self.__kind
|
||||||
|
|
||||||
|
def set_kind(self, kind: str) -> None:
|
||||||
|
"""Set __kind value"""
|
||||||
|
self.__kind = kind
|
||||||
|
|
||||||
|
def grow(self) -> None:
|
||||||
|
"""Grow the plant"""
|
||||||
self.height = self.height + 1
|
self.height = self.height + 1
|
||||||
|
self.total_growth += 1
|
||||||
|
|
||||||
|
|
||||||
class FloweringPlant(Plant):
|
class FloweringPlant(Plant):
|
||||||
color: str
|
color: str
|
||||||
blooming = False
|
blooming: bool = False
|
||||||
|
|
||||||
def bloom (self):
|
def bloom(self) -> None:
|
||||||
|
"""Set blooming to true"""
|
||||||
self.blooming = True
|
self.blooming = True
|
||||||
print(self.name, "is blooming beautifully !")
|
print(self.name, "is blooming beautifully !")
|
||||||
|
|
||||||
def __init__(self, name, height, color):
|
def __init__(self, name: str, height: int, color: str) -> None:
|
||||||
|
"""Init Flowering plant with his value"""
|
||||||
super().__init__(name=name, height=height)
|
super().__init__(name=name, height=height)
|
||||||
self.color = color
|
self.color = color
|
||||||
self.kind = "flowering"
|
self.set_kind("flowering")
|
||||||
|
|
||||||
|
|
||||||
class PrizeFlower(FloweringPlant):
|
class PrizeFlower(FloweringPlant):
|
||||||
prize: int
|
prize: int
|
||||||
|
|
||||||
def __init__(self, name, height, color, prize):
|
def __init__(self, name: str, height: int, color: str, prize: int) -> None:
|
||||||
|
"""Init prize flower with his value"""
|
||||||
super().__init__(name=name, height=height, color=color)
|
super().__init__(name=name, height=height, color=color)
|
||||||
self.prize = prize
|
self.prize = prize
|
||||||
self.kind = "prize"
|
self.set_kind("prize")
|
||||||
|
|
||||||
|
|
||||||
class GardenManager:
|
class GardenManager:
|
||||||
__gardens = {}
|
gardens: dict[str, list[Plant | FloweringPlant | PrizeFlower]]
|
||||||
|
nb_gardens: int
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
"""Init garden manager"""
|
||||||
|
self.gardens = {}
|
||||||
|
self.nb_gardens = 0
|
||||||
|
|
||||||
class GardenStats:
|
class GardenStats:
|
||||||
@staticmethod
|
@classmethod
|
||||||
def garden_report(garden: []):
|
def garden_report(cls,
|
||||||
i = 0
|
garden: list[Plant |
|
||||||
for n in garden:
|
FloweringPlant |
|
||||||
i = i + 1
|
PrizeFlower] | None) -> None:
|
||||||
n = 0
|
"""Start all garden report"""
|
||||||
while n < i:
|
if garden is not None:
|
||||||
print(garden[n].name + ":", end=" ")
|
cls.flower_in_garden(garden)
|
||||||
print(garden[n].height, "cm", end="")
|
|
||||||
if (garden[n].kind == "flowering" or garden[n].kind == "prize"):
|
|
||||||
print(",", garden[n].color, "flowers", end=" ")
|
|
||||||
if (garden[n].blooming):
|
|
||||||
print("(blooming)", end="")
|
|
||||||
if (garden[n].kind == "prize"):
|
|
||||||
print(", Prize points:", garden[n].prize, end="")
|
|
||||||
print("")
|
print("")
|
||||||
n = n + 1
|
cls.added_plant_report(garden)
|
||||||
|
cls.plants_in_garden(garden)
|
||||||
|
else:
|
||||||
|
print("Garden not provide")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def flower_in_garden(garden: list[Plant |
|
||||||
|
FloweringPlant |
|
||||||
|
PrizeFlower]) -> None:
|
||||||
|
"""List flower in garden and display their status"""
|
||||||
|
for n in garden:
|
||||||
|
print("-", n.name + ":", end=" ")
|
||||||
|
print(n.height, "cm", end="")
|
||||||
|
if n.get_kind() == "flowering" or n.get_kind() == "prize":
|
||||||
|
print(",", n.color, "flowers", end="")
|
||||||
|
if n.blooming:
|
||||||
|
print(" (blooming)", end="")
|
||||||
|
if n.get_kind() == "prize":
|
||||||
|
print(", Prize points:", n.prize, end="")
|
||||||
|
print("")
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
def get_garden(self, garden: str) -> []:
|
def added_plant_report(garden: list[Plant |
|
||||||
return self.__gardens[garden]
|
FloweringPlant |
|
||||||
|
PrizeFlower]) -> None:
|
||||||
|
"""Report number of plant added to garden"""
|
||||||
|
total_growth = 0
|
||||||
|
total_plants = 0
|
||||||
|
for n in garden:
|
||||||
|
total_plants += 1
|
||||||
|
total_growth += n.total_growth
|
||||||
|
print(f"Plants added: {total_plants},",
|
||||||
|
f"Total growth: {total_growth}cm")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def plants_in_garden(garden: list[Plant |
|
||||||
|
FloweringPlant |
|
||||||
|
PrizeFlower]) -> None:
|
||||||
|
"""Report number of plant by types"""
|
||||||
|
nb_regular = 0
|
||||||
|
nb_flowering = 0
|
||||||
|
nb_prize = 0
|
||||||
|
for n in garden:
|
||||||
|
if n.get_kind() == "regular":
|
||||||
|
nb_regular += 1
|
||||||
|
elif n.get_kind() == "flowering":
|
||||||
|
nb_flowering += 1
|
||||||
|
elif n.get_kind() == "prize":
|
||||||
|
nb_prize += 1
|
||||||
|
print(f"Plant types: {nb_regular} regular,",
|
||||||
|
f"{nb_flowering} flowering, {nb_prize} prize flowers")
|
||||||
|
|
||||||
@classmethod
|
def get_nb_gardens(self) -> int:
|
||||||
def add_plant(self, garden: str, new_plant: Plant):
|
"""return the number of garden in the manager"""
|
||||||
if garden in self.__gardens:
|
return self.nb_gardens
|
||||||
|
|
||||||
|
def get_garden(self, garden: str) -> list[Plant |
|
||||||
|
FloweringPlant |
|
||||||
|
PrizeFlower] | None:
|
||||||
|
"""Return the plant list of the garden name provide if exist"""
|
||||||
|
if garden in self.gardens:
|
||||||
|
return self.gardens[garden]
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def add_plant(self, garden: str, new_plant: Plant |
|
||||||
|
FloweringPlant |
|
||||||
|
PrizeFlower) -> None:
|
||||||
|
"""Add one plant to the garden if exist"""
|
||||||
|
if garden in self.gardens:
|
||||||
i = 0
|
i = 0
|
||||||
for n in self.__gardens[garden]:
|
for n in self.gardens[garden]:
|
||||||
i = i + 1
|
i = i + 1
|
||||||
new = [None] * (i + 1)
|
new: list[Plant |
|
||||||
j = 0;
|
FloweringPlant |
|
||||||
|
PrizeFlower] = [Plant("", 0)] * (i + 1)
|
||||||
|
j = 0
|
||||||
while j < i:
|
while j < i:
|
||||||
new[j] = self.__gardens[garden][j]
|
new[j] = self.gardens[garden][j]
|
||||||
j = j + 1
|
j = j + 1
|
||||||
new[i] = new_plant
|
new[i] = new_plant
|
||||||
self.__gardens[garden] = new
|
self.gardens[garden] = new
|
||||||
print("Added", new_plant.name, "to", garden, "'s garden")
|
print("Added ", new_plant.name, " to ",
|
||||||
|
garden, "'s garden", sep="")
|
||||||
else:
|
else:
|
||||||
print("Garden not found")
|
print("Garden not found")
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def create_garden_network(self, garden_name: str) -> None:
|
def create_garden_network(self, garden_name: str) -> None:
|
||||||
if garden_name in self.__gardens:
|
"""Create a garden by name provide if it not exist"""
|
||||||
|
if garden_name in self.gardens:
|
||||||
print("garden already exist")
|
print("garden already exist")
|
||||||
else:
|
else:
|
||||||
self.__gardens[garden_name] = []
|
self.gardens[garden_name] = []
|
||||||
|
self.nb_gardens += 1
|
||||||
|
|
||||||
|
def grow_all(self, garden: str) -> None:
|
||||||
|
"""Make all plant of one garden to grow"""
|
||||||
|
if garden in self.gardens:
|
||||||
|
print(f"{garden} is helping all plants grow...")
|
||||||
|
for n in self.gardens[garden]:
|
||||||
|
print(f"{n.name} grew 1cm")
|
||||||
|
n.grow()
|
||||||
|
else:
|
||||||
|
print("Garden not found")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@@ -99,6 +188,7 @@ if __name__ == "__main__":
|
|||||||
manager.add_plant("Alice", rose)
|
manager.add_plant("Alice", rose)
|
||||||
manager.add_plant("Alice", sunflower)
|
manager.add_plant("Alice", sunflower)
|
||||||
manager.add_plant("Bob", oak)
|
manager.add_plant("Bob", oak)
|
||||||
|
manager.grow_all("Alice")
|
||||||
|
manager.grow_all("Alice")
|
||||||
|
print("\n\nGardenReport")
|
||||||
manager.GardenStats.garden_report(manager.get_garden("Alice"))
|
manager.GardenStats.garden_report(manager.get_garden("Alice"))
|
||||||
|
|||||||
Reference in New Issue
Block a user