Skip to content

Commit b71c67a

Browse files
committed
add week 8 - jar
1 parent 197f764 commit b71c67a

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

week-8/jar/jar.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Jar:
2+
"""Jar base class"""
3+
def __init__(self, capacity: int=12):
4+
self.size = 0
5+
self.capacity = capacity # use setter
6+
7+
8+
def deposit(self, v):
9+
if self.__size + v > self.__capacity:
10+
raise ValueError
11+
else:
12+
self.__size += v
13+
14+
15+
def withdraw(self, v):
16+
if v > self.__size:
17+
raise ValueError
18+
else:
19+
self.__size -= v
20+
21+
def __str__(self):
22+
cockie = "🍪"
23+
return(cockie * self.__size)
24+
25+
def __repr__(self):
26+
return self.__str__
27+
28+
@property
29+
def capacity(self):
30+
return (self.__capacity)
31+
32+
@capacity.setter
33+
def capacity(self, a):
34+
a = int(a)
35+
if a < 0:
36+
raise ValueError
37+
self.__capacity = a
38+
39+
40+
@property
41+
def size(self):
42+
return (self.__size)
43+
44+
@size.setter
45+
def size(self, a):
46+
self.__size = a

week-8/jar/test_jar.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
from jar import Jar
3+
4+
5+
def test_create_jar_default_values():
6+
jar = Jar()
7+
assert jar.size == 0
8+
assert jar.capacity == 12
9+
10+
11+
12+
def test_capacity_valueerror_int():
13+
with pytest.raises(ValueError):
14+
jar = Jar(capacity=-1)
15+
with pytest.raises(ValueError):
16+
jar = Jar(capacity="asd")
17+
18+
19+
def test_adding():
20+
jar = Jar()
21+
jar.deposit(8)
22+
with pytest.raises(ValueError):
23+
jar.deposit(12)
24+
25+
def test_sub():
26+
jar = Jar()
27+
jar.deposit(12)
28+
jar.withdraw(12)
29+
with pytest.raises(ValueError):
30+
jar.withdraw(12)
31+

0 commit comments

Comments
 (0)