Advertisement
GeorgiLukanov87

First Steps in OOP - Exercise

Sep 23rd, 2022 (edited)
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.97 KB | None | 0 0
  1. # First Steps in OOP - Exercise
  2.  
  3. # https://judge.softuni.org/Contests/Practice/Index/1935#0
  4.  
  5. # 01. Shop
  6. # 02. Hero
  7. # 03. Employee
  8. # 04. Cup
  9. # 05. Flower
  10. # 06. Steam User
  11. # 07. Programmer
  12. # 08. *Pokemon Battle
  13.  
  14. --------------------------------------------------------------------------------------------------
  15.  
  16. # 01. Shop
  17.  
  18.  
  19. class Shop:
  20.     def __init__(self, name, items):
  21.         self.name = name
  22.         self.items = items
  23.  
  24.     def get_items_count(self):
  25.         return len(self.items)
  26.    
  27.  
  28. --------------------------------------------------------------------------------------------------
  29.  
  30. # 02. Hero
  31.  
  32.  
  33. class Hero:
  34.    
  35.     def __init__(self, name, health):
  36.         self.name = name
  37.         self.health = health
  38.  
  39.     def defend(self, damage):
  40.         self.health -= damage
  41.         if self.health <= 0:
  42.             self.health = 0
  43.             return f'{self.name} was defeated'
  44.  
  45.     def heal(self, amount):
  46.         self.health += amount
  47.  
  48.  
  49. --------------------------------------------------------------------------------------------------
  50.  
  51. # 03. Employee
  52.  
  53.  
  54. class Employee:
  55.     def __init__(self, id, first_name, last_name, salary):
  56.         self.id = id
  57.         self.first_name = first_name
  58.         self.last_name = last_name
  59.         self.salary = salary
  60.  
  61.     def get_full_name(self):
  62.         return f'{self.first_name} {self.last_name}'
  63.  
  64.     def raise_salary(self, amount):
  65.         self.salary += amount
  66.         return self.salary
  67.  
  68.     def get_annual_salary(self):
  69.         result = self.salary * 12
  70.         return result
  71.    
  72.  
  73. --------------------------------------------------------------------------------------------------
  74.  
  75.  
  76. # 04. Cup
  77.  
  78. class Cup:
  79.     def __init__(self, size, quantity):
  80.         self.size = size
  81.         self.quantity = quantity
  82.  
  83.  
  84.     def fill(self, milliliters):
  85.         if milliliters <= self.size - self.quantity:
  86.             self.quantity += milliliters
  87.  
  88.  
  89.     def status(self):
  90.         result = self.size - self.quantity
  91.         return result
  92.  
  93.  
  94. --------------------------------------------------------------------------------------------------
  95.  
  96. # 05. Flower
  97.  
  98.  
  99. class Flower:
  100.     def __init__(self, name, water_requirements, is_happy=False):
  101.         self.name = name
  102.         self.water_requirements = water_requirements
  103.         self.is_happy = is_happy
  104.  
  105.     def water(self, quantity):
  106.         if quantity >= self.water_requirements:
  107.             self.is_happy = True
  108.  
  109.     def status(self):
  110.         if self.is_happy:
  111.             return f'{self.name} is happy'
  112.         return f'{self.name} is not happy'
  113.    
  114.  
  115. --------------------------------------------------------------------------------------------------
  116.  
  117. # 06. Steam User
  118.  
  119.  
  120. class SteamUser:
  121.     def __init__(self, username, games, played_hours=0):
  122.         self.username = username
  123.         self.games = games
  124.         self.played_hours = played_hours
  125.  
  126.     def play(self, game, hours):
  127.         if game in self.games:
  128.             self.played_hours += hours
  129.             return f'{self.username} is playing {game}'
  130.         return f'{game} is not in library'
  131.  
  132.     def buy_game(self, game):
  133.         if game not in self.games:
  134.             self.games.append(game)
  135.             return f'{self.username} bought {game}'
  136.         return f'{game} is already in your library'
  137.  
  138.     def status(self):
  139.         return f"{self.username} has {len(self.games)} games. Total play time: {self.played_hours}"
  140.    
  141.  
  142. --------------------------------------------------------------------------------------------------
  143.  
  144. # 07. Programmer
  145.  
  146.  
  147. class Programmer:
  148.     def __init__(self, name, language, skills):
  149.         self.name = name
  150.         self.language = language
  151.         self.skills = skills
  152.  
  153.     def watch_course(self, course_name, language, skills_earned):
  154.         if language in self.language:
  155.             self.skills += skills_earned
  156.             return f'{self.name} watched {course_name}'
  157.         return f'{self.name} does not know {language}'
  158.  
  159.     def change_language(self, new_language, skills_needed):
  160.         if skills_needed <= self.skills and new_language != self.language:
  161.             result = f'{self.name} switched from {self.language} to {new_language}'
  162.             self.language = new_language
  163.             return result
  164.  
  165.         elif skills_needed <= self.skills and new_language == self.language:
  166.             return f'{self.name} already knows {new_language}'
  167.  
  168.         elif skills_needed > self.skills:
  169.             return f'{self.name} needs {skills_needed - self.skills} more skills'
  170.  
  171. --------------------------------------------------------------------------------------------------
  172.  
  173.  
  174. # 08. *Pokemon Battle
  175.  
  176. # file name : pokemon.py
  177.  
  178. class Pokemon:
  179.     def __init__(self, name, health):
  180.         self.name = name
  181.         self.health = health
  182.  
  183.     def pokemon_details(self):
  184.         return f"{self.name} with health {self.health}"
  185.    
  186. ================================================================================================
  187.  
  188. # file name: trainer.py
  189.  
  190. from project.pokemon import Pokemon
  191.  
  192.  
  193. class Trainer:
  194.     def __init__(self, name):
  195.         self.name = name
  196.         self.pokemons = []
  197.  
  198.     def add_pokemon(self, pokemon: Pokemon):
  199.         if pokemon in self.pokemons:
  200.             return 'This pokemon is already caught'
  201.         self.pokemons.append(pokemon)
  202.         return f'Caught {pokemon.pokemon_details()}'
  203.  
  204.     def release_pokemon(self, pokemon_name: str):
  205.         for current_pokemon in self.pokemons:
  206.             if current_pokemon.name == pokemon_name:
  207.                 self.pokemons.remove(current_pokemon)
  208.                 return f'You have released {pokemon_name}'
  209.         return f'Pokemon is not caught'
  210.  
  211.     def trainer_data(self):
  212.         final_print = f"Pokemon Trainer {self.name}\nPokemon count {len(self.pokemons)}"
  213.         for pokemon in self.pokemons:
  214.             final_print += '\n' + f'- {pokemon.pokemon_details()}'
  215.  
  216.         return final_print
  217.  
  218. ------------------------------------------------------------------
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement