Advertisement
GeorgiLukanov87

Python OOP Exam - 14 August 2022 - Horse Racings

Nov 16th, 2022 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.66 KB | None | 0 0
  1. # Python OOP Exam - 14 August 2022 - Horse Racings
  2.  
  3. # https://judge.softuni.org/Contests/Practice/Index/3558#1
  4. =============================================================================================
  5. # file name: horse.py
  6.  
  7. from abc import ABC, abstractmethod
  8.  
  9.  
  10. class Horse(ABC):
  11.     MAX_HORSE_SPEED = None
  12.  
  13.     def __init__(self, name: str, speed: int):
  14.         self.name = name
  15.         self.speed = speed
  16.         self.is_taken = False
  17.  
  18.     @property
  19.     def name(self):
  20.         return self.__name
  21.  
  22.     @name.setter
  23.     def name(self, value):
  24.         if len(value) < 4:
  25.             raise ValueError(f"Horse name {value} is less than 4 symbols!")
  26.         self.__name = value
  27.  
  28.     @property
  29.     def speed(self):
  30.         return self.__speed
  31.  
  32.     @speed.setter
  33.     def speed(self, value):
  34.         if value > self.MAX_HORSE_SPEED:
  35.             raise ValueError(f"Horse speed is too high!")
  36.         self.__speed = value
  37.  
  38.     @abstractmethod
  39.     def train(self):
  40.         ...
  41.  
  42. =============================================================================================
  43. # file name: appaloosa.py
  44.  
  45. from project.horse_specification.horse import Horse
  46.  
  47.  
  48. class Appaloosa(Horse):
  49.     MAX_HORSE_SPEED = 120
  50.  
  51.     def __init__(self, name: str, speed: int):
  52.         super().__init__(name, speed)
  53.  
  54.     def train(self):
  55.         if self.speed <= 118:
  56.             self.speed += 2
  57.         elif self.speed < 120:
  58.             self.speed = 120
  59.  
  60. =============================================================================================
  61. # file name: thoroughbred.py
  62.  
  63. from project.horse_specification.horse import Horse
  64.  
  65.  
  66. class Thoroughbred(Horse):
  67.     MAX_HORSE_SPEED = 140
  68.  
  69.     def __init__(self, name: str, speed: int):
  70.         super().__init__(name, speed)
  71.  
  72.     def train(self):
  73.         if self.speed <= 137:
  74.             self.speed += 3
  75.         elif self.speed < 140:
  76.             self.speed = 140
  77.  
  78. =============================================================================================
  79. # file name: jockey.py
  80.  
  81. class Jockey:
  82.     def __init__(self, name, age):
  83.         self.name = name
  84.         self.age = age
  85.         self.horse = None
  86.  
  87.     @property
  88.     def name(self):
  89.         return self.__name
  90.  
  91.     @name.setter
  92.     def name(self, value):
  93.         if value.strip() == '':
  94.             raise ValueError("Name should contain at least one character!")
  95.         self.__name = value
  96.  
  97.     @property
  98.     def age(self):
  99.         return self.__age
  100.  
  101.     @age.setter
  102.     def age(self, value):
  103.         if value < 18:
  104.             raise ValueError('Jockeys must be at least 18 to participate the race!')
  105.         self.__age = value
  106.  
  107.  
  108. =============================================================================================
  109. # file name: horse_race.py
  110.  
  111. class HorseRace:
  112.     def __init__(self, race_type: str):
  113.         self.race_type = race_type
  114.         self.jockeys = []
  115.  
  116.     @property
  117.     def race_type(self):
  118.         return self.__race_type
  119.  
  120.     @race_type.setter
  121.     def race_type(self, value):
  122.         if value not in ["Winter", "Spring", "Autumn", "Summer"]:
  123.             raise ValueError("Race type does not exist!")
  124.         self.__race_type = value
  125.  
  126. =============================================================================================
  127. # file name: horse_race_app
  128.  
  129. from project.horse_race import HorseRace
  130. from project.horse_specification.appaloosa import Appaloosa
  131. from project.horse_specification.thoroughbred import Thoroughbred
  132. from project.jockey import Jockey
  133.  
  134.  
  135. class HorseRaceApp:
  136.     def __init__(self):
  137.         self.horses = []  # all the horses (objects).
  138.         self.jockeys = []  # all the jockeys (objects).
  139.         self.horse_races = []  # all the horse races (objects).
  140.  
  141.     def add_horse(self, horse_type: str, horse_name: str, horse_speed: int):
  142.         new_horse = self.create_horse_by_type(horse_type, horse_name, horse_speed)
  143.         if new_horse:
  144.             if horse_name not in [h.name for h in self.horses]:
  145.                 self.horses.append(new_horse)
  146.                 return f"{horse_type} horse {horse_name} is added."
  147.             else:
  148.                 raise Exception(f"Horse {horse_name} has been already added!")
  149.  
  150.     @staticmethod
  151.     def create_horse_by_type(horse_type_, horse_name_, horse_speed_):
  152.         if horse_type_ == 'Appaloosa':
  153.             return Appaloosa(horse_name_, horse_speed_)
  154.         elif horse_type_ == 'Thoroughbred':
  155.             return Thoroughbred(horse_name_, horse_speed_)
  156.         else:
  157.             return None
  158.  
  159.     def add_jockey(self, jockey_name: str, age: int):
  160.         new_jockey = Jockey(jockey_name, age)
  161.         if jockey_name not in [j.name for j in self.jockeys]:
  162.             self.jockeys.append(new_jockey)
  163.             return f'Jockey {jockey_name} is added.'
  164.         else:
  165.             raise Exception(f'Jockey {jockey_name} has been already added!')
  166.  
  167.     def create_horse_race(self, race_type: str):
  168.         new_race = HorseRace(race_type)
  169.         if race_type not in [r.race_type for r in self.horse_races]:
  170.             self.horse_races.append(new_race)
  171.             return f'Race {race_type} is created.'
  172.         else:
  173.             raise Exception(f"Race {race_type} has been already created!")
  174.  
  175.     def add_horse_to_jockey(self, jockey_name: str, horse_type: str):
  176.         jockey = self.search_jockey_by_name(jockey_name, self.jockeys)
  177.         horse = self.search_horse_by_type(horse_type, self.horses)
  178.  
  179.         if jockey.horse:
  180.             return f'Jockey {jockey_name} already has a horse.'
  181.         else:
  182.             jockey.horse = horse
  183.             horse.is_taken = True
  184.             return f'Jockey {jockey_name} will ride the horse {horse.name}.'
  185.  
  186.     @staticmethod
  187.     def search_jockey_by_name(jockey_name_, all_jockeys_):
  188.         search_jockey = [j for j in all_jockeys_ if j.name == jockey_name_]
  189.         if not search_jockey:
  190.             raise Exception(f"Jockey {jockey_name_} could not be found!")
  191.         else:
  192.             return search_jockey[0]
  193.  
  194.     @staticmethod
  195.     def search_horse_by_type(horse_type_, all_horses_):
  196.         search_horse = [h for h in all_horses_ if type(h).__name__ == horse_type_ and not h.is_taken]
  197.         if not search_horse:
  198.             raise Exception(f"Horse breed {horse_type_} could not be found!")
  199.         else:
  200.             return search_horse[-1]
  201.  
  202.     def add_jockey_to_horse_race(self, race_type: str, jockey_name: str):
  203.         race = self.search_race_by_type(race_type, self.horse_races)
  204.         jockey = self.search_jockey_by_name(jockey_name, self.jockeys)
  205.         if jockey.horse is None:
  206.             raise Exception(f'Jockey {jockey_name} cannot race without a horse!')
  207.         if jockey not in race.jockeys:
  208.             race.jockeys.append(jockey)
  209.             return f"Jockey {jockey_name} added to the {race_type} race."
  210.         else:
  211.             return f'Jockey {jockey_name} has been already added to the {race_type} race.'
  212.  
  213.     @staticmethod
  214.     def search_race_by_type(race_type_, all_races_):
  215.         search_race = [r for r in all_races_ if r.race_type == race_type_]
  216.         if search_race:
  217.             return search_race[0]
  218.         else:
  219.             raise Exception(f"Race {race_type_} could not be found!")
  220.  
  221.     def start_horse_race(self, race_type: str):
  222.         race = self.search_race_by_type(race_type, self.horse_races)
  223.         if len(race.jockeys) < 2:
  224.             raise Exception(f'Horse race {race_type} needs at least two participants!')
  225.  
  226.         winner = sorted(race.jockeys, key=lambda x: -x.horse.speed)[0]
  227.         return f"The winner of the {race_type} race, with a speed of " \
  228.                f"{winner.horse.speed}km/h is {winner.name}! Winner's horse: {winner.horse.name}."
  229.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement