Advertisement
Guest User

Untitled

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