Advertisement
roman_gemini

Reversive kitchen

Jun 18th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.41 KB | None | 0 0
  1. import time
  2.  
  3. # Class that represents food object
  4. class Product(object):
  5.     def __init__(self, name, calories):
  6.         self.__name = name
  7.         self.__calories = calories
  8.  
  9.     def name(self):
  10.         return self.__name
  11.  
  12.     def calories(self):
  13.         return self.__calories
  14.  
  15.     def __str__(self):
  16.         return "(%s, %d calories)" % (self.name(), self.calories())
  17.  
  18.     def __repr__(self):
  19.         return self.__str__()
  20.  
  21.  
  22. # Class that represents shelf on a kitchen
  23. class Shelf(object):
  24.     def __init__(self):
  25.         self.__list = list()
  26.  
  27.     def __iadd__(self, other):
  28.         self.__list.append(other)
  29.         return self
  30.  
  31.     def __getitem__(self, item):
  32.         return self.__list.__getitem__(item)
  33.  
  34.     def calories(self):
  35.         return sum(map(lambda x: x.calories(), self.__list))
  36.  
  37.     def products(self):
  38.         return self.__list[::]
  39.  
  40.     def __str__(self):
  41.         return str(self.__list)
  42.  
  43.     def __repr__(self):
  44.         return self.__str__()
  45.  
  46.     def __len__(self):
  47.         return len(self.__list)
  48.  
  49.  
  50. # Class that represents kitchen object
  51. class Kitchen(object):
  52.     def __init__(self, size=100, *items):
  53.         self.__size = size
  54.         self.__shelves = list()
  55.         for item in items:
  56.             self += item
  57.  
  58.     def __iadd__(self, other: Product):
  59.         for shelf in self.__shelves:
  60.             if other.calories() + shelf.calories() <= self.__size:
  61.                 shelf += other
  62.                 return self
  63.  
  64.         s = Shelf()
  65.         s += other
  66.  
  67.         self.__shelves.append(s)
  68.         self.__shelves.sort(key=lambda x: x.calories(), reverse=True)
  69.  
  70.         return self
  71.  
  72.     def __len__(self):
  73.         return len(self.__shelves)
  74.  
  75.     def fetch(self):
  76.         if len(self.__shelves):
  77.             return self.__shelves.pop(0)
  78.  
  79.     def __str__(self):
  80.         return str(self.__shelves)
  81.  
  82.     def __repr__(self):
  83.         return self.__str__()
  84.  
  85.  
  86. # Maximum calories per eating
  87. threshold = 100
  88.  
  89. # Available meal on a kitchen
  90. kitchen = Kitchen(threshold,
  91.     Product("Beer", 31), Product("Chocolate", 45),
  92.     Product("Wine", 11), Product("Pizza", 55),
  93.     Product("Sushi", 54), Product("Tea", 10),
  94.     Product("Cake", 35), Product("Ice", 63),
  95.     Product("Orange", 5), Product("Snickers", 80),
  96.     Product("Meat", 89), Product("Apple", 42),
  97.     Product("Orange", 5), Product("Snickers", 80),
  98.     Product("Meat", 89), Product("Apple", 42),
  99.     Product("Cherry", 38), Product("Cheese", 29),
  100.     Product("Wine", 11), Product("Pizza", 55),
  101.     Product("Sushi", 54), Product("Tea", 10),
  102.     Product("Cake", 35), Product("Ice", 63),
  103.     Product("Orange", 5), Product("Snickers", 80),
  104.     Product("Meat", 89), Product("Apple", 42),
  105.     Product("Orange", 5), Product("Snickers", 80),
  106.     Product("Meat", 89), Product("Apple", 42),
  107.     Product("Orange", 5), Product("Snickers", 80),
  108.     Product("Meat", 89), Product("Apple", 42),
  109.     Product("Cherry", 38), Product("Cheese", 29),
  110.     Product("Water", 1), Product("Bread", 42)
  111. )
  112.  
  113. # Business logic
  114. while True:
  115.     if len(kitchen) == 0:
  116.         print("The kitchen is empty. Nothing more to eat. Bye!")
  117.         break
  118.  
  119.     print("Food on a kitchen: ", kitchen)
  120.  
  121.     ans = input("Do you want some food? (y/n)").lower()
  122.  
  123.     if ans == "n":
  124.         print("Ok, bye!")
  125.         break
  126.  
  127.     start = time.time()
  128.     eat = kitchen.fetch()
  129.     end = time.time()
  130.  
  131.     if eat is not None:
  132.         print("You ate: ", eat.products())
  133.         print("Total calories: ", eat.calories())
  134.         print("Time: ", str(end - start))
  135.  
  136.     print("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement