1234567891011121314151617181920212223242526 |
- class Jar:
- def __init__(self, capacity=12):
- if not isinstance(capacity, int) or capacity < 0:
- raise ValueError("Invalid capacity")
- self._capacity = capacity
- self._size = 0
- def __str__(self):
- return '🍪' * self.size
- def deposit(self, n):
- new_size = n + self.size
- if new_size > self.capacity or new_size < 0:
- raise ValueError("Exceeded capacity")
- self._size += n
- def withdraw(self, n):
- return self.deposit(-n)
- @property
- def capacity(self):
- return self._capacity
- @property
- def size(self):
- return self._size
|