Advertisement
SimeonTs

SUPyF2 Objects/Classes-Exericse - 01. Storage

Oct 19th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. """
  2. Objects and Classes - Exericse
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1734#0
  4.  
  5. SUPyF2 Objects/Classes-Exericse - 01. Storage
  6.  
  7. Problem:
  8. Create a class Storage. The __init__ method should accept one parameter: the capacity of the storage.
  9. The Storage class should also have an attribute called storage, where all the items will be stored.
  10. The class should have two additional methods:
  11. • add_product(product) - adds the product in the storage if there is space for it
  12. • get_products() - returns the storage list
  13. Example:
  14.    Test Code
  15. storage = Storage(4)
  16. storage.add_product("apple")
  17. storage.add_product("banana")
  18. storage.add_product("potato")
  19. storage.add_product("tomato")
  20. storage.add_product("bread")
  21. print(storage.get_products())
  22.  
  23. Output:
  24. ['apple', 'banana', 'potato', 'tomato']
  25. """
  26.  
  27.  
  28. class Storage:
  29.     def __init__(self, capacity):
  30.         self.capacity = capacity
  31.         self.storage = []
  32.  
  33.     def add_product(self, product):
  34.         if self.capacity > 0:
  35.             self.storage.append(product)
  36.             self.capacity -= 1
  37.  
  38.     def get_products(self):
  39.         return self.storage
  40.  
  41.  
  42. storage = Storage(4)
  43. storage.add_product("apple")
  44. storage.add_product("banana")
  45. storage.add_product("potato")
  46. storage.add_product("tomato")
  47. storage.add_product("bread")
  48. print(storage.get_products())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement