Advertisement
Nenogzar

cash

Mar 5th, 2024
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 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.             parts = e.split('-')
  12.             if len(parts) == 2:
  13.                 val, cnt = map(int, parts)
  14.                 self.cash[self.num.index(val)] = cnt
  15.                 self.overall += val * cnt
  16.             else:
  17.                 print('Invalid input format:', e)
  18.                 return
  19.  
  20.     def show_total_amount(self):
  21.         print('Total at checkout', self.overall, 'eur.')
  22.         for i in range(self.n - 1, -1, -1):
  23.             if self.cash[i]:
  24.                 print(self.num[i], '-', self.cash[i])
  25.         print()
  26.  
  27.     def sell_product(self, price, income):
  28.         cash_buf = self.cash.copy()
  29.         change = income - price
  30.         if change < 0:
  31.             print('Insufficient funds. Operation rejected.')
  32.             print()
  33.             return
  34.         for i in range(self.n):
  35.             self.cash[i] += income // self.num[i]
  36.             income = income % self.num[i]
  37.         change_cash = [0] * self.n
  38.         for i in range(self.n):
  39.             x = min(change // self.num[i], self.cash[i])
  40.             change_cash[i] += x
  41.             change -= x * self.num[i]
  42.             self.cash[i] -= x
  43.         if change:
  44.             print('Not enough currency! The cash desk cannot issue the remaining', change, 'eur.')
  45.             print('Top up the cash register and repeat the operation')
  46.             print()
  47.             self.cash = cash_buf.copy()
  48.             return
  49.         for i in range(self.n - 1, -1, -1):
  50.             if change_cash[i]:
  51.                 print(self.num[i], '-', change_cash[i])
  52.         print()
  53.  
  54.  
  55. cashier = Cashier()
  56. request = input()
  57. while request != '0':
  58.     if request[0] == '1':
  59.         cashier.set_data(request[2:])
  60.     elif request[0] == '2':
  61.         price, income = map(int, request[2:].split())
  62.         cashier.sell_product(price, income)
  63.     elif request == '3':
  64.         cashier.show_total_amount()
  65.     else:
  66.         print('Invalid request.')
  67.         print()
  68.     request = input()
  69.  
  70.  
  71. #  test input
  72. """ loading a cassette with banknotes """
  73. #  1, 5000-10,2000-10,1000-10,500-10,200-10,100-10,50-10,10-10,5-10,1-10
  74.  
  75.  
  76. """  banknote availability check """
  77. #  3
  78.  
  79.  
  80. """ This input means that the customer buys a product at a price of €50 and pays with a €100 note/coin."""
  81. #  2,50 100
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement