Advertisement
Nenogzar

animal

Jul 24th, 2024
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. from abc import ABC, abstractmethod
  2. from project.food import Food
  3.  
  4. data_dic = {
  5.     "Hen": {
  6.         "food": ["Vegetable", "Fruit", "Meat", "Seed"],
  7.         "weight increase": 0.35,
  8.         "sound": "Cluck"
  9.     },
  10.     "Owl": {
  11.         "food": ["Meat"],
  12.         "weight increase": 0.25,
  13.         "sound": "Hoot Hoot"
  14.     },
  15.     "Mouse": {
  16.         "food": ["Vegetable", "Fruit"],
  17.         "weight increase": 0.10,
  18.         "sound": "Squeak"
  19.     },
  20.     "Cat": {
  21.         "food": ["Vegetable", "Meat"],
  22.         "weight increase": 0.30,
  23.         "sound": "Meow"
  24.     },
  25.     "Dog": {
  26.         "food": ["Meat"],
  27.         "weight increase": 0.40,
  28.         "sound": "Woof!"
  29.     },
  30.     "Tiger": {
  31.         "food": ["Meat"],
  32.         "weight increase": 1.00,
  33.         "sound": "ROAR!!!"
  34.     }
  35. }
  36.  
  37. class Animal(ABC):
  38.     def __init__(self, name: str, weight: float):
  39.         self.name = name
  40.         self.weight = weight
  41.         self.food_eaten = 0
  42.         self.animal_type = self.__class__.__name__
  43.  
  44.     @abstractmethod
  45.     def make_sound(self):
  46.         pass
  47.  
  48.     def feed(self, food: Food):
  49.         animal_data = data_dic[self.animal_type]
  50.         if type(food).__name__ in animal_data["food"]:
  51.             self.food_eaten += food.quantity
  52.             self.weight += food.quantity * animal_data["weight increase"]
  53.         else:
  54.             return f"{self.__class__.__name__} does not eat {food.__class__.__name__}!"
  55.  
  56.     def make_sound(self):
  57.         return data_dic[self.animal_type]["sound"]
  58.  
  59.     @abstractmethod
  60.     def __repr__(self):
  61.         pass
  62.  
  63. class Bird(Animal):
  64.     def __init__(self, name: str, weight: float, wing_size: float):
  65.         super().__init__(name, weight)
  66.         self.wing_size = wing_size
  67.  
  68.     def __repr__(self):
  69.         return f"{self.__class__.__name__} [{self.name}, {self.wing_size}, {self.weight}, {self.food_eaten}]"
  70.  
  71. class Mammal(Animal):
  72.     def __init__(self, name: str, weight: float, living_region: str):
  73.         super().__init__(name, weight)
  74.         self.living_region = living_region
  75.  
  76.     def __repr__(self):
  77.         return f"{self.__class__.__name__} [{self.name}, {self.weight}, {self.living_region}, {self.food_eaten}]"
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement