Advertisement
GalinaKG

Python OOP Retake Exam - 22 Aug 2020 - Hotel Everland

Dec 9th, 2022
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.61 KB | None | 0 0
  1. # file name appliance.py
  2.  
  3.  
  4. class Appliance:
  5.     def __init__(self, cost: float):
  6.         self.cost = cost
  7.  
  8.     def get_monthly_expense(self):
  9.         return self.cost * 30
  10.  
  11.  
  12. ---------------------------------------------------------------------------------------------------------------
  13. # file name fridge.py
  14.  
  15.  
  16. from project.appliances.appliance import Appliance
  17.  
  18.  
  19. class Fridge(Appliance):
  20.     def __init__(self):
  21.         super().__init__(1.2)
  22.  
  23.  
  24. ---------------------------------------------------------------------------------------------------------------
  25. # file name laptop.py
  26.  
  27.  
  28. from project.appliances.appliance import Appliance
  29.  
  30.  
  31. class Laptop(Appliance):
  32.     def __init__(self):
  33.         super().__init__(1)
  34.  
  35.  
  36. ---------------------------------------------------------------------------------------------------------------
  37. # file name stove.py
  38.  
  39.  
  40. from project.appliances.appliance import Appliance
  41.  
  42.  
  43. class Stove(Appliance):
  44.     def __init__(self):
  45.         super().__init__(0.7)
  46.  
  47.  
  48. ---------------------------------------------------------------------------------------------------------------
  49. # file name tv.py
  50.  
  51.  
  52. from project.appliances.appliance import Appliance
  53.  
  54.  
  55. class TV(Appliance):
  56.     def __init__(self):
  57.         super().__init__(1.5)
  58.  
  59.  
  60.  
  61. ---------------------------------------------------------------------------------------------------------------
  62. # file name child.py
  63.  
  64.  
  65. class Child:
  66.     def __init__(self, food_cost: int, *toys_cost):
  67.         self.food_cost = food_cost  # money for a day
  68.  
  69.         self.cost = 0
  70.         for toy in toys_cost:
  71.             self.cost += toy
  72.         self.cost += food_cost
  73.  
  74.  
  75. ---------------------------------------------------------------------------------------------------------------
  76. # file name alone_old.py
  77.  
  78.  
  79. from project.rooms.room import Room
  80.  
  81.  
  82. class AloneOld(Room):
  83.     def __init__(self, family_name: str, pension: float):
  84.         super().__init__(family_name, pension, 1)
  85.         self.room_cost = 10
  86.  
  87.  
  88. ---------------------------------------------------------------------------------------------------------------
  89. # file name alone_young.py
  90.  
  91.  
  92. from project.appliances.tv import TV
  93. from project.rooms.room import Room
  94.  
  95.  
  96. class AloneYoung(Room):
  97.     def __init__(self, family_name: str, salary: float):
  98.         super().__init__(family_name, salary, 1)
  99.         self.room_cost = 10
  100.         self.appliances = [TV()]
  101.         self.calculate_expenses(self.appliances)
  102.  
  103.  
  104. ---------------------------------------------------------------------------------------------------------------
  105. # file name old_couple.py
  106.  
  107.  
  108. from project.appliances.fridge import Fridge
  109. from project.appliances.stove import Stove
  110. from project.appliances.tv import TV
  111. from project.rooms.room import Room
  112.  
  113.  
  114. class OldCouple(Room):
  115.     def __init__(self, family_name: str, pension_one: float, pension_two: float):
  116.         super().__init__(family_name, pension_one + pension_two, 2)
  117.         self.room_cost = 15
  118.         self.appliances = [TV(), Fridge(), Stove()] * 2
  119.         self.calculate_expenses(self.appliances)
  120.  
  121.  
  122. ---------------------------------------------------------------------------------------------------------------
  123. # file name room.py
  124.  
  125.  
  126. from project.appliances.appliance import Appliance
  127.  
  128.  
  129. class Room:
  130.     def __init__(self, name: str, budget: float, members_count: int):
  131.         self.family_name = name
  132.         self.budget = budget
  133.         self.members_count = members_count
  134.         self.children = []
  135.         self.expenses = 0
  136.        
  137.     @property
  138.     def expenses(self):
  139.         return self.__expenses
  140.    
  141.     @expenses.setter
  142.     def expenses(self, value):
  143.         if value < 0:
  144.             raise ValueError("Expenses cannot be negative")
  145.         self.__expenses = value
  146.  
  147.     def calculate_expenses(self, *args):
  148.         total_expenses = 0
  149.         for elements in args:
  150.             for el in elements:
  151.                 if isinstance(el, Appliance):
  152.                     total_expenses += el.get_monthly_expense()
  153.                 elif el.__class__.__name__ == "Child":
  154.                     total_expenses += el.cost * 30
  155.         self.expenses = total_expenses
  156.  
  157.  
  158. ---------------------------------------------------------------------------------------------------------------
  159. # file name young_couple.py
  160.  
  161.  
  162. from project.appliances.fridge import Fridge
  163. from project.appliances.laptop import Laptop
  164. from project.appliances.tv import TV
  165. from project.rooms.room import Room
  166.  
  167.  
  168. class YoungCouple(Room):
  169.     def __init__(self, family_name: str, salary_one: float, salary_two: float):
  170.         super().__init__(family_name, salary_one + salary_two, 2)
  171.         self.room_cost = 20
  172.         self.appliances = [TV(), Fridge(), Laptop()] * 2
  173.         self.calculate_expenses(self.appliances)
  174.  
  175.  
  176.  
  177. --------------------------------------------------------------------------------------------------------------
  178. # file name young_couple_with_children.py
  179.  
  180.  
  181. from project.appliances.fridge import Fridge
  182. from project.appliances.laptop import Laptop
  183. from project.appliances.tv import TV
  184. from project.rooms.room import Room
  185.  
  186.  
  187. class YoungCoupleWithChildren(Room):
  188.     def __init__(self, family_name: str, salary_one: float, salary_two: float, *children):
  189.         count = 2 + len(children)
  190.         super().__init__(family_name, salary_one + salary_two, count)
  191.         self.room_cost = 30
  192.         self.children = list(children)
  193.         self.appliances = [TV(), Fridge(), Laptop()] * count
  194.  
  195.         self.calculate_expenses(self.appliances, self.children)
  196.  
  197.  
  198. ---------------------------------------------------------------------------------------------------------------
  199. # file name everland.py
  200.  
  201.  
  202. from project.rooms.room import Room
  203.  
  204.  
  205. class Everland:
  206.     def __init__(self):
  207.         self.rooms = []
  208.  
  209.     def add_room(self, room: Room):
  210.         if room not in self.rooms:
  211.             self.rooms.append(room)
  212.  
  213.     def get_monthly_consumptions(self):
  214.         total_consumption = 0
  215.         for room in self.rooms:
  216.             total_consumption += room.expenses + room.room_cost
  217.  
  218.         return f"Monthly consumptions: {total_consumption:.2f}$."
  219.  
  220.     def pay(self):
  221.         result = ''
  222.         for room in self.rooms:
  223.             if room.budget >= room.expenses:
  224.                 room.budget -= room.expenses + room.room_cost
  225.                 result += f"{room.family_name} paid {(room.expenses + room.room_cost):.2f}$ and have" \
  226.                           f" {room.budget:.2f}$ left.\n"
  227.             else:
  228.                 self.rooms.remove(room)
  229.                 result += f"{room.family_name} does not have enough budget and must leave the hotel.\n"
  230.         return result.strip()
  231.  
  232.     def status(self):
  233.         all_people_in_the_hotel = sum([room.members_count for room in self.rooms])
  234.         result = f'Total population: {all_people_in_the_hotel}\n'
  235.  
  236.         for room in self.rooms:
  237.             result += f"{room.family_name} with {room.members_count} members. Budget: {room.budget:.2f}$," \
  238.                       f" Expenses: {room.expenses:.2f}$\n"
  239.             if room.__class__.__name__ == "YoungCoupleWithChildren":
  240.                 child_number = 1
  241.                 for child in room.children:
  242.                     result += f"--- Child {child_number} monthly cost: {(child.cost * 30):.2f}$\n"
  243.                     child_number += 1
  244.  
  245.             if room.__class__.__name__ != "AloneOld":
  246.                 total_expenses = 0
  247.                 for appliance in room.appliances:
  248.                     total_expenses += appliance.get_monthly_expense()
  249.  
  250.                 result += f"--- Appliances monthly cost: {total_expenses:.2f}$\n"
  251.  
  252.         return result.strip()
  253.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement