Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # First Steps in OOP - Exercise
- # https://judge.softuni.org/Contests/Practice/Index/1935#0
- # 01. Shop
- # 02. Hero
- # 03. Employee
- # 04. Cup
- # 05. Flower
- # 06. Steam User
- # 07. Programmer
- # 08. *Pokemon Battle
- --------------------------------------------------------------------------------------------------
- # 01. Shop
- class Shop:
- def __init__(self, name, items):
- self.name = name
- self.items = items
- def get_items_count(self):
- return len(self.items)
- --------------------------------------------------------------------------------------------------
- # 02. Hero
- class Hero:
- def __init__(self, name, health):
- self.name = name
- self.health = health
- def defend(self, damage):
- self.health -= damage
- if self.health <= 0:
- self.health = 0
- return f'{self.name} was defeated'
- def heal(self, amount):
- self.health += amount
- --------------------------------------------------------------------------------------------------
- # 03. Employee
- class Employee:
- def __init__(self, id, first_name, last_name, salary):
- self.id = id
- self.first_name = first_name
- self.last_name = last_name
- self.salary = salary
- def get_full_name(self):
- return f'{self.first_name} {self.last_name}'
- def raise_salary(self, amount):
- self.salary += amount
- return self.salary
- def get_annual_salary(self):
- result = self.salary * 12
- return result
- --------------------------------------------------------------------------------------------------
- # 04. Cup
- class Cup:
- def __init__(self, size, quantity):
- self.size = size
- self.quantity = quantity
- def fill(self, milliliters):
- if milliliters <= self.size - self.quantity:
- self.quantity += milliliters
- def status(self):
- result = self.size - self.quantity
- return result
- --------------------------------------------------------------------------------------------------
- # 05. Flower
- class Flower:
- def __init__(self, name, water_requirements, is_happy=False):
- self.name = name
- self.water_requirements = water_requirements
- self.is_happy = is_happy
- def water(self, quantity):
- if quantity >= self.water_requirements:
- self.is_happy = True
- def status(self):
- if self.is_happy:
- return f'{self.name} is happy'
- return f'{self.name} is not happy'
- --------------------------------------------------------------------------------------------------
- # 06. Steam User
- class SteamUser:
- def __init__(self, username, games, played_hours=0):
- self.username = username
- self.games = games
- self.played_hours = played_hours
- def play(self, game, hours):
- if game in self.games:
- self.played_hours += hours
- return f'{self.username} is playing {game}'
- return f'{game} is not in library'
- def buy_game(self, game):
- if game not in self.games:
- self.games.append(game)
- return f'{self.username} bought {game}'
- return f'{game} is already in your library'
- def status(self):
- return f"{self.username} has {len(self.games)} games. Total play time: {self.played_hours}"
- --------------------------------------------------------------------------------------------------
- # 07. Programmer
- class Programmer:
- def __init__(self, name, language, skills):
- self.name = name
- self.language = language
- self.skills = skills
- def watch_course(self, course_name, language, skills_earned):
- if language in self.language:
- self.skills += skills_earned
- return f'{self.name} watched {course_name}'
- return f'{self.name} does not know {language}'
- def change_language(self, new_language, skills_needed):
- if skills_needed <= self.skills and new_language != self.language:
- result = f'{self.name} switched from {self.language} to {new_language}'
- self.language = new_language
- return result
- elif skills_needed <= self.skills and new_language == self.language:
- return f'{self.name} already knows {new_language}'
- elif skills_needed > self.skills:
- return f'{self.name} needs {skills_needed - self.skills} more skills'
- --------------------------------------------------------------------------------------------------
- # 08. *Pokemon Battle
- # file name : pokemon.py
- class Pokemon:
- def __init__(self, name, health):
- self.name = name
- self.health = health
- def pokemon_details(self):
- return f"{self.name} with health {self.health}"
- ================================================================================================
- # file name: trainer.py
- from project.pokemon import Pokemon
- class Trainer:
- def __init__(self, name):
- self.name = name
- self.pokemons = []
- def add_pokemon(self, pokemon: Pokemon):
- if pokemon in self.pokemons:
- return 'This pokemon is already caught'
- self.pokemons.append(pokemon)
- return f'Caught {pokemon.pokemon_details()}'
- def release_pokemon(self, pokemon_name: str):
- for current_pokemon in self.pokemons:
- if current_pokemon.name == pokemon_name:
- self.pokemons.remove(current_pokemon)
- return f'You have released {pokemon_name}'
- return f'Pokemon is not caught'
- def trainer_data(self):
- final_print = f"Pokemon Trainer {self.name}\nPokemon count {len(self.pokemons)}"
- for pokemon in self.pokemons:
- final_print += '\n' + f'- {pokemon.pokemon_details()}'
- return final_print
- ------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement