Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. import unittest
  2. from Automat import Automat
  3. from Bank import Bank
  4. from Item import Item
  5. from exceptions.NoItemException import NoItemException
  6.  
  7.  
  8. class AutomatTest(unittest.TestCase):
  9.  
  10. def test_checkPriceOfGivenID(self):
  11. bank = Bank()
  12. automat = Automat(bank)
  13. Cola = Item(2)
  14. automat.add_object(Cola)
  15. self.assertEqual(automat.find_price_of_given_id(30), 2)
  16.  
  17. def test_checkIfPurchaseItemCanBeProcessedWhenNoProduct(self):
  18. bank = Bank()
  19. automat2 = Automat(bank)
  20. Soda = Item(2, 0)
  21. automat2.add_object(Soda)
  22. self.assertEqual(automat2.purchaseItem(30), "Given amount it too small and "
  23. "no item with " + str(30) + " in automat!")
  24.  
  25.  
  26. if __name__ == '__main__':
  27. unittest.main()
  28.  
  29. Testing started at 14:12 ...
  30. C:UsersAdminPycharmProjectsvending-machinevenvScriptspython.exe "C:Program FilesJetBrainsPyCharm Community Edition 2018.3.4helperspycharm_jb_unittest_runner.py" --path C:/Users/Admin/PycharmProjects/vending-machine/AutomatTest.py
  31. Launching unittests with arguments python -m unittest C:/Users/Admin/PycharmProjects/vending-machine/AutomatTest.py in C:UsersAdminPycharmProjectsvending-machine
  32.  
  33.  
  34. Ran 2 tests in 0.004s
  35. Error
  36. Traceback (most recent call last):
  37. File "C:ProgramDataAnaconda3libunittestcase.py", line 59, in testPartExecutor
  38. yield
  39. File "C:ProgramDataAnaconda3libunittestcase.py", line 615, in run
  40. testMethod()
  41. File "C:UsersAdminPycharmProjectsvending-machineAutomatTest.py", line 15, in test_checkPriceOfGivenID
  42. self.assertEqual(automat.find_price_of_given_id(30), 2)
  43. File "C:UsersAdminPycharmProjectsvending-machineAutomat.py", line 28, in find_price_of_given_id
  44. raise NoItemException
  45. exceptions.NoItemException.NoItemException
  46.  
  47.  
  48. FAILED (errors=1)
  49.  
  50. Process finished with exit code 1
  51.  
  52. from Item import Item
  53. from exceptions.NoItemException import NoItemException
  54. from exceptions.NoProperAmountException import NoProperAmountException
  55. from Coin import Coin
  56. from decimal import *
  57.  
  58. class Automat:
  59. item_id = 30
  60.  
  61. def __init__(self, _bank, objects=None):
  62. self.bank = _bank
  63. if objects is None:
  64. objects = {}
  65. self.objects = objects
  66. self.purchaseItemBank = []
  67.  
  68. def add_object(self, obj: Item):
  69. id_to_assign = Automat.item_id
  70. self.objects.update({id_to_assign: obj})
  71. Automat.item_id = Automat.item_id + 1
  72. return id_to_assign
  73.  
  74. def find_price_of_given_id(self, item_id):
  75. if self.objects.get(item_id) is not None:
  76. return self.objects.get(item_id).get_price()
  77. else:
  78. raise NoItemException
  79.  
  80. def find_amount_of_given_id(self, item_id):
  81. if self.objects.get(item_id) is not None:
  82. return self.objects.get(item_id).get_amount()
  83. else:
  84. raise NoItemException
  85.  
  86. def checkIfAmountIsPositive(self, item_id):
  87. if self.objects.get(item_id) is not None:
  88. var = True if self.objects.get(item_id).get_amount() > 0 else False
  89. return var
  90. else:
  91. raise NoItemException
  92.  
  93. def withdrawItem(self, item_id):
  94. self.objects.get(item_id).decrease()
  95.  
  96.  
  97. def purchaseItem(self, item_id):
  98.  
  99. sumOfCoins = 0
  100. if 30 <= item_id <= 50:
  101. lista = []
  102. for i in self.purchaseItemBank:
  103. lista.append(i)
  104. sumOfCoins += i.getCoinValue()
  105. priceOfGivenProduct = self.find_price_of_given_id(item_id)
  106.  
  107. if sumOfCoins < priceOfGivenProduct:
  108. if not self.checkIfAmountIsPositive(item_id):
  109. return "Given amount it too small and "
  110. "no item with " + str(item_id) + " in automat!"
  111. else:
  112. raise NoProperAmountException
  113. else:
  114. if not self.checkIfAmountIsPositive(item_id):
  115. return "No item with " + str(item_id) + " in automat!"
  116. a = round(abs(Decimal(priceOfGivenProduct) - sumOfCoins), 2)
  117. listaCopy = self.bank.getSortedBankListWithCoins()
  118. if a > 0:
  119. if len(listaCopy) == 0:
  120. return "Nie mozna wydac"
  121. for i, v in enumerate(self.bank.bank):
  122. if a == 0:
  123. break
  124. elif a >= v.getCoinValue():
  125. a = a - v.getCoinValue()
  126. listaCopy.remove(v)
  127. elif a < v.getCoinValue():
  128. continue
  129. if i + 1 == (len(self.bank.bank)):
  130. return "Nie mozna wydac"
  131. if a > 0:
  132. return "Nie mozna wydac"
  133. self.bank.bank = listaCopy.copy()
  134. self.withdrawItem(item_id)
  135. for iterator in lista:
  136. self.bank.addMoney(iterator)
  137. return "Wydano towar"
  138. else:
  139. raise NoItemException
  140.  
  141. class Item:
  142. def __init__(self, price, amount=5):
  143. self.amount = amount
  144. self.price = price
  145.  
  146. def get_price(self):
  147. return self.price
  148.  
  149. def get_amount(self):
  150. return self.amount
  151.  
  152. def decrease(self):
  153. self.amount -= 1
  154.  
  155. def __str__(self):
  156. return f"{self.amount} @ {self.price}"
  157.  
  158. class Bank:
  159. def __init__(self):
  160. self.bank = []
  161.  
  162. def addMoney(self, value):
  163. self.bank.append(value)
  164.  
  165. def getSumBank(self):
  166. return sum(value.getCoinValue() for value in self.bank)
  167.  
  168. def getSumOfGivenCoins(self, *args):
  169. suma = 0
  170. for i in args:
  171. suma += i.getCoinValue()
  172. return suma
  173.  
  174. def getSortedBankListWithCoins(self):
  175. self.bank.sort(key=lambda x: x.getCoinValue(), reverse=True)
  176. listaCopy = self.bank.copy()
  177. return listaCopy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement