└── Standard Shelter /Standard Shelter: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | class Animal: 4 | def __init__(self, name, age): 5 | self.name = name 6 | self.age = age 7 | 8 | def make_sound(self): 9 | raise NotImplementedError("Subclass must implement abstract method") 10 | 11 | class Dog(Animal): 12 | def make_sound(self): 13 | return "Woof!" 14 | 15 | class Cat(Animal): 16 | def make_sound(self): 17 | return "Meow!" 18 | 19 | class AnimalShelter: 20 | def __init__(self): 21 | self.animals = [] 22 | 23 | def add_animal(self, animal): 24 | self.animals.append(animal) 25 | 26 | def adopt_animal(self, animal_type): 27 | for i, animal in enumerate(self.animals): 28 | if isinstance(animal, animal_type): 29 | return self.animals.pop(i) 30 | return None 31 | 32 | def show_animals(self): 33 | for animal in self.animals: 34 | print(f"{animal.name}, Age: {animal.age}, Sound: {animal.make_sound()}") 35 | 36 | def main(): 37 | shelter = AnimalShelter() 38 | 39 | # Добавляем животных в приют 40 | shelter.add_animal(Dog("Rex", 3)) 41 | shelter.add_animal(Cat("Whiskers", 2)) 42 | shelter.add_animal(Dog("Buddy", 5)) 43 | 44 | # Отображаем животных в приюте 45 | print("Animals in the shelter:") 46 | shelter.show_animals() 47 | 48 | # Пробуем усыновить собаку 49 | adopted_dog = shelter.adopt_animal(Dog) 50 | if adopted_dog: 51 | print(f"Adopted: {adopted_dog.name}, Age: {adopted_dog.age}, Sound: {adopted_dog.make_sound()}") 52 | else: 53 | print("No dog available for adoption") 54 | 55 | # Проверяем оставшихся животных 56 | print("\nAnimals remaining in the shelter:") 57 | shelter.show_animals() 58 | 59 | if __name__ == "__main__": 60 | main() 61 | --------------------------------------------------------------------------------