Advertisement
GeorgiLukanov87

Classes and Objects - Exercise

Sep 26th, 2022 (edited)
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.19 KB | None | 0 0
  1. # Classes and Objects - Exercise
  2.  
  3. # https://judge.softuni.org/Contests/Practice/Index/1937#0
  4.  
  5. # 01. Vet
  6. # 02. Time
  7. # 03. Account
  8. # 04. Pizza Delivery
  9. # 05. To-do List
  10. # 06. Guild System
  11. # 07. Spoopify
  12. # 08. *Library JUDGE - 85/100
  13. ------------------------------------------------------------------------------------------
  14.  _______________
  15. |               |
  16. |    01. Vet    |
  17. |_______________|
  18.  
  19. class Vet:
  20.     animals = []
  21.     space = 5
  22.  
  23.     def __init__(self, name):
  24.         self.name = name
  25.         self.animals = []
  26.  
  27.     def register_animal(self, animal_name):
  28.         if Vet.space > 0:
  29.             Vet.animals.append(animal_name)
  30.             Vet.space -= 1
  31.             self.animals.append(animal_name)
  32.             return f"{animal_name} registered in the clinic"
  33.         return f"Not enough space"
  34.  
  35.     def unregister_animal(self, animal_name):
  36.         if animal_name in self.animals:
  37.             self.animals.remove(animal_name)
  38.             Vet.animals.remove(animal_name)
  39.             Vet.space += 1
  40.             return f"{animal_name} unregistered successfully"
  41.         return f"{animal_name} not in the clinic"
  42.  
  43.     def info(self):
  44.         return f"{self.name} has {len(self.animals)} animals. {Vet.space} space left in clinic"
  45.  
  46.  
  47. ------------------------------------------------------------------------------------------
  48.  ________________
  49. |                |
  50. |    02. Time    |
  51. |________________|
  52.  
  53.  
  54. class Time:
  55.     max_hours = 23
  56.     max_minutes = 59
  57.     max_seconds = 59
  58.  
  59.     def __init__(self, hours, minutes, seconds):
  60.         self.hours = hours
  61.         self.minutes = minutes
  62.         self.seconds = seconds
  63.  
  64.     def set_time(self, hours, minutes, seconds):
  65.         self.hours = hours
  66.         self.minutes = minutes
  67.         self.seconds = seconds
  68.  
  69.     def get_time(self):
  70.         return f"{self.hours:02d}:{self.minutes:02d}:{self.seconds:02d}"
  71.  
  72.     def next_second(self):
  73.         self.seconds += 1
  74.         if self.seconds + 1 > Time.max_seconds:
  75.             self.seconds = 0
  76.             self.minutes += 1
  77.         if self.minutes > Time.max_minutes:
  78.             self.minutes = 0
  79.             self.hours += 1
  80.         if self.hours > Time.max_hours:
  81.             self.hours = 0
  82.  
  83.         return self.get_time()
  84.  
  85.  
  86. ------------------------------------------------------------------------------------------
  87.  ___________________
  88. |                   |
  89. |    03. Account    |
  90. |___________________|
  91.  
  92.  
  93. class Account:
  94.     def __init__(self, id, name, balance=0):
  95.         self.id = id
  96.         self.name = name
  97.         self.balance = balance
  98.  
  99.     def credit(self, amount):
  100.         self.balance += amount
  101.         return self.balance
  102.  
  103.     def debit(self, amount):
  104.         if amount <= self.balance:
  105.             self.balance -= amount
  106.             return self.balance
  107.         return f"Amount exceeded balance"
  108.  
  109.     def info(self):
  110.         return f"User {self.name} with account {self.id} has {self.balance} balance"
  111.  
  112.  
  113. ------------------------------------------------------------------------------------------
  114.  __________________________
  115. |                          |
  116. |    04. Pizza Delivery    |
  117. |__________________________|
  118.  
  119.  
  120. class PizzaDelivery:
  121.  
  122.     def __init__(self, name, price, ingredients):
  123.         self.name = name
  124.         self.price = price
  125.         self.ingredients = ingredients
  126.         self.ordered = False
  127.  
  128.     def add_extra(self, ingredients: str, quantity: int, price_per_quantity: float):
  129.         if self.ordered:
  130.             return f"Pizza {self.name} already prepared, and we can't make any changes!"
  131.  
  132.         if ingredients in self.ingredients:
  133.             self.ingredients[ingredients] += quantity
  134.             self.price += quantity * price_per_quantity
  135.         else:
  136.             self.ingredients[ingredients] = quantity
  137.             self.price += quantity * price_per_quantity
  138.  
  139.     def remove_ingredient(self, ingredient: str, quantity: int, price_per_quantity: float):
  140.         if self.ordered:
  141.             return f"Pizza {self.name} already prepared, and we can't make any changes!"
  142.  
  143.         if ingredient not in self.ingredients:
  144.             return f"Wrong ingredient selected! We do not use {ingredient} in {self.name}!"
  145.         else:
  146.             if quantity > self.ingredients[ingredient]:
  147.                 return f"Please check again the desired quantity of {ingredient}!"
  148.             self.ingredients[ingredient] -= quantity
  149.             self.price -= quantity * price_per_quantity
  150.  
  151.  
  152.     def make_order(self):
  153.         self.ordered = True
  154.         return f"You've ordered pizza {self.name} prepared with " \
  155.                f"{', '.join([f'{k}: {v}' for k, v in self.ingredients.items()])} and the price will be {self.price}lv."
  156.    
  157.  
  158. ------------------------------------------------------------------------------------------
  159.  ______________________
  160. |                      |
  161. |    05. To-do List    |
  162. |______________________|
  163.  
  164. # file name : task.py
  165.  
  166. class Task:
  167.  
  168.     def __init__(self, name: str, due_date: str):
  169.         self.name = name
  170.         self.due_date = due_date
  171.         self.comments = []
  172.         self.completed = False
  173.  
  174.     def change_name(self, new_name: str):
  175.         if new_name != self.name:
  176.             self.name = new_name
  177.             return self.name
  178.         return 'Name cannot be the same.'
  179.  
  180.     def change_due_date(self, new_date: str):
  181.         if new_date != self.due_date:
  182.             self.due_date = new_date
  183.             return self.due_date
  184.         return 'Date cannot be the same.'
  185.  
  186.     def add_comment(self, comment: str):
  187.         self.comments.append(comment)
  188.  
  189.     def edit_comment(self, comment_number: int, new_comment: str):
  190.         if 0 <= comment_number < len(self.comments):
  191.             self.comments = new_comment
  192.             return '\n'.join(self.comments.split(', '))
  193.         return 'Cannot find comment.'
  194.  
  195.     def details(self):
  196.         return f'Name: {self.name} - Due Date: {self.due_date}'
  197.  
  198.  
  199. ============================================================================================
  200. # file name: section.py
  201.  
  202. from project.task import Task
  203.  
  204.  
  205. class Section:
  206.  
  207.     def __init__(self, name):
  208.         self.name = name
  209.         self.tasks = []
  210.  
  211.     def add_task(self, new_task: Task):
  212.         if new_task in self.tasks:
  213.             return f"Task is already in the section {self.name}"
  214.         self.tasks.append(new_task)
  215.         return f"Task {new_task.details()} is added to the section"
  216.  
  217.     def complete_task(self, task_name: str):
  218.         for current_task in self.tasks:
  219.             if current_task.name == task_name:
  220.                 current_task.completed = True
  221.             return f"Completed task {task_name}"
  222.         return f"Could not find task with the name {task_name}"
  223.  
  224.     def clean_section(self):
  225.         start_len = len(self.tasks)
  226.         self.tasks = [t for t in self.tasks if not t.completed]
  227.         return f'Cleared {start_len - len(self.tasks)} tasks.'
  228.  
  229.     def view_section(self):
  230.         result = f'Section {self.name}:'
  231.         for current_task in self.tasks:
  232.             result += '\n' + f"{current_task.details()}"
  233.         return result.strip()
  234.  
  235. ------------------------------------------------------------------------------------------
  236.  ________________________
  237. |                        |
  238. |    06. Guild System    |
  239. |________________________|
  240.  
  241. # file name : guild.py
  242.  
  243. from .player import Player
  244.  
  245.  
  246. class Guild:
  247.     def __init__(self, name: str):
  248.         self.name = name
  249.         self.players = []
  250.  
  251.     def assign_player(self, player: Player):
  252.         if player.guild != 'Unaffiliated' and player.name not in [existing.name for existing in self.players]:
  253.             return f"Player {player.name} is in another guild."
  254.         if player.name in [existing.name for existing in self.players]:
  255.             return f"Player {player.name} is already in the guild."
  256.         self.players.append(player)
  257.         player.guild = f'{self.name}'
  258.         return f"Welcome player {player.name} to the guild {self.name}"
  259.  
  260.     def kick_player(self, player_name: str):
  261.         for player in self.players:
  262.             if player.name == player_name:
  263.                 player.guild = 'Unaffiliated'
  264.                 return f"Player {player_name} has been removed from the guild."
  265.         return f"Player {player_name} is not in the guild."
  266.  
  267.     def guild_info(self):
  268.         result = f"Guild: {self.name}\n"
  269.         for player_class in self.players:
  270.             result += f"{Player.player_info(player_class)}\n"
  271.         return result
  272.    
  273. ============================================================================================
  274. # file name: player.py
  275.  
  276. class Player:
  277.     def __init__(self, name: str, hp: int, mp: int):
  278.         self.name = name
  279.         self.hp = hp
  280.         self.mp = mp
  281.         self.skills = {}
  282.         self.guild = 'Unaffiliated'
  283.  
  284.     def add_skill(self, skill_name, mana_cost):
  285.         if skill_name not in self.skills:
  286.             self.skills[skill_name] = mana_cost
  287.             return f"Skill {skill_name} added to the collection of the player {self.name}"
  288.         return "Skill already added"
  289.  
  290.     def player_info(self):
  291.         result = ""
  292.         result += f'Name: {self.name}\nGuild: {self.guild}\nHP: {self.hp}\nMP: {self.mp}\n'
  293.         result += f"{', '.join([f'==={hp} - {mp}' for hp, mp in self.skills.items()])}"
  294.         return result
  295.  
  296. ------------------------------------------------------------------------------------------
  297.  ____________________
  298. |                    |
  299. |    07. Spoopify    |
  300. |____________________|
  301.  
  302. # file name: song.py
  303.  
  304. class Song:
  305.     def __init__(self, name: str, length: float, single):
  306.         self.name = name
  307.         self.length = length
  308.         self.single = single
  309.  
  310.     def get_info(self):
  311.         return f'{self.name} - {self.length}'
  312.  
  313. ============================================================================================
  314. # file name: album.py
  315.  
  316. from project.song import Song
  317.  
  318.  
  319. class Album:
  320.     def __init__(self, name: str, *song):
  321.         self.name = name
  322.         self.songs = list(song)
  323.         self.published = False
  324.  
  325.     def add_song(self, song: Song):
  326.         if song.single:
  327.             return f"Cannot add {self.name}. It's a single"
  328.         if self.published:
  329.             return f"Cannot add songs. Album is published."
  330.         if song in self.songs:
  331.             return f"Song is already in the album."
  332.         self.songs.append(song)
  333.         return f"Song {song.name} has been added to the album {self.name}."
  334.  
  335.     def remove_song(self, song_name: str):
  336.         if self.published:
  337.             return f"Cannot remove songs. Album is published."
  338.         for current_song in self.songs:
  339.             if current_song.name == song_name:
  340.                 self.songs.remove(current_song)
  341.                 return f"Removed song {song_name} from album {self.name}."
  342.         return f"Song is not in the album."
  343.  
  344.     def publish(self):
  345.         if not self.published:
  346.             self.published = True
  347.             return f"Album {self.name} has been published."
  348.         return f"Album {self.name} is already published."
  349.  
  350.     def details(self):
  351.         result = ""
  352.         result += f"Album {self.name}\n"
  353.         for song in self.songs:
  354.             result += f"== {song.get_info()}\n"
  355.         return result
  356.    
  357. ============================================================================================
  358.  
  359. # file name: band.py
  360.  
  361. from project.album import Album
  362.  
  363.  
  364. class Band:
  365.     def __init__(self, name: str):
  366.         self.name = name
  367.         self.albums = []
  368.  
  369.     def add_album(self, album: Album):
  370.         if album in self.albums:
  371.             return f"Band {self.name} already has {album.name} in their library."
  372.         self.albums.append(album)
  373.         return f"Band {self.name} has added their newest album {album.name}."
  374.  
  375.     def remove_album(self, album_name: str):
  376.         for current_album in self.albums:
  377.             if current_album.name == album_name:
  378.                 if current_album.published:
  379.                     return f"Album has been published. It cannot be removed."
  380.                 self.albums.remove(current_album)
  381.                 return f"Album {album_name} has been removed."
  382.         return f"Album {album_name} is not found."
  383.  
  384.     def details(self):
  385.         result = ""
  386.         result += f'Band {self.name}\n'
  387.         for album in self.albums:
  388.             result += f"{album.details()}\n"
  389.         return result
  390.  
  391. ------------------------------------------------------------------------------------------
  392.  ___________________________________
  393. |                                   |
  394. |    *08. Library JUDGE - 85/100    |
  395. |___________________________________|
  396.  
  397.  
  398. #file name: library.py
  399.  
  400. from project.user import User
  401.  
  402.  
  403. class Library:
  404.     def __init__(self):
  405.         self.user_records = []  # Storing users objects/instances !!!
  406.         self.books_available = {}  # 'author': [book1,book2...]
  407.         self.rented_books = {}  # {usernames: {book names: days to return}}
  408.  
  409.     def get_book(self, author: str, book_name: str, days_to_return: int, user: User):
  410.         if book_name in self.books_available[author]:
  411.             user.books.append(book_name)
  412.             self.books_available[author].remove(book_name)
  413.             if user.username not in self.rented_books:
  414.                 self.rented_books[user.username] = {book_name: days_to_return}
  415.             else:
  416.                 self.rented_books[user.username] |= {book_name: days_to_return}
  417.             return f"{book_name} successfully rented for the next {days_to_return} days!"
  418.  
  419.         return f'The book "{book_name}" is already rented and ' \
  420.                f'will be available in {self.rented_books[user.username][book_name]} days!'
  421.  
  422.     def return_book(self, author: str, book_name: str, user: User):
  423.         if book_name in user.books:
  424.             self.books_available[author].append(book_name)
  425.             del self.rented_books[user.username][book_name]
  426.             user.books.remove(book_name)
  427.         else:
  428.             return f"{user.username} doesn't have this book in his/her records!"
  429.  
  430. =======================================================================================================
  431. # file name: registration.py
  432.  
  433. from project.library import Library
  434. from project.user import User
  435.  
  436.  
  437. class Registration:
  438.  
  439.     def add_user(self, user: User, library: Library):
  440.         if user in library.user_records:
  441.             return f"User with id = {user.user_id} already registered in the library!"
  442.         library.user_records.append(user)
  443.  
  444.     def remove_user(self, user: User, library: Library):
  445.         if user not in library.user_records:
  446.             return "We could not find such user to remove!"
  447.         library.user_records.remove(user)
  448.  
  449.     def change_username(self, user_id: int, new_username: str, library: Library):
  450.         for current_user in library.user_records:
  451.             if current_user.user_id == user_id:
  452.                 for current_username in library.user_records:
  453.                     if current_username.username != new_username:
  454.                         for change_user in library.user_records:
  455.                             if change_user.user_id == user_id:
  456.                                 change_user.username = new_username
  457.                         if current_username.username in library.rented_books:
  458.                             library.rented_books[new_username] = library.rented_books.pop(current_username.username)
  459.                         return f"Username successfully changed to: {new_username} for user id: {user_id}"
  460.  
  461.         for current_user in library.user_records:
  462.             if current_user.user_id == user_id:
  463.                 return f"Please check again the provided username - it " \
  464.                        f"should be different than the username used so far!"
  465.             return f"There is no user with id = {user_id}!"
  466.  
  467. =======================================================================================================
  468.  
  469. # file name: user.py
  470.  
  471. class User:
  472.     def __init__(self, user_id: int, username: str):
  473.         self.user_id = user_id
  474.         self.username = username
  475.         self.books = []
  476.  
  477.     def info(self):
  478.         return ', '.join(sorted(self.books))
  479.  
  480.     def __str__(self):
  481.         return f"{self.user_id}, {self.username}, {self.books}"
  482.  
  483. -------------------------------------------------------------------------------------------------------------
  484.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement