method_task_2.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from collections import defaultdict
  2. class Product:
  3. def __init__(self, name, price):
  4. self.name = name
  5. self.price = price
  6. class User:
  7. def __init__(self, login, balance=0):
  8. self.login = login
  9. self.balance = balance
  10. @property
  11. def balance(self):
  12. return self.__balance
  13. @balance.setter
  14. def balance(self, value):
  15. self.__balance = value
  16. def __str__(self):
  17. return f"Пользователь {self.login}, баланс - {self.balance}"
  18. def deposit(self, value):
  19. self.balance += value
  20. def is_money_enough(self, value):
  21. return value <= self .balance
  22. def payment(self, value):
  23. if self.is_money_enough(value):
  24. self.balance -= value
  25. return True
  26. else:
  27. print("Не хватает средств на балансе. Пополните счет")
  28. return False
  29. class Cart:
  30. def __init__(self, user:User) -> None:
  31. self.user = user
  32. self.goods = defaultdict()
  33. self.__total = 0
  34. def add(self, product:Product, count=1):
  35. if product not in self.goods.keys():
  36. self.goods[product] = count
  37. else:
  38. self.goods[product] += count
  39. self.__total += product.price * count
  40. def remove(self, product:Product, number=1):
  41. if number >= self.goods[product]:
  42. number = self.goods[product]
  43. self.goods[product] = self.goods.get(product, 0) - number
  44. self.__total -= number * product.price
  45. @property
  46. def total(self):
  47. return self.__total
  48. def order(self):
  49. order_price = 0
  50. for key in self.goods.keys():
  51. order_price += key.price * self.goods[key]
  52. if self.user.payment(order_price):
  53. print("Заказ оплачен")
  54. else:
  55. print("Проблема с оплатой")
  56. def print_check(self):
  57. sorted_list = sorted(self.goods, key=lambda x: x.name)
  58. print("---Your check---")
  59. for element in sorted_list:
  60. if self.goods[element] > 0:
  61. print(f"{element.name} {element.price} {self.goods[element]} {element.price*self.goods[element]}")
  62. print(f"---Total: {self.total}---")
  63. def main():
  64. billy = User('billy@rambler.ru')
  65. lemon = Product('lemon', 20)
  66. carrot = Product('carrot', 30)
  67. cart_billy = Cart(billy)
  68. print(cart_billy.user) # Пользователь billy@rambler.ru, баланс - 0
  69. cart_billy.add(lemon, 2)
  70. cart_billy.add(carrot)
  71. cart_billy.print_check()
  72. ''' Печатает текст ниже
  73. ---Your check---
  74. carrot 30 1 30
  75. lemon 20 2 40
  76. ---Total: 70---'''
  77. cart_billy.add(lemon, 3)
  78. cart_billy.print_check()
  79. ''' Печатает текст ниже
  80. ---Your check---
  81. carrot 30 1 30
  82. lemon 20 5 100
  83. ---Total: 130---'''
  84. cart_billy.remove(lemon, 6)
  85. cart_billy.print_check()
  86. ''' Печатает текст ниже
  87. ---Your check---
  88. carrot 30 1 30
  89. ---Total: 30---'''
  90. print(cart_billy.total) # 30
  91. cart_billy.add(lemon, 5)
  92. cart_billy.print_check()
  93. ''' Печатает текст ниже
  94. ---Your check---
  95. carrot 30 1 30
  96. lemon 20 5 100
  97. ---Total: 130---'''
  98. cart_billy.order()
  99. ''' Печатает текст ниже
  100. Не хватает средств на балансе. Пополните счет
  101. Проблема с оплатой'''
  102. cart_billy.user.deposit(150)
  103. cart_billy.order() # Заказ оплачен
  104. print(cart_billy.user.balance) # 20
  105. if __name__ == '__main__':
  106. main()