Advertisement
s_m4rt

Untitled

Nov 8th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. class Drink:
  2. def __init__(self, name, price, percent, capacity):
  3. self.name = name
  4. self.price = price
  5. self.percent = percent
  6. self.capacity = capacity
  7. def __add__(self, inny):
  8. name = self.name + " z " + inny.name
  9. price = self.price + inny.price
  10. obj = self.capacity + inny.capacity
  11. procent = (self.percent * self.capacity + inny.capacity * inny.percent) / (self.capacity + inny.capacity)
  12. wynik = Drink(name,price,obj,procent)
  13. return wynik
  14. def __mul__(self, number):
  15. price = self.price * number
  16. obj = self.capacity * number
  17. return Drink(self.name, price, self.percent, obj)
  18. def __str__(self):
  19. return self.name + ", " + str(self.percent) + "%, " + str(self.price) + "zl, " + str(self.capacity) + "ml;"
  20. __repr__ = __str__
  21. def __lt__(self, inny):
  22. return (self.price/self.percent) < (inny.price/inny.percent)
  23.  
  24. wodka = Drink("wodka", 5, 40.0, 40)
  25. martini = Drink("martini", 10, 11.0, 25)
  26. sprite = Drink("sprite", 4, 0.0, 200)
  27. lod = Drink("lod", 0, 0.0, 50)
  28.  
  29. drink1 = wodka + martini + sprite + lod
  30. drink2 = wodka + sprite + lod
  31. drink3 = martini + lod
  32.  
  33. print(drink2)
  34. print(drink3*3)
  35. lista = [drink1, drink2, drink3]
  36. lista.sort()
  37. print(lista)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement