-
Notifications
You must be signed in to change notification settings - Fork 130
/
cucumbers.py
45 lines (35 loc) · 1.19 KB
/
cucumbers.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
39
40
41
42
43
44
45
"""
This module contains a simple class modeling a cucumber basket.
Cucumbers may be added or removed from the basket.
The basket has a maximum size, however.
"""
class CucumberBasket:
def __init__(self, initial_count=0, max_count=10):
if initial_count < 0:
raise ValueError("Initial cucumber basket count must not be negative")
if max_count < 0:
raise ValueError("Max cucumber basket count must not be negative")
self._count = initial_count
self._max_count = max_count
@property
def count(self):
return self._count
@property
def full(self):
return self.count == self.max_count
@property
def empty(self):
return self.count == 0
@property
def max_count(self):
return self._max_count
def add(self, count=1):
new_count = self.count + count
if new_count > self.max_count:
raise ValueError("Attempted to add too many cucumbers")
self._count = new_count
def remove(self, count=1):
new_count = self.count - count
if new_count < 0:
raise ValueError("Attempted to remove too many cucumbers")
self._count = new_count