-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjar.py
38 lines (30 loc) · 1.06 KB
/
jar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Jar:
def __init__(self, capacity=12):
self._validate_positive_integer(capacity)
self._capacity = capacity
self.cookies = 0
def __str__(self):
return "🍪" * self.cookies
def _validate_positive_integer(self, value):
if not isinstance(value, int) or value < 0:
raise ValueError("Invalid positive integer.")
def _validate_non_negative_withdrawal(self, n):
if n < 0 or self.cookies - n < 0:
raise ValueError("Invalid withdrawal amount.")
def _validate_deposit(self, n):
if n < 0 or self.cookies + n > self._capacity:
raise ValueError("Invalid deposit amount.")
def deposit(self, n):
self._validate_positive_integer(n)
self._validate_deposit(n)
self.cookies += n
def withdraw(self, n):
self._validate_positive_integer(n)
self._validate_non_negative_withdrawal(n)
self.cookies -= n
@property
def capacity(self):
return self._capacity
@property
def size(self):
return self.cookies