class Shop: def __init__(self): self.product_repository = ProductRepository() self.customer_repository = CustomerRepository() def deliver(self, product_type: str, name: str): if product_type == 'Food': return self.product_repository.add(Food(name)) elif product_type == 'Drink': return self.product_repository.add(Drink(name)) def sell(self, customer_name: str, **shopping_list): result = [] customer = Customer(customer_name) if self.customer_repository.find(customer_name) == 'None': self.customer_repository.add(customer) for product_name, quantity in shopping_list.items(): product = self.product_repository.find(product_name) if product is not 'None': if product.quantity - quantity <= 0: customer.products[product_name] = product.quantity self.product_repository.products.remove(product) result.append(f"Left quantity of {product_name}: {0}") else: customer.products[product_name] = product.quantity - quantity result.append(self.product_repository.decrease(product, quantity)) return '\n'.join(result)
class Shop: def __init__(self): self.product_repository = ProductRepository() self.customer_repository = CustomerRepository() @staticmethod def __create_product(product_type, product_name): product = None if product_type == 'Drink': product = Drink(product_name) elif product_type == 'Food': product = Food(product_name) return product def deliver(self, product_type: str, name: str): product = self.__create_product(product_type, name) if product in self.product_repository.products: raise ValueError(f"Product {product.name} already exists.") return self.product_repository.add(product) def sell(self, customer_name: str, **shopping_list): customer = self.customer_repository.find(customer_name) if customer == "None": customer = Customer(customer_name) self.customer_repository.add(customer) result = "" for product_name, quantity in shopping_list.items(): product = self.product_repository.find(product_name) if not product == "None": if quantity < product.quantity: customer.products[product_name] = quantity result += self.product_repository.decrease(product, quantity) else: customer.products[product_name] = product.quantity result += self.product_repository.decrease(product, product.quantity) self.product_repository.products.remove(product) return result
class Shop: def __init__(self): self.product_repository = ProductRepository() self.customer_repository = CustomerRepository() def deliver(self, product_type: str, name: str): product = self.create_product(product_type, name) if self.check_if_product_in_products(product): raise ValueError(f'Product {product.name} already exists.') if product: self.product_repository.add(product) return f"Product {product.name} successfully added to inventory." def sell(self, customer_name, **kwargs): if not customer_name in [c.name for c in self.customer_repository.customers]: customer = self.create_customer(customer_name) self.customer_repository.add(customer) else: customer = self.customer_repository.find(customer_name) for product, value in kwargs.items(): if str(product) in [p.name for p in self.product_repository.products]: prod = [p for p in self.product_repository.products if p.name == str(product)][0] if prod.quantity <= value: prod_quality = prod.quantity self.product_repository.products.remove(prod) else: prod_quality = value self.product_repository.decrease(prod, value) customer.products[str(product)] = prod_quality result = '' for product_name, product_quantity in customer.products.items(): result + "\n".join(f"Left quantity of {product_name}: {product_quantity}") @staticmethod def create_product(product_type, name): if product_type == 'Drink': return Drink(name) if product_type == 'Food': return Food(name) def check_if_product_in_products(self, product): return product.name in [p.name for p in self.product_repository.products] # def customer_operator(self, customer_name): # if self.customer_repository.find(customer_name) == 'None': # c = Customer(customer_name) # @staticmethod def create_customer(customer_name): return Customer(customer_name)
def __init__(self): self.product_repository = ProductRepository() self.customer_repository = CustomerRepository()
class Shop: def __init__(self): self.product_repository = ProductRepository() self.customer_repository = CustomerRepository() def deliver(self, product_type: str, name: str): if product_type in ["Drink", "Food"]: if product_type == "Drink": product = Drink(name) product_exists = [ prod for prod in self.product_repository.products if prod.name == name ] if product_exists: raise ValueError(f"Product {product.name} already exists.") self.product_repository.products.append(product) return f"Product {name} successfully added to inventory." elif product_type == "Food": product = Food(name) product_exists = [ prod for prod in self.product_repository.products if prod.name == name ] if product_exists: raise ValueError(f"Product {product.name} already exists.") self.product_repository.products.append(product) return f"Product {name} successfully added to inventory." # product_exists = [prod for prod in self.product_repository.products if prod.name == name] def sell( self, customer_name: str, **shopping_list ): # shopping_list contains product name (str) as key argument and quantity (int) as value argument customer_exists = [ cust for cust in self.customer_repository.customers if customer_name == cust.name ] if not customer_exists: customer = Customer(customer_name) self.customer_repository.customers.append(customer) products_bought = {} shop_list = shopping_list for product_name, qty in shop_list.items(): product_exist = [ prod for prod in self.product_repository.products if product_name == prod.name ] if product_exist: product = product_exist[0] if product.quantity < qty: customer.products[ product_name] = qty - product.quantity self.product_repository.products.remove(product) products_bought[product_name] = product.quantity else: products_bought[product_name] = product.quantity - qty customer.products[ product_name] = product.quantity - qty self.product_repository.decrease(product, qty) data = "" for prod_name, quantity in products_bought.items(): data += f"Left quantity of {prod_name}: {quantity}" + '\n' return data
class Shop: def __init__(self): self.product_repository = ProductRepository() self.customer_repository = CustomerRepository() def deliver(self, product_type: str, name: str): product = None if product_type == "Drink": return self.product_repository.add(Drink(name)) elif product_type == "Food": return self.product_repository.add(Food(name)) # method sell version 1: def customer_create_or_find(self, customer_name): if self.customer_repository.find(customer_name) == "None": self.customer_repository.add(Customer(customer_name)) customer = [ c for c in self.customer_repository.customers if c.name == customer_name ][0] else: customer = self.customer_repository.find(customer_name) return customer def sell(self, customer_name: str, **shopping_list): res = "" customer = self.customer_create_or_find(customer_name) for item, quantity in shopping_list.items(): product = self.product_repository.find(item) if product.quantity > shopping_list[item]: res += self.product_repository.decrease( product, shopping_list[item]) res += "\n" customer.products[item] = quantity else: res += f"Left quantity of {product.name}: 0\n" customer.products[item] = product.quantity self.product_repository.products.remove(product) return res # method sell version 2: # def sell(self, customer_name: str, **shopping_list): # bought_products_left_quantities = {} # customer = self.customer_repository.find(customer_name) # if customer == "None": # customer = self.customer_repository.add(Customer(customer_name)) # customer = [c for c in self.customer_repository.customers if c.name == customer_name][0] # item_names = [i.name for i in self.product_repository.products] # # for item, quantity in shopping_list.items(): # # if item in item_names: # item_in_shop = [i for i in self.product_repository.products if i.name == item][0] # if item_in_shop.quantity > quantity: # # # item_in_shop.quantity -= quantity # customer.products[item] = quantity # # bought_products_left_quantities[item] = item_in_shop.quantity # # elif item_in_shop.quantity <= quantity: # customer.products[item] = item_in_shop.quantity # self.product_repository.products.remove(item_in_shop) # bought_products_left_quantities[item] = 0 # # ll = [] # for key, value in bought_products_left_quantities.items(): # ll.append(key) # last_item = ll[-1] # res = "" # for item, quantity in bought_products_left_quantities.items(): # if not item == last_item: # res += f"Left quantity of product {item}: {quantity}\n" # else: # res += f"Left quantity of product {item}: {quantity}" # return res # # version 3 # def sell(self, customer_name: str, **shopping_list): # res = "" # if self.customer_repository.find(customer_name) == "None": # self.customer_repository.add(Customer(customer_name)) # customer = [c for c in self.customer_repository.customers if c.name == customer_name][0] # else: # customer = self.customer_repository.find(customer_name) # for item, quantity in shopping_list.items(): # product = self.product_repository.find(item) # try: # res += self.product_repository.decrease(product, shopping_list[item]) # res += "\n" # customer.products[item] = quantity # except ValueError: # customer.products[item] = quantity # res += f"Left quantity of {product.name}: 0\n" # self.product_repository.products.remove(product) # return res