Skip to content

Commit ae0c731

Browse files
committed
feat(2-2): add example
1 parent a8dd4d9 commit ae0c731

File tree

10 files changed

+99
-4
lines changed

10 files changed

+99
-4
lines changed

02-demeter_tell_dont_ask/2-tell_dont_ask/example.py

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,51 @@
11
from user import User
2+
from product import Product
23

34
def bad_example():
45
user = User(1, "Javier", "Ferrer")
56

6-
full_name = f"{user.full_name.name.value} {user.full_name.last_name.value}"
7-
print(f"User ID: {user.id.value}, Full Name: {full_name}")
7+
product1 = Product(101, "Smartphone", 799.99)
8+
product2 = Product(102, "Headphones", 149.99)
9+
10+
if product1 not in user.saved_products:
11+
user.saved_products.append(product1)
12+
13+
if product2 not in user.saved_products:
14+
user.saved_products.append(product2)
15+
16+
if not user.saved_products:
17+
saved_products_text = "No saved products"
18+
else:
19+
saved_products_text = f"{user.full_name.name.value} {user.full_name.last_name.value}'s saved products:\n"
20+
for saved_product in user.saved_products:
21+
product_id = saved_product.id.value
22+
product_name = saved_product.name.value
23+
product_price = saved_product.price.value
24+
saved_products_text += f"- Product ID: {product_id}, Name: {product_name}, Price: ${product_price:.2f}\n"
25+
26+
print(saved_products_text)
27+
28+
user.saved_products.remove(product1)
29+
30+
remaining_products = len(user.saved_products)
31+
print(f"Remaining saved products after removal: {remaining_products}")
832

933

1034
def good_example():
1135
user = User(1, "Javier", "Ferrer")
1236

13-
print(user.display_information())
37+
product1 = Product(101, "Smartphone", 799.99)
38+
product2 = Product(102, "Headphones", 149.99)
39+
40+
user.add_to_saved_products(product1)
41+
user.add_to_saved_products(product2)
42+
43+
print(user.display_saved_products())
44+
45+
user.remove_from_saved_products(product1)
46+
47+
print("After removing a product:")
48+
print(user.display_saved_products())
1449

1550

1651
if __name__ == "__main__":
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from product_id import ProductId
2+
from product_name import ProductName
3+
from product_price import ProductPrice
4+
5+
class Product:
6+
def __init__(self, product_id, name, price):
7+
self.id = ProductId(product_id)
8+
self.name = ProductName(name)
9+
self.price = ProductPrice(price)
10+
11+
def display_information(self):
12+
return f"Product ID: {self.id}, Name: {self.name}, Price: {self.price}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class ProductId:
2+
def __init__(self, value):
3+
self.value = value
4+
5+
def __str__(self):
6+
return str(self.value)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class ProductName:
2+
def __init__(self, value):
3+
self.value = value
4+
5+
def __str__(self):
6+
return self.value
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class ProductPrice:
2+
def __init__(self, value):
3+
self.value = value
4+
5+
def __str__(self):
6+
return f"${self.value:.2f}"

02-demeter_tell_dont_ask/2-tell_dont_ask/user.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ def __init__(self, user_id, name, last_name):
1010
UserName(name),
1111
UserLastName(last_name)
1212
)
13+
self.saved_products = []
1314

1415
def display_information(self):
15-
return f"User ID: {self.id}, Full Name: {self.full_name}"
16+
return f"User ID: {self.id}, Full Name: {self.full_name}"
17+
18+
def add_to_saved_products(self, product):
19+
if product not in self.saved_products:
20+
self.saved_products.append(product)
21+
22+
def remove_from_saved_products(self, product):
23+
if product in self.saved_products:
24+
self.saved_products.remove(product)
25+
26+
def display_saved_products(self):
27+
if not self.saved_products:
28+
return "No saved products"
29+
30+
result = f"{self.full_name}'s saved products:\n"
31+
for product in self.saved_products:
32+
result += f"- {product.display_information()}\n"
33+
return result

02-demeter_tell_dont_ask/2-tell_dont_ask/user_full_name.py

+3
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ def __init__(self, name, last_name):
88

99
def formatted(self):
1010
return f"{self.name} {self.last_name}"
11+
12+
def __str__(self):
13+
return self.formatted()
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
class UserId:
22
def __init__(self, value):
33
self.value = value
4+
5+
def __str__(self):
6+
return str(self.value)
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
class UserLastName:
22
def __init__(self, value):
33
self.value = value
4+
5+
def __str__(self):
6+
return self.value
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
class UserName:
22
def __init__(self, value):
33
self.value = value
4+
5+
def __str__(self):
6+
return self.value

0 commit comments

Comments
 (0)