Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions 22 app.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from grocery_store.product import Product
from grocery_store.order import Order
from grocery_store.product import create_product
from grocery_store.order import create_order, calculate_total, add_product


kamala_order = Order([], "Kamala")
kamala_order = create_order([], "Kamala")

product_1 = Product("Apples", 3.47)
product_2 = Product("Oranges", 4.89)
product_3 = Product("Macbook", 1379.99)
product_1 = create_product("Apples", 3.47)
product_2 = create_product("Oranges", 4.89)
product_3 = create_product("Macbook", 1379.99)

kamala_order.add_product(product_1)
kamala_order.add_product(product_2)
kamala_order.add_product(product_3)
add_product(kamala_order, product_1)
add_product(kamala_order, product_2)
add_product(kamala_order, product_3)

print(f"The order total for {kamala_order.customer_name} is " +
f"{kamala_order.calculate_total()}")
print(f"The order total for {kamala_order['customer_name']} is " +
f"{calculate_total(kamala_order)}")

26 changes: 12 additions & 14 deletions 26 grocery_store/order.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
class Order:
def __init__(self, products, customer_name):
self.products = products
self.customer_name = customer_name

def add_product(self, product):
self.products.append(product)

def calculate_total(self):
total = 0
for i in range(1, len(self.products)):
total += self.products[i].price

return total


def create_order(products, customer_name):
return {
"customer_name": customer_name,
"products": products,
}

def add_product(order, product):
order["products"].append(product)

def calculate_total(order):
total = 0
for i in range(1, len(order["products"])):
total += order["products"][i]["price"]
tgoslee marked this conversation as resolved.
Show resolved Hide resolved


return total
13 changes: 7 additions & 6 deletions 13 grocery_store/product.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class Product:
def __init__(self, name, price):
self.name = name
self.price = name

def create_product(name, price):
return {
"name": name,
"price": name,
}

def __str__(self):
return f"{self.name} - ${self.price}"
def print_product(product):
return f"{product['name']} - ${product['price']}"


8 changes: 8 additions & 0 deletions 8 requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
attrs==20.3.0
iniconfig==1.1.1
packaging==20.9
pluggy==0.13.1
py==1.10.0
pyparsing==2.4.7
pytest==6.2.3
toml==0.10.2
54 changes: 27 additions & 27 deletions 54 tests/test_order.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
import pytest
from grocery_store.order import Order
from grocery_store.product import Product
from grocery_store.order import create_order, calculate_total, add_product
from grocery_store.product import create_product

def test_order_creation():
# Arrange
product_one = Product("Trombone", 478.95)
product_two = Product("Guitar", 327.99)
product_three = Product("Triangle", 2)
product_one = create_product("Trombone", 478.95)
product_two = create_product("Guitar", 327.99)
product_three = create_product("Triangle", 2)
products = [product_one, product_two, product_three]

# Act
sample_order = Order(products, "Shona Frederick")
sample_order = create_order(products, "Shona Frederick")

# Assert
assert len(sample_order.products) == 3
assert sample_order.customer_name == "Shona Frederick"
assert len(sample_order["products"]) == 3
assert sample_order["customer_name"] == "Shona Frederick"
for product in products:
assert product in sample_order.products
assert product in sample_order["products"]

def test_order_creation_with_no_products():
# Arrange
products = []

# Act
sample_order = Order(products, "Shona Frederick")
sample_order = create_order(products, "Shona Frederick")

# Assert
assert len(sample_order.products) == 0
assert sample_order.customer_name == "Shona Frederick"
assert len(sample_order["products"]) == 0
assert sample_order["customer_name"] == "Shona Frederick"

def test_add_product():
# Arrange
product_one = Product("Trombone", 478.95)
product_two = Product("Guitar", 327.99)
product_three = Product("Triangle", 2)
product_one = create_product("Trombone", 478.95)
product_two = create_product("Guitar", 327.99)
product_three = create_product("Triangle", 2)
products = [product_one, product_two, product_three]
sample_order = Order(products, "Shona Frederick")
sample_order = create_order(products, "Shona Frederick")

new_product = Product("Bass", 115)
new_product = create_product("Bass", 115)

# Act
sample_order.add_product(new_product)
add_product(sample_order, new_product)

# Assert
assert len(sample_order.products) == 4
assert new_product in sample_order.products
assert len(sample_order["products"]) == 4
assert new_product in sample_order["products"]

def test_calculate_total_with_no_products():
# Arrange
order = Order([], "Shona Frederick")
order = create_order([], "Shona Frederick")

# Act
total = order.calculate_total()
total = calculate_total(order)

# Assert
assert total == 0

def test_calculate_total_with_multiple_products():
# Arrange
product_one = Product("Trombone", 478.95)
product_two = Product("Guitar", 327.99)
product_three = Product("Triangle", 2)
product_one = create_product("Trombone", 478.95)
product_two = create_product("Guitar", 327.99)
product_three = create_product("Triangle", 2)
products = [product_one, product_two, product_three]

order = Order(products, "Shona Frederick")
order = create_order(products, "Shona Frederick")


# Act
total = order.calculate_total()
total = calculate_total(order)

# Assert
assert total == pytest.approx(478.95 + 327.99 + 2)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.