Untitled
user_3592770
plain_text
3 years ago
2.6 kB
13
Indexable
class Warehouse:
def __init__(self):
self.products = {}
def add_product(self, name, price, quantity, currency="EUR"):
if currency != "EUR":
price = self.convert_to_eur(price, currency)
if name in self.products:
self.products[name]["quantity"] += quantity
else:
self.products[name] = {"price": price, "quantity": quantity}
def remove_quantity(self, name, quantity):
if name in self.products:
current_quantity = self.products[name]["quantity"]
if current_quantity >= quantity:
self.products[name]["quantity"] -= quantity
print(f"Removed {quantity} {name}(s) from the warehouse.")
else:
print(f"Not enough {name}s in the warehouse to remove {quantity}.")
def update_price(self, name, price, currency="EUR"):
if currency != "EUR":
price = self.convert_to_eur(price, currency)
if name in self.products:
self.products[name]["price"] = price
print(f"Updated the price of {name} to {price} €.")
else:
print(f"{name} is not in the warehouse.")
def update_quantity(self, name, quantity):
if name in self.products:
self.products[name]["quantity"] += quantity
print(f"Added {quantity} {name}(s) to the warehouse.")
else:
print(f"{name} is not in the warehouse.")
def convert_to_eur(self, price, currency):
conversion_rates = {"USD": 0.82, "JPY": 0.0077, "CAD": 0.64, "SEK": 0.097}
if currency in conversion_rates:
return round(price * conversion_rates[currency], 2)
else:
raise ValueError("Currency not supported for conversion")
def __repr__(self):
product_list = []
for name, product in self.products.items():
product_list.append(f"{name}: {product['price']} €, {product['quantity']} available")
return "\n".join(product_list)
# create a new warehouse
w = Warehouse()
# add some products to the warehouse
w.add_product("apple", 1.2, 10)
w.add_product("banana", 0.8, 20)
w.add_product("orange", 1.5, 15, "USD") # add a product with USD currency
# print the warehouse contents
print(w)
# update the price of an existing product
w.update_price("apple", 1.5)
# update the quantity of an existing product
w.update_quantity("banana", 5)
# remove some products from the warehouse
w.remove_quantity("orange", 5)
# print the warehouse contents again
print(w)Editor is loading...