From bb39d2366f3f088a65d9d855bf5382808e0ec855 Mon Sep 17 00:00:00 2001 From: Manuel Vergara Date: Wed, 10 Jan 2024 22:55:31 +0100 Subject: [PATCH] Update Python Ofensivo --- .../04_proyecto_tienda_animales/exercise.py | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 python-ofensivo/04_proyecto_tienda_animales/exercise.py diff --git a/python-ofensivo/04_proyecto_tienda_animales/exercise.py b/python-ofensivo/04_proyecto_tienda_animales/exercise.py new file mode 100644 index 0000000..cf29452 --- /dev/null +++ b/python-ofensivo/04_proyecto_tienda_animales/exercise.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 + +class Animal: + + def __init__(self, nombre, especie): + + self.nombre = nombre + self.especia = especie + self.alimentado = False + + def alimentar(self): + + self.alimentado = True + + def vender(self): + + self.alimentado = False + + def __str__(self): + + return f"{self.nombre} ({self.especia}) - {'Alimentado' if self.alimentado else 'Hambriento'}" + + +class TiendaAnimales: + + def __init__(self, nombre): + + self.nombre = nombre + self.animales = [] + + def agregar_animal(self, animal): + + self.animales.append(animal) + + def mostrar_animales(self): + + for animal in self.animales: + print(animal) + + def alimentar_animales(self): + + for animal in self.animales: + animal.alimentar() + + def vender_animal(self, nombre): + + for animal in self.animales: + if animal.nombre == nombre: + animal.vender() + self.animales.remove(animal) + print(f"\n[+] Se ha vendido a {animal.nombre}") + return + + print(f"\n[!] No se ha encontrado a {nombre}") + +if __name__ == '__main__': + + tienda = TiendaAnimales("La casa de los animales") + + gato = Animal('Tijuana', 'Gato') + perro = Animal('Munchi', 'Perro') + canario = Animal('Piolin', 'Canario') + + tienda.agregar_animal(gato) + tienda.agregar_animal(perro) + tienda.agregar_animal(canario) + + tienda.mostrar_animales() + tienda.alimentar_animales() + + print(f""" +[+] Mostrando los animales una vez alimentados:""") + + tienda.mostrar_animales() + tienda.vender_animal("Tijuana") + + print(f"\n[+] Mostrando los animales una vez vendido Tijuana:") + + tienda.mostrar_animales()