PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ch09/cashregister.py

https://bitbucket.org/cayhorstmann/pythonforeveryone
Python | 37 lines | 14 code | 5 blank | 18 comment | 0 complexity | 2242c5c604e1ab765a0a94844a00715e MD5 | raw file
  1. ##
  2. # This module defines the CashRegister class.
  3. #
  4. ## A simulated cash register that tracks the item count and the total amount due.
  5. #
  6. class CashRegister :
  7. ## Constructs a cash register with cleared item count and total.
  8. #
  9. def __init__(self) :
  10. self._itemCount = 0
  11. self._totalPrice = 0.0
  12. ## Adds an item to this cash register.
  13. # @param price the price of this item
  14. #
  15. def addItem(self, price) :
  16. self._itemCount = self._itemCount + 1
  17. self._totalPrice = self._totalPrice + price
  18. ## Gets the price of all items in the current sale.
  19. # @return the total price
  20. #
  21. def getTotal(self) :
  22. return self._totalPrice
  23. ## Gets the number of items in the current sale.
  24. # @return the item count
  25. #
  26. def getCount(self) :
  27. return self._itemCount
  28. ## Clears the item count and the total.
  29. #
  30. def clear(self) :
  31. self._itemCount = 0
  32. self._totalPrice = 0.0