Advertisement
GeorgiLukanov87

Objects and Classes - Lab and Exericse 100/100

Jul 3rd, 2022 (edited)
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.20 KB | None | 0 0
  1. # All Objects and Classes - Lab and Exericse - Python Fundamentals 100/100.
  2.  
  3.  
  4. #Objects and Classes - Lab
  5.  
  6. ////////////////////////////////////////////////////////////////////////////
  7. #01. Comment
  8.  
  9. class Comment:
  10.     def __init__(self, username, content, likes=0):
  11.         self.username = username
  12.         self.content = content
  13.         self.likes = likes
  14.  
  15. ////////////////////////////////////////////////////////////////////////////
  16. # 02. Party
  17.  
  18. class Party:
  19.     def __init__(self):
  20.         self.people = []
  21.  
  22.  
  23. total_people = Party()
  24. command_line = input()
  25. while not command_line == 'End':
  26.     current_name = command_line
  27.     total_people.people.append(current_name)
  28.     command_line = input()
  29.  
  30. print(f'Going: {", ".join(total_people.people)}')
  31. print(f"Total: {len(total_people.people)}")
  32.  
  33. ////////////////////////////////////////////////////////////////////////////
  34. # 03. Email
  35.  
  36. class Email:
  37.     def __init__(self, sender, receiver, content):
  38.         self.sender = sender
  39.         self.receiver = receiver
  40.         self.content = content
  41.         self.is_sent = False
  42.  
  43.     def get_info(self):
  44.         return f"{self.sender} says to {self.receiver}: " \
  45.                f"{self.content}. Sent: {self.is_sent}"
  46.  
  47.     def send(self):
  48.         self.is_sent = True
  49.  
  50.  
  51. message = input()
  52. emails_data = []
  53. while not message == 'Stop':
  54.     details = message.split(' ')
  55.     sender = details[0]
  56.     receiver = details[1]
  57.     content = details[2]
  58.     all_info = Email(sender, receiver, content)
  59.     emails_data.append(all_info)
  60.     message = input()
  61.  
  62. sent_emails = list(map(int, input().split(', ')))
  63.  
  64. for x in sent_emails:
  65.     emails_data[x].send()
  66.  
  67. for email in emails_data:
  68.     print(email.get_info())
  69.  
  70. ////////////////////////////////////////////////////////////////////////////
  71. # 04. Zoo
  72.  
  73. class Zoo:
  74.     __animals = 0
  75.  
  76.     def __init__(self, name):
  77.         self.name = name
  78.         self.mammals = []
  79.         self.fishes = []
  80.         self.birds = []
  81.  
  82.     def add_animal(self, species, name):
  83.         if species == 'mammal':
  84.             self.mammals.append(name)
  85.         elif species == 'fish':
  86.             self.fishes.append(name)
  87.         elif species == 'bird':
  88.             self.birds.append(name)
  89.         Zoo.__animals += 1
  90.  
  91.     def get_info(self, species):
  92.         result = ""
  93.         if species == 'mammal':
  94.             result += f'Mammals in {self.name}: {", ".join(self.mammals)}\n'
  95.  
  96.         elif species == 'fish':
  97.             result += f'Fishes in {self.name}: {", ".join(self.fishes)}\n'
  98.  
  99.         elif species == 'bird':
  100.             result += f'Birds in {self.name}: {", ".join(self.birds)}\n'
  101.  
  102.         result += f"Total animals: {Zoo.__animals}"
  103.         return result
  104.  
  105.  
  106. zoo_name = input()
  107. my_zoo = Zoo(zoo_name)
  108. total_animals = int(input())
  109. for _ in range(1, total_animals + 1):
  110.     animal_info = input().split(' ')
  111.     current_species = animal_info[0]
  112.     name = animal_info[1]
  113.     my_zoo.add_animal(current_species, name)
  114.  
  115. species_to_print = input()
  116. print(my_zoo.get_info(species_to_print))
  117.  
  118. ////////////////////////////////////////////////////////////////////////////
  119. # 05. Circle
  120.  
  121. class Circle:
  122.  
  123.     __pi = 3.14
  124.  
  125.     def __init__(self, diameter):
  126.         self.diameter = diameter
  127.         self.radius = diameter / 2
  128.  
  129.     def calculate_circumference(self):
  130.         return 2 * (Circle.__pi * self.radius)
  131.  
  132.     def calculate_area(self):
  133.         return Circle.__pi * (self.radius ** 2)
  134.  
  135.     def calculate_area_of_sector(self, angle):
  136.         return (angle / 360) * Circle.__pi * (self.radius ** 2)
  137.  
  138. ////////////////////////////////////////////////////////////////////////////
  139.  
  140. # Objects and Classes - Exericse
  141.  
  142. ***************************************************************************************************************
  143. #01. Storage
  144.  
  145. class Storage:
  146.     storage = []
  147.  
  148.     def __init__(self, capacity: int):
  149.         self.capacity = capacity
  150.  
  151.     def add_product(self, product: str):
  152.         if len(self.storage) < self.capacity:
  153.             Storage.storage.append(product)
  154.  
  155.     def get_products(self):
  156.         return Storage.storage
  157.  
  158. ***************************************************************************************************************
  159. #02. Weapon
  160.  
  161. class Weapon:
  162.  
  163.     def __init__(self, bullets: int):
  164.         self.bullets = bullets
  165.  
  166.     def shoot(self):
  167.         if self.bullets > 0:
  168.             self.bullets -= 1
  169.             return 'shooting...'
  170.         return 'no bullets left'
  171.  
  172.     def __repr__(self):
  173.         return f'Remaining bullets: {self.bullets}'
  174.  
  175. ***************************************************************************************************************
  176. # 03. Catalogue
  177.  
  178. class Catalogue:
  179.  
  180.     def __init__(self, name: str):
  181.         self.name = name
  182.         self.products = []
  183.  
  184.     def add_product(self, product_name: str):
  185.         self.products.append(product_name)
  186.  
  187.     def get_by_letter(self, first_letter: str):
  188.         by_letter = []
  189.         for letter in self.products:
  190.             if letter[0] == first_letter:
  191.                 by_letter.append(letter)
  192.         return by_letter
  193.  
  194.     def __repr__(self):
  195.         self.products = sorted(self.products)
  196.         final_products = '\n'.join(self.products)
  197.         return f"Items in the {self.name} catalogue:\n{final_products}"
  198.  
  199. ***************************************************************************************************************
  200. # 04. Town
  201.  
  202. class Town:
  203.  
  204.     def __init__(self, name: str, latitude="0°N", longitude="0°E"):
  205.         self.name = name
  206.         self.latitude = latitude
  207.         self.longitude = longitude
  208.  
  209.     def set_latitude(self, latitude):
  210.         result1 = self.latitude = latitude
  211.         return result1
  212.  
  213.     def set_longitude(self, longitude):
  214.         result2 = self.longitude = longitude
  215.         return result2
  216.  
  217.     def __repr__(self):
  218.         return f"Town: {self.name} | Latitude: {self.latitude} | Longitude: {self.longitude}"
  219.  
  220. ***************************************************************************************************************
  221. # 05. Class
  222.  
  223. class Class:
  224.     __student_count = 22
  225.  
  226.     def __init__(self, name: str):
  227.         self.name = name
  228.         self.students = []
  229.         self.grades = []
  230.  
  231.     def add_student(self, name: str, grade: float):
  232.         Class.__student_count -= 1
  233.         if Class.__student_count > 0:
  234.             self.students.append(name)
  235.             self.grades.append(grade)
  236.  
  237.     def get_average_grade(self):
  238.         result = sum(self.grades) / len(self.students)
  239.         return float(f'{result:.2f}')
  240.  
  241.     def __repr__(self):
  242.         result = f"The students in {self.name}: {', '.join(self.students)}. Average grade: {self.get_average_grade()}"
  243.         return result
  244.         return result
  245.  
  246. ***************************************************************************************************************
  247. # 06. Inventory
  248.  
  249. class Inventory:
  250.  
  251.     def __init__(self, __capacity: int):
  252.         self.items = []
  253.         self.__capacity = __capacity
  254.  
  255.     def add_item(self, item: str):
  256.         if self.__capacity > 0:
  257.             self.items.append(item)
  258.             self.__capacity -= 1
  259.         return "not enough room in the inventory"
  260.  
  261.     def get_capacity(self):
  262.         result = len(self.items)
  263.         return result
  264.  
  265.     def __repr__(self):
  266.         to_print = f"{', '.join(self.items)}"
  267.         return f"Items: {to_print}.\nCapacity left: {self.__capacity}"
  268.  
  269. ***************************************************************************************************************
  270. # 07. Articles
  271.  
  272. class Article:
  273.  
  274.     def __init__(self, title: str, content: str, author: str):
  275.         self.title = title
  276.         self.content = content
  277.         self.author = author
  278.  
  279.     def edit(self, new_content: str):
  280.         self.content = new_content
  281.         return self.content
  282.  
  283.     def change_author(self, new_author: str):
  284.         self.author = new_author
  285.         return self.author
  286.  
  287.     def rename(self, new_title: str):
  288.         self.title = new_title
  289.         return self.title
  290.  
  291.     def __repr__(self):
  292.         result = f"{self.title} - {self.content}: {self.author}"
  293.         return result
  294.  
  295. ***************************************************************************************************************
  296. # 08. Vehicle
  297.  
  298. class Vehicle:
  299.  
  300.     def __init__(self, type, model, price):
  301.         self.type = type
  302.         self.model = model
  303.         self.price = price
  304.         self.owner = None
  305.  
  306.     def buy(self, money: int, owner: str):
  307.         if (money >= self.price) and (self.owner is None):
  308.             price_dif = float(f"{(money - self.price):.2f}")
  309.             result = f"Successfully bought a {self.type}. Change: {price_dif:.2f}"
  310.             self.owner = owner
  311.             return result
  312.  
  313.         elif money < self.price:
  314.             return 'Sorry, not enough money'
  315.  
  316.         elif self.owner is not None:
  317.             return 'Car already sold'
  318.  
  319.     def sell(self):
  320.         if self.owner is not None:
  321.             self.owner = None
  322.         else:
  323.             return 'Vehicle has no owner'
  324.  
  325.     def __repr__(self):
  326.         if self.owner is not None:
  327.             return f"{self.model} {self.type} is owned by: {self.owner}"
  328.         return f"{self.model} {self.type} is on sale: {self.price}"
  329.  
  330. ***************************************************************************************************************
  331. # 09. Movie
  332.  
  333. class Movie:
  334.     __watched_movies = []
  335.  
  336.     def __init__(self, name, director, watched=False):
  337.         self.name = name
  338.         self.director = director
  339.         self.watched = watched
  340.  
  341.     def change_name(self, new_name: str):
  342.         self.name = new_name
  343.         return self.name
  344.  
  345.     def change_director(self, new_director: str):
  346.         self.director = new_director
  347.         return self.director
  348.  
  349.     def watch(self):
  350.         if self.name not in Movie.__watched_movies:
  351.             self.watched = True
  352.             Movie.__watched_movies.append(self.name)
  353.  
  354.     def __repr__(self):
  355.         to_print = len(Movie.__watched_movies)
  356.         return f"Movie name: {self.name}; Movie director: {self.director}. Total watched movies: {to_print}"
  357.  
  358. ***************************************************************************************************************
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement