Advertisement
nq1s788

cashier

Feb 26th, 2024
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. class Cashier:
  2.     num = [5000, 2000, 1000, 500, 200, 100, 50, 10, 5, 1]
  3.     n = len(num)
  4.     cash = [0] * n
  5.     overall = 0
  6.  
  7.     def set_data(self, s):
  8.         self.overall = 0
  9.         s = s.split(',')
  10.         for e in s:
  11.             val, cnt = map(int, e.split('-'))
  12.             self.cash[self.num.index(val)] = cnt
  13.             self.overall += val * cnt
  14.  
  15.     def show_total_amount(self):
  16.         print('Всего в кассе', self.overall, 'руб.')
  17.         for i in range(self.n - 1, -1, -1):
  18.             if self.cash[i]:
  19.                 print(self.num[i], '-', self.cash[i])
  20.         print()
  21.  
  22.     def sell_product(self, price, income):
  23.         cash_buf = self.cash.copy()
  24.         change = income - price
  25.         if change < 0:
  26.             print('Недостаточно средств. Операция отклонена.')
  27.             print()
  28.             return
  29.         for i in range(self.n):
  30.             self.cash[i] += income // self.num[i]
  31.             income = income % self.num[i]
  32.         change_cash = [0] * self.n
  33.         for i in range(self.n):
  34.             x = min(change // self.num[i], self.cash[i])
  35.             change_cash[i] += x
  36.             change -= x * self.num[i]
  37.             self.cash[i] -= x
  38.         if change:
  39.             print('Не хватило валюты! Касса не может выдать оставшиеся', change, 'руб.')
  40.             print('Пополните кассе и повторите операцию')
  41.             print()
  42.             self.cash = cash_buf.copy()
  43.             return
  44.         for i in range(self.n - 1, -1, -1):
  45.             if change_cash[i]:
  46.                 print(self.num[i], '-', change_cash[i])
  47.         print()
  48.  
  49.  
  50. cashier = Cashier()
  51. request = input()
  52. while request != '0':
  53.     if request[0] == '1':
  54.         cashier.set_data(request[2:])
  55.     elif request[0] == '2':
  56.         price, income = map(int, request[2:].split())
  57.         cashier.sell_product(price, income)
  58.     elif request == '3':
  59.         cashier.show_total_amount()
  60.     else:
  61.         print('Неверный запрос.')
  62.         print()
  63.     request = input()
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement