Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Classes and Objects - Exercise
- # https://judge.softuni.org/Contests/Practice/Index/1937#0
- # 01. Vet
- # 02. Time
- # 03. Account
- # 04. Pizza Delivery
- # 05. To-do List
- # 06. Guild System
- # 07. Spoopify
- # 08. *Library JUDGE - 85/100
- ------------------------------------------------------------------------------------------
- _______________
- | |
- | 01. Vet |
- |_______________|
- class Vet:
- animals = []
- space = 5
- def __init__(self, name):
- self.name = name
- self.animals = []
- def register_animal(self, animal_name):
- if Vet.space > 0:
- Vet.animals.append(animal_name)
- Vet.space -= 1
- self.animals.append(animal_name)
- return f"{animal_name} registered in the clinic"
- return f"Not enough space"
- def unregister_animal(self, animal_name):
- if animal_name in self.animals:
- self.animals.remove(animal_name)
- Vet.animals.remove(animal_name)
- Vet.space += 1
- return f"{animal_name} unregistered successfully"
- return f"{animal_name} not in the clinic"
- def info(self):
- return f"{self.name} has {len(self.animals)} animals. {Vet.space} space left in clinic"
- ------------------------------------------------------------------------------------------
- ________________
- | |
- | 02. Time |
- |________________|
- class Time:
- max_hours = 23
- max_minutes = 59
- max_seconds = 59
- def __init__(self, hours, minutes, seconds):
- self.hours = hours
- self.minutes = minutes
- self.seconds = seconds
- def set_time(self, hours, minutes, seconds):
- self.hours = hours
- self.minutes = minutes
- self.seconds = seconds
- def get_time(self):
- return f"{self.hours:02d}:{self.minutes:02d}:{self.seconds:02d}"
- def next_second(self):
- self.seconds += 1
- if self.seconds + 1 > Time.max_seconds:
- self.seconds = 0
- self.minutes += 1
- if self.minutes > Time.max_minutes:
- self.minutes = 0
- self.hours += 1
- if self.hours > Time.max_hours:
- self.hours = 0
- return self.get_time()
- ------------------------------------------------------------------------------------------
- ___________________
- | |
- | 03. Account |
- |___________________|
- class Account:
- def __init__(self, id, name, balance=0):
- self.id = id
- self.name = name
- self.balance = balance
- def credit(self, amount):
- self.balance += amount
- return self.balance
- def debit(self, amount):
- if amount <= self.balance:
- self.balance -= amount
- return self.balance
- return f"Amount exceeded balance"
- def info(self):
- return f"User {self.name} with account {self.id} has {self.balance} balance"
- ------------------------------------------------------------------------------------------
- __________________________
- | |
- | 04. Pizza Delivery |
- |__________________________|
- class PizzaDelivery:
- def __init__(self, name, price, ingredients):
- self.name = name
- self.price = price
- self.ingredients = ingredients
- self.ordered = False
- def add_extra(self, ingredients: str, quantity: int, price_per_quantity: float):
- if self.ordered:
- return f"Pizza {self.name} already prepared, and we can't make any changes!"
- if ingredients in self.ingredients:
- self.ingredients[ingredients] += quantity
- self.price += quantity * price_per_quantity
- else:
- self.ingredients[ingredients] = quantity
- self.price += quantity * price_per_quantity
- def remove_ingredient(self, ingredient: str, quantity: int, price_per_quantity: float):
- if self.ordered:
- return f"Pizza {self.name} already prepared, and we can't make any changes!"
- if ingredient not in self.ingredients:
- return f"Wrong ingredient selected! We do not use {ingredient} in {self.name}!"
- else:
- if quantity > self.ingredients[ingredient]:
- return f"Please check again the desired quantity of {ingredient}!"
- self.ingredients[ingredient] -= quantity
- self.price -= quantity * price_per_quantity
- def make_order(self):
- self.ordered = True
- return f"You've ordered pizza {self.name} prepared with " \
- f"{', '.join([f'{k}: {v}' for k, v in self.ingredients.items()])} and the price will be {self.price}lv."
- ------------------------------------------------------------------------------------------
- ______________________
- | |
- | 05. To-do List |
- |______________________|
- # file name : task.py
- class Task:
- def __init__(self, name: str, due_date: str):
- self.name = name
- self.due_date = due_date
- self.comments = []
- self.completed = False
- def change_name(self, new_name: str):
- if new_name != self.name:
- self.name = new_name
- return self.name
- return 'Name cannot be the same.'
- def change_due_date(self, new_date: str):
- if new_date != self.due_date:
- self.due_date = new_date
- return self.due_date
- return 'Date cannot be the same.'
- def add_comment(self, comment: str):
- self.comments.append(comment)
- def edit_comment(self, comment_number: int, new_comment: str):
- if 0 <= comment_number < len(self.comments):
- self.comments = new_comment
- return '\n'.join(self.comments.split(', '))
- return 'Cannot find comment.'
- def details(self):
- return f'Name: {self.name} - Due Date: {self.due_date}'
- ============================================================================================
- # file name: section.py
- from project.task import Task
- class Section:
- def __init__(self, name):
- self.name = name
- self.tasks = []
- def add_task(self, new_task: Task):
- if new_task in self.tasks:
- return f"Task is already in the section {self.name}"
- self.tasks.append(new_task)
- return f"Task {new_task.details()} is added to the section"
- def complete_task(self, task_name: str):
- for current_task in self.tasks:
- if current_task.name == task_name:
- current_task.completed = True
- return f"Completed task {task_name}"
- return f"Could not find task with the name {task_name}"
- def clean_section(self):
- start_len = len(self.tasks)
- self.tasks = [t for t in self.tasks if not t.completed]
- return f'Cleared {start_len - len(self.tasks)} tasks.'
- def view_section(self):
- result = f'Section {self.name}:'
- for current_task in self.tasks:
- result += '\n' + f"{current_task.details()}"
- return result.strip()
- ------------------------------------------------------------------------------------------
- ________________________
- | |
- | 06. Guild System |
- |________________________|
- # file name : guild.py
- from .player import Player
- class Guild:
- def __init__(self, name: str):
- self.name = name
- self.players = []
- def assign_player(self, player: Player):
- if player.guild != 'Unaffiliated' and player.name not in [existing.name for existing in self.players]:
- return f"Player {player.name} is in another guild."
- if player.name in [existing.name for existing in self.players]:
- return f"Player {player.name} is already in the guild."
- self.players.append(player)
- player.guild = f'{self.name}'
- return f"Welcome player {player.name} to the guild {self.name}"
- def kick_player(self, player_name: str):
- for player in self.players:
- if player.name == player_name:
- player.guild = 'Unaffiliated'
- return f"Player {player_name} has been removed from the guild."
- return f"Player {player_name} is not in the guild."
- def guild_info(self):
- result = f"Guild: {self.name}\n"
- for player_class in self.players:
- result += f"{Player.player_info(player_class)}\n"
- return result
- ============================================================================================
- # file name: player.py
- class Player:
- def __init__(self, name: str, hp: int, mp: int):
- self.name = name
- self.hp = hp
- self.mp = mp
- self.skills = {}
- self.guild = 'Unaffiliated'
- def add_skill(self, skill_name, mana_cost):
- if skill_name not in self.skills:
- self.skills[skill_name] = mana_cost
- return f"Skill {skill_name} added to the collection of the player {self.name}"
- return "Skill already added"
- def player_info(self):
- result = ""
- result += f'Name: {self.name}\nGuild: {self.guild}\nHP: {self.hp}\nMP: {self.mp}\n'
- result += f"{', '.join([f'==={hp} - {mp}' for hp, mp in self.skills.items()])}"
- return result
- ------------------------------------------------------------------------------------------
- ____________________
- | |
- | 07. Spoopify |
- |____________________|
- # file name: song.py
- class Song:
- def __init__(self, name: str, length: float, single):
- self.name = name
- self.length = length
- self.single = single
- def get_info(self):
- return f'{self.name} - {self.length}'
- ============================================================================================
- # file name: album.py
- from project.song import Song
- class Album:
- def __init__(self, name: str, *song):
- self.name = name
- self.songs = list(song)
- self.published = False
- def add_song(self, song: Song):
- if song.single:
- return f"Cannot add {self.name}. It's a single"
- if self.published:
- return f"Cannot add songs. Album is published."
- if song in self.songs:
- return f"Song is already in the album."
- self.songs.append(song)
- return f"Song {song.name} has been added to the album {self.name}."
- def remove_song(self, song_name: str):
- if self.published:
- return f"Cannot remove songs. Album is published."
- for current_song in self.songs:
- if current_song.name == song_name:
- self.songs.remove(current_song)
- return f"Removed song {song_name} from album {self.name}."
- return f"Song is not in the album."
- def publish(self):
- if not self.published:
- self.published = True
- return f"Album {self.name} has been published."
- return f"Album {self.name} is already published."
- def details(self):
- result = ""
- result += f"Album {self.name}\n"
- for song in self.songs:
- result += f"== {song.get_info()}\n"
- return result
- ============================================================================================
- # file name: band.py
- from project.album import Album
- class Band:
- def __init__(self, name: str):
- self.name = name
- self.albums = []
- def add_album(self, album: Album):
- if album in self.albums:
- return f"Band {self.name} already has {album.name} in their library."
- self.albums.append(album)
- return f"Band {self.name} has added their newest album {album.name}."
- def remove_album(self, album_name: str):
- for current_album in self.albums:
- if current_album.name == album_name:
- if current_album.published:
- return f"Album has been published. It cannot be removed."
- self.albums.remove(current_album)
- return f"Album {album_name} has been removed."
- return f"Album {album_name} is not found."
- def details(self):
- result = ""
- result += f'Band {self.name}\n'
- for album in self.albums:
- result += f"{album.details()}\n"
- return result
- ------------------------------------------------------------------------------------------
- ___________________________________
- | |
- | *08. Library JUDGE - 85/100 |
- |___________________________________|
- #file name: library.py
- from project.user import User
- class Library:
- def __init__(self):
- self.user_records = [] # Storing users objects/instances !!!
- self.books_available = {} # 'author': [book1,book2...]
- self.rented_books = {} # {usernames: {book names: days to return}}
- def get_book(self, author: str, book_name: str, days_to_return: int, user: User):
- if book_name in self.books_available[author]:
- user.books.append(book_name)
- self.books_available[author].remove(book_name)
- if user.username not in self.rented_books:
- self.rented_books[user.username] = {book_name: days_to_return}
- else:
- self.rented_books[user.username] |= {book_name: days_to_return}
- return f"{book_name} successfully rented for the next {days_to_return} days!"
- return f'The book "{book_name}" is already rented and ' \
- f'will be available in {self.rented_books[user.username][book_name]} days!'
- def return_book(self, author: str, book_name: str, user: User):
- if book_name in user.books:
- self.books_available[author].append(book_name)
- del self.rented_books[user.username][book_name]
- user.books.remove(book_name)
- else:
- return f"{user.username} doesn't have this book in his/her records!"
- =======================================================================================================
- # file name: registration.py
- from project.library import Library
- from project.user import User
- class Registration:
- def add_user(self, user: User, library: Library):
- if user in library.user_records:
- return f"User with id = {user.user_id} already registered in the library!"
- library.user_records.append(user)
- def remove_user(self, user: User, library: Library):
- if user not in library.user_records:
- return "We could not find such user to remove!"
- library.user_records.remove(user)
- def change_username(self, user_id: int, new_username: str, library: Library):
- for current_user in library.user_records:
- if current_user.user_id == user_id:
- for current_username in library.user_records:
- if current_username.username != new_username:
- for change_user in library.user_records:
- if change_user.user_id == user_id:
- change_user.username = new_username
- if current_username.username in library.rented_books:
- library.rented_books[new_username] = library.rented_books.pop(current_username.username)
- return f"Username successfully changed to: {new_username} for user id: {user_id}"
- for current_user in library.user_records:
- if current_user.user_id == user_id:
- return f"Please check again the provided username - it " \
- f"should be different than the username used so far!"
- return f"There is no user with id = {user_id}!"
- =======================================================================================================
- # file name: user.py
- class User:
- def __init__(self, user_id: int, username: str):
- self.user_id = user_id
- self.username = username
- self.books = []
- def info(self):
- return ', '.join(sorted(self.books))
- def __str__(self):
- return f"{self.user_id}, {self.username}, {self.books}"
- -------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement