Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1. """Order system."""
  2. from typing import List, Dict
  3.  
  4.  
  5. class OrderItem:
  6. """Order Item requested by a customer."""
  7.  
  8. def __init__(self, customer: str, name: str, quantity: int, one_item_volume: int):
  9. """
  10. Constructor that creates an order item.
  11.  
  12. :param customer: requester name.
  13. :param name: the name of the item.
  14. :param quantity: quantity that shows how many such items customer needs.
  15. :param one_item_volume: the volume of one item.
  16. """
  17. self.customer = customer
  18. self.name = name
  19. self.quantity = quantity
  20. self.one_item_volume = one_item_volume
  21.  
  22. @property
  23. def total_volume(self) -> int:
  24. """
  25. Calculate and return total volume of the current order item.
  26.  
  27. :return: Total volume (cm^3), int.
  28. """
  29. return self.quantity * self.one_item_volume
  30.  
  31.  
  32. class Order:
  33. """Combination of order items of one customer."""
  34.  
  35. def __init__(self, order_items: List[OrderItem]):
  36. """
  37. Constructor that creates an order.
  38.  
  39. :param order_items: list of order items.
  40. """
  41. self.order_items = order_items
  42. self.destination = None
  43.  
  44. # total_quantity - kõigi tellimuse ridade summaarne kogus
  45. @property
  46. def total_quantity(self) -> int:
  47. """
  48. Calculate and return the sum of quantities of all items in the order.
  49.  
  50. :return: Total quantity as int.
  51. """
  52. total = 0
  53. for item in self.order_items:
  54. total += item.quantity
  55. return total
  56.  
  57. @property
  58. def total_volume(self) -> int:
  59. """
  60. Calculate and return the total volume of all items in the order.
  61.  
  62. :return: Total volume (cm^3) as int.
  63. """
  64. calculate = 0
  65. for x in self.order_items:
  66. calculate += x.total_volume
  67. return calculate
  68.  
  69.  
  70. class Container:
  71. """Container to transport orders."""
  72.  
  73. def __init__(self, volume: int, orders: List[Order] = None):
  74. """Constructor that makes a container."""
  75. self.volume = volume # maht
  76. self.orders = orders # tellimused
  77. self.orders = orders if orders else []
  78.  
  79. @property
  80. def volume_left(self):
  81. """Calculate how much space is unused.
  82.  
  83. Order1 60 m3
  84. Order2 40 m3
  85. Order3 20 m3
  86. siis: volume - (60 + 40 + 20)
  87. orders on list orderitest
  88. """
  89. new = 0
  90. for i in self.orders:
  91. new += i.total_volume
  92. return self.volume - new
  93.  
  94.  
  95. class OrderAggregator:
  96. """Algorithm of aggregating orders."""
  97.  
  98. def __init__(self):
  99. """Initialize order aggregator."""
  100. self.order_items: List[OrderItem] = []
  101.  
  102. def add_item(self, item: OrderItem):
  103. """
  104. Add order item to the aggregator.
  105.  
  106. :param item: Item to add.
  107. :return: None
  108. """
  109. self.order_items.append(item)
  110.  
  111. def aggregate_order(self, customer: str, max_items_quantity: int, max_volume: int):
  112. """
  113. Create an order for customer which contains order lines added by add_item method.
  114.  
  115. Iterate over added orders items and add them to order if they are for given customer
  116. and can fit to the order.
  117.  
  118. :param customer: Customer's name to create an order for.
  119. :param max_items_quantity: Maximum amount on items in order.
  120. :param max_volume: Maximum volume of order. All items volumes must not exceed this value.
  121. :return: Order.
  122. """
  123. sum_of_q = 0
  124. sum_of_v = 0
  125. new_items = []
  126. items: List[OrderItem] = []
  127. # items_for_the_customer = filter(lambda item: item.customer == customer, self.order_items) # kui on tõene lisab sisse
  128. for order_item in self.order_items:
  129. delete = False
  130. if order_item.customer == customer:
  131. sum_of_q += order_item.quantity
  132. sum_of_v += order_item.total_volume
  133. if sum_of_q <= max_items_quantity and sum_of_v <= max_volume:
  134. items.append(order_item)
  135. delete = True
  136. else:
  137. sum_of_q -= order_item.quantity
  138. sum_of_v -= order_item.total_volume
  139. if not delete:
  140. new_items.append(order_item)
  141. self.order_items = new_items
  142. return Order(items)
  143.  
  144.  
  145. class ContainerAggregator:
  146. """Algorithm to prepare containers."""
  147.  
  148. def __init__(self, container_volume: int):
  149. """
  150. Initialize Container Aggregator.
  151.  
  152. :param container_volume: Volume of each container created by this aggregator.
  153. """
  154. self.container_volume = container_volume
  155. self.not_used_orders = []
  156.  
  157. def prepare_containers(self, orders: tuple) -> dict:
  158. """
  159. Create containers and put orders to them.
  160.  
  161. If order cannot be put to a container, it is added to self.not_used_orders list.
  162.  
  163. :param orders: tuple of orders.
  164. :return: dict where keys are destinations and values are containers to that destination with orders.
  165. """
  166. all_conteiners: Dict[str, List[Container]] = {}
  167. print(orders)
  168. for i in orders: # i on tellimus
  169. in_box = False
  170. if i.total_volume <= self.container_volume: # konteinerisse mahub
  171. if not all_conteiners.get(i.destination, None): # kui destination on tühi tuleb luua uus list
  172. all_conteiners[i.destination] = [Container(self.container_volume, [i])]
  173. else: # kui destination on dictis juba olemas
  174. # loobin läbi kõik konteinerid
  175. for conteiner in all_conteiners[i.destination]:
  176. if conteiner.volume_left >= i.total_volume and in_box is False: # kui mahub tuleb lisada liste
  177. in_box = True
  178. conteiner.orders.append(i)
  179. if not in_box:
  180. all_conteiners[i.destination].append(Container(self.container_volume, [i]))
  181. else:
  182. self.not_used_orders.append(i)
  183. return all_conteiners
  184.  
  185.  
  186. if __name__ == '__main__':
  187. print("Order items")
  188.  
  189. order_item1 = OrderItem("Apple", "iPhone 11", 100, 10)
  190. order_item2 = OrderItem("Samsung", "Samsung Galaxy Note 10", 80, 10)
  191. order_item3 = OrderItem("Mööbel 24", "Laud", 300, 200)
  192. order_item4 = OrderItem("Apple", "iPhone 11 Pro", 200, 10)
  193. order_item5 = OrderItem("Mööbel 24", "Diivan", 20, 200)
  194. order_item6 = OrderItem("Mööbel 24", "Midagi väga suurt", 20, 100)
  195.  
  196. print(order_item3.total_volume) # 60000
  197.  
  198. print("Order Aggregator")
  199. oa = OrderAggregator()
  200. oa.add_item(order_item1)
  201. oa.add_item(order_item2)
  202. oa.add_item(order_item3)
  203. oa.add_item(order_item4)
  204. oa.add_item(order_item5)
  205. oa.add_item(order_item6)
  206. print(f'Added {len(oa.order_items)}(6 is correct) order items')
  207.  
  208. order1 = oa.aggregate_order("Apple", 350, 3000)
  209. order1.destination = "Tallinn"
  210. print(f'order1 has {len(order1.order_items)}(2 is correct) order items')
  211.  
  212. order2 = oa.aggregate_order("Mööbel 24", 325, 64100)
  213. order2.destination = "Tallinn"
  214. print(f'order2 has {len(order2.order_items)}(2 is correct) order items')
  215.  
  216. print(f'after orders creation, aggregator has only {len(oa.order_items)}(2 is correct) order items left.')
  217.  
  218. print("Container Aggregator")
  219. ca = ContainerAggregator(70000)
  220. too_big_order = Order([OrderItem("Apple", "Apple Car", 10000, 300)])
  221. too_big_order.destination = "Somewhere"
  222. containers = ca.prepare_containers((order1, order2, too_big_order))
  223. print(f'prepare_containers produced containers to {len(containers)}(1 is correct) different destination(s)')
  224.  
  225. try:
  226. containers_to_tallinn = containers['Tallinn']
  227. print(f'volume of the container to tallinn is {containers_to_tallinn[0].volume}(70000 is correct) cm^3')
  228. print(f'container to tallinn has {len(containers_to_tallinn[0].orders)}(2 is correct) orders')
  229. except KeyError:
  230. print('Container to Tallinn not found!')
  231. print(f'{len(ca.not_used_orders)}(1 is correct) cannot be added to containers')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement