Queer European MD passionate about IT

jar.py 647 B

1234567891011121314151617181920212223242526
  1. class Jar:
  2. def __init__(self, capacity=12):
  3. if not isinstance(capacity, int) or capacity < 0:
  4. raise ValueError("Invalid capacity")
  5. self._capacity = capacity
  6. self._size = 0
  7. def __str__(self):
  8. return '🍪' * self.size
  9. def deposit(self, n):
  10. new_size = n + self.size
  11. if new_size > self.capacity or new_size < 0:
  12. raise ValueError("Exceeded capacity")
  13. self._size += n
  14. def withdraw(self, n):
  15. return self.deposit(-n)
  16. @property
  17. def capacity(self):
  18. return self._capacity
  19. @property
  20. def size(self):
  21. return self._size