Advertisement
GeorgiLukanov87

Python OOP Retake Exam - 22 Aug 2020 - Hotel Everland

Nov 16th, 2022 (edited)
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.68 KB | None | 0 0
  1. # Python OOP Retake Exam - 22 Aug 2020 - Hotel Everland
  2.  
  3. # https://judge.softuni.org/Contests/Practice/Index/2472#1
  4. =============================================================================================
  5. # file name: appliance.py
  6.  
  7. from abc import ABC
  8.  
  9.  
  10. class Appliance(ABC):
  11.  
  12.     def __init__(self, cost: float):
  13.         self.cost = cost  # cost for a single day!
  14.  
  15.     def get_monthly_expense(self):
  16.         return self.cost * 30
  17.  
  18. =============================================================================================
  19. # file name: fridge.py
  20.  
  21. from project.appliances.appliance import Appliance
  22.  
  23.  
  24. class Fridge(Appliance):
  25.     COST = 1.2
  26.  
  27.     def __init__(self):
  28.         super().__init__(self.COST)
  29.         self.cost = self.COST
  30.  
  31.     def get_monthly_expense(self):
  32.         return self.cost * 30
  33.  
  34.     def calculate_cost(self, toys_cost):
  35.         total_cost = 0
  36.         for cost in toys_cost:
  37.             total_cost += cost
  38.         return total_cost + self.get_monthly_expense()
  39.  
  40. =============================================================================================
  41. # file name: laptop.py
  42.  
  43. from project.appliances.appliance import Appliance
  44.  
  45.  
  46. class Laptop(Appliance):
  47.     COST = 1
  48.  
  49.     def __init__(self):
  50.         super().__init__(self.COST)
  51.         self.cost = self.COST
  52.  
  53.     def get_monthly_expense(self):
  54.         return self.cost * 30
  55.  
  56.     def calculate_cost(self, toys_cost):
  57.         total_cost = 0
  58.         for cost in toys_cost:
  59.             total_cost += cost
  60.         return total_cost + self.get_monthly_expense()
  61.  
  62. =============================================================================================
  63. # file name: stove.py
  64.  
  65. from project.appliances.appliance import Appliance
  66.  
  67.  
  68. class Stove(Appliance):
  69.     COST = 0.7
  70.  
  71.     def __init__(self):
  72.         super().__init__(self.COST)
  73.         self.cost = self.COST
  74.  
  75.     def get_monthly_expense(self):
  76.         return self.cost * 30
  77.  
  78.     def calculate_cost(self, toys_cost):
  79.         total_cost = 0
  80.         for cost in toys_cost:
  81.             total_cost += cost
  82.         return total_cost + self.get_monthly_expense()
  83.  
  84. =============================================================================================
  85. # file name: tv.py
  86.  
  87. from project.appliances.appliance import Appliance
  88.  
  89.  
  90. class TV(Appliance):
  91.     COST = 1.5
  92.  
  93.     def __init__(self):
  94.         super().__init__(self.COST)
  95.         self.cost = self.COST
  96.  
  97.     def get_monthly_expense(self):
  98.         return self.cost * 30
  99.  
  100.     def calculate_cost(self, toys_cost):
  101.         total_cost = 0
  102.         for cost in toys_cost:
  103.             total_cost += cost
  104.         return total_cost + self.get_monthly_expense()
  105.  
  106. =============================================================================================
  107. # file name: child.py
  108.  
  109. class Child:
  110.     def __init__(self, food_cost: int, *toys_cost):
  111.         self.food_cost = food_cost
  112.         self.cost = sum(x for x in toys_cost) + self.food_cost
  113.  
  114. =============================================================================================
  115. # file name: room.py
  116.  
  117. class Room:
  118.     def __init__(self, name: str, budget: float, members_count: int):
  119.         self.family_name = name
  120.         self.budget = budget
  121.         self.members_count = members_count
  122.         self.children = []  # will contain all kids in that room (objects)!
  123.         self.expenses = self.calculate_expenses()
  124.  
  125.     @property
  126.     def expenses(self):
  127.         return self.__expenses
  128.  
  129.     @expenses.setter
  130.     def expenses(self, value):
  131.         if value < 0:
  132.             raise ValueError("Expenses cannot be negative")
  133.         self.__expenses = value
  134.  
  135.     @staticmethod
  136.     def calculate_expenses(*args):
  137.         total_expenses = 0
  138.         for el in args:
  139.             for sub_el in el:
  140.                 if type(sub_el).__name__ != 'Child':
  141.                     total_expenses += sub_el.get_monthly_expense()
  142.                 else:
  143.                     total_expenses += sub_el.cost * 30
  144.  
  145.         return total_expenses
  146.  
  147.     @staticmethod
  148.     def child_cost(children):
  149.         total_cost = 0
  150.         for child in children:
  151.             total_cost += child.cost
  152.         return total_cost
  153.  
  154. =============================================================================================
  155. # file name: alone_old.py
  156.  
  157. from project.rooms.room import Room
  158.  
  159.  
  160. class AloneOld(Room):
  161.     def __init__(self, family_name: str, pension: float):
  162.         super().__init__(family_name, pension, 1)
  163.         self.family_name = family_name
  164.         self.budget = pension
  165.         self.members_count = 1
  166.         self.room_cost = 10
  167.         self.appliances = []
  168.  
  169. =============================================================================================
  170. # file name: alone_young.py
  171.  
  172. from project.appliances.tv import TV
  173. from project.rooms.room import Room
  174.  
  175.  
  176. class AloneYoung(Room):
  177.     def __init__(self, family_name: str, salary: float):
  178.         super().__init__(family_name, salary, 1)
  179.         self.family_name = family_name
  180.         self.budget = salary
  181.         self.members_count = 1
  182.         self.appliances = [TV()]
  183.         self.room_cost = 10
  184.         self.expenses = self.calculate_expenses(self.appliances)
  185.  
  186. =============================================================================================
  187. # file name: old_couple.py
  188.  
  189. from project.appliances.fridge import Fridge
  190. from project.appliances.stove import Stove
  191. from project.appliances.tv import TV
  192. from project.rooms.room import Room
  193.  
  194.  
  195. class OldCouple(Room):
  196.     def __init__(self, family_name: str, pension_one: float, pension_two: float):
  197.         super().__init__(family_name, pension_one + pension_two, 2)
  198.         self.family_name = family_name
  199.         self.budget = pension_one + pension_two
  200.         self.appliances = [TV(), TV(), Fridge(), Fridge(), Stove(), Stove()]
  201.         self.room_cost = 15
  202.         self.expenses = self.calculate_expenses(self.appliances)
  203.  
  204. =============================================================================================
  205. # file name: young_couple.py
  206.  
  207. from project.appliances.fridge import Fridge
  208. from project.appliances.laptop import Laptop
  209. from project.appliances.tv import TV
  210. from project.rooms.room import Room
  211.  
  212.  
  213. class YoungCouple(Room):
  214.     def __init__(self, family_name: str, salary_one: float, salary_two: float):
  215.         super().__init__(family_name, salary_one + salary_two, 2)
  216.         self.family_name = family_name
  217.         self.budget = salary_one + salary_two
  218.         self.members_count = 2
  219.         self.appliances = [TV(), TV(), Fridge(), Fridge(), Laptop(), Laptop()]
  220.         self.room_cost = 20
  221.         self.expenses = self.calculate_expenses(self.appliances)
  222.  
  223. =============================================================================================
  224. # file name: young_couple_with_children.py
  225.  
  226. from project.appliances.fridge import Fridge
  227. from project.appliances.laptop import Laptop
  228. from project.appliances.tv import TV
  229. from project.rooms.room import Room
  230.  
  231.  
  232. class YoungCoupleWithChildren(Room):
  233.     def __init__(self, family_name: str, salary_one: float, salary_two: float, *children):
  234.         super().__init__(family_name, salary_two + salary_one, 2 + len(children))
  235.         self.family_name = family_name
  236.         self.budget = salary_one + salary_two
  237.         self.children = [*children]
  238.         self.members_count = 2 + len(children)
  239.         self.room_cost = 30
  240.         self.appliances = [TV(), TV(), Fridge(), Fridge(), Laptop(), Laptop()]
  241.         self.appliances.extend(self.extra_items(children))
  242.         self.expenses = self.calculate_expenses(self.appliances, children)
  243.  
  244.     @staticmethod
  245.     def extra_items(children):
  246.         extra_items_need = []
  247.         for _ in children:
  248.             extra_items_need.extend([TV(), Fridge(), Laptop()])
  249.         return [*extra_items_need]
  250.  
  251. =============================================================================================
  252. # file name: everland.py
  253.  
  254. from project.rooms.room import Room
  255.  
  256.  
  257. class Everland:
  258.     def __init__(self):
  259.         self.rooms = []
  260.  
  261.     def add_room(self, room: Room):
  262.         if room not in self.rooms:
  263.             self.rooms.append(room)
  264.  
  265.     def get_monthly_consumptions(self):
  266.         total_sum = 0
  267.         for room in self.rooms:
  268.             total_sum += room.expenses + room.room_cost
  269.         return f"Monthly consumption: {total_sum:.2f}$."
  270.  
  271.     def pay(self):
  272.         result = []
  273.         for room in self.rooms:
  274.             if room.budget >= room.expenses + room.room_cost:
  275.                 room.budget -= (room.expenses + room.room_cost)
  276.                 result.append(f"{room.family_name} paid {room.expenses + room.room_cost:.2f}$ and have {room.budget:.2f}$ left.")
  277.             else:
  278.                 result.append(f"{room.family_name} does not have enough budget and must leave the hotel.")
  279.                 self.rooms.remove(room)
  280.  
  281.         return '\n'.join(result)
  282.  
  283.     def status(self):
  284.         total_population = sum([p.members_count for p in self.rooms])
  285.         result = [f'Total population: {total_population}']
  286.         for room in self.rooms:
  287.             child_exp = sum([c.cost for c in room.children]) * 30
  288.             if not child_exp:
  289.                 child_exp = 0
  290.             current_budget = room.budget
  291.             result.append(f'{room.family_name} with {room.members_count} members. '
  292.                           f'Budget: {current_budget:.2f}$, Expenses: {room.expenses:.2f}$')
  293.  
  294.             for index, child in enumerate(room.children):
  295.                 result.append(f'--- Child {index + 1} monthly cost: {room.children[index].cost*30:.2f}$')
  296.  
  297.             result.append(f'--- Appliances monthly cost: '
  298.                           f'{(room.expenses - child_exp):.2f}$')
  299.  
  300.         return '\n'.join(result)
  301.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement