Advertisement
SimeonTs

SUPyF Basics OOP Principles - 04. Animals

Aug 12th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.73 KB | None | 0 0
  1. """
  2. Basics OOP Principles
  3. Check your solution: https://judge.softuni.bg/Contests/Practice/Index/1556#3
  4.  
  5. SUPyF Basics OOP Principles - 04. Animals
  6.  
  7. Problem:
  8. Create a hierarchy of Animals. Your program should have three different animals – Dog, Frog and Cat.
  9. Deeper in the hierarchy you should have two additional classes
  10. – Kitten and Tomcat. Kittens are female and Tomcats are male!
  11. All types of animals should be able to produce some kind of sound (ProduceSound()).
  12. For example, the dog should be able to bark.
  13. Your task is to model the hierarchy and test its functionality.
  14. Create an animal of each kind and make them all produce sound.
  15. You will be given some lines of input. Each two lines will represent an animal.
  16. On the first line will be the type of animal and on the second – the name, the age and the gender.
  17. When the command "Beast!" is given, stop the input and print all the animals in the format shown below.
  18. Output:
  19. • Print the information for each animal on three lines. On the first line, print: "<AnimalType>"
  20. • On the second line print: "<Name> <Age> <Gender>"
  21. • On the third line print the sounds it produces: "<ProduceSound()>"
  22. Constraints:
  23. • Each Animal should have a name, an age and a gender
  24. • All input values should not be blank (e.g. name, age and so on…)
  25. • If you receive an input for the gender of a Tomcat or a Kitten, ignore it but create the animal
  26. • If the input is invalid for one of the properties, throw an exception with message: "Invalid input!"
  27. • Each animal should have the functionality to ProduceSound()
  28. • Here is the type of sound each animal should produce:
  29. o   Dog: "Woof!"
  30. o   Cat: "Meow meow"
  31. o   Frog: "Ribbit"
  32. o   Kittens: "Meow"
  33. o   Tomcat: "MEOW"
  34. Examples:
  35.    Input:
  36.        Cat
  37.        Tom 12 Male
  38.        Dog
  39.        Sharo 132 Male
  40.        Beast!
  41.    Output:
  42.        Cat
  43.        Tom 12 Male
  44.        Meow meow
  45.        Dog
  46.        Sharo 132 Male
  47.        Woof!
  48. ---------------------------
  49.    Input:
  50.        Frog
  51.        Kermit 12 Male
  52.        Beast!
  53.    Output:
  54.        Frog
  55.        Kermit 12 Male
  56.        Ribbit
  57. ---------------------------
  58.    Input:
  59.        Frog
  60.        Sashko -2 Male
  61.        Frog
  62.        Sashko 2 Male
  63.        Beast!
  64.    Output:
  65.        Invalid input!
  66.        Frog
  67.        Sashko 2 Male
  68.        Ribbit
  69. """
  70.  
  71.  
  72. class Animal:
  73.     def __init__(self, a_type, name, age, gender):
  74.         self.a_type = a_type
  75.         self.name = name
  76.         self.age = age
  77.         self.gender = gender
  78.  
  79.     @property
  80.     def a_type(self):
  81.         return self._a_type
  82.  
  83.     @a_type.setter
  84.     def a_type(self, a_type):
  85.         if a_type == "Dog" or a_type == "Cat" or a_type == "Frog" or a_type == "Kitten" or a_type == "Tomcat":
  86.             self._a_type = a_type
  87.         else:
  88.             raise Exception("Invalid input!")
  89.  
  90.     @property
  91.     def name(self):
  92.         return self._name
  93.  
  94.     @name.setter
  95.     def name(self, name):
  96.         if len(name) > 2 and name.isalpha() and name[0].isupper():
  97.             self._name = name
  98.         else:
  99.             raise Exception("Invalid input!")
  100.  
  101.     @property
  102.     def age(self):
  103.         return self._age
  104.  
  105.     @age.setter
  106.     def age(self, age):
  107.         if age.isdigit() and int(age) > 0:
  108.             self._age = age
  109.         else:
  110.             raise Exception("Invalid input!")
  111.  
  112.     @property
  113.     def gender(self):
  114.         return self._gender
  115.  
  116.     @gender.setter
  117.     def gender(self, gender):
  118.         if self.a_type == "Kitten":
  119.             self._gender = "Female"
  120.         elif self.a_type == "Tomcat":
  121.             self._gender = "Male"
  122.         elif gender == "Male" or gender == "Female":
  123.             self._gender = gender
  124.         else:
  125.             raise Exception("Invalid input!")
  126.  
  127.     def sound(self):
  128.         if self.a_type == "Dog":
  129.             sound = "Woof!"
  130.             return sound
  131.         elif self.a_type == "Cat":
  132.             sound = "Meow meow"
  133.             return sound
  134.         elif self.a_type == "Frog":
  135.             sound = "Ribbit"
  136.             return sound
  137.         elif self.a_type == "Kitten":
  138.             sound = "Meow"
  139.             return sound
  140.         elif self.a_type == "Tomcat":
  141.             sound = "MEOW"
  142.             return sound
  143.  
  144.     def __str__(self):
  145.         return f"{self.a_type}" + "\n" + f"{self.name} {self.age} {self.gender}" + "\n" + f"{self.sound()}"
  146.  
  147.  
  148. while True:
  149.     type_animal = input()
  150.     if type_animal == "Beast!":
  151.         break
  152.     animal_data = input().split()
  153.     animal_name = animal_data[0]
  154.     animal_age = animal_data[1]
  155.     animal_gender = animal_data[2]
  156.  
  157.     try:
  158.         a = Animal(a_type=type_animal, name=animal_name, age=animal_age, gender=animal_gender)
  159.         print(a)
  160.     except Exception as e:
  161.         print(str(e))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement