Consider a store that sells 6 items from item1-item6. Assume that the available quantity…
- User
Consider a store that sells 6 items from item1-item6. Assume that the available quantity of each item is 50 and price of each item1-item6 is $4 per item. A buyer gets discount of 1%, 2%, 3%,4%, 5% and 6% respectively on item1-item6 per item for buying more than 10 items. For example, if a buyer purchases 5 of item1 and 11 of item2 he/she will get 2% discount on item2. Write a code that runs for five customers and updates the stock every time you are done with a customer. When you run the code it should first display the following message. (a) Welcome to our Test Store! (b) Your name please...Here you enter the buyer name When you enter it should display the current stock in the following form, item Qty Price/item Discount item1 50 4 1% .... ... ... ... In the next step it should ask the order from the buyer, Your order please...User will enter the name and quantity of the item(s) in the form of a dictionary When you press enter it should display user invoice in the following format Dear username thank you for shopping with us. Please find below your invoice. item Qty Price/item Discount Price item1 5 4 0% 20 item2 11 4 2% 38.46 ................... Total = 58.46 Also, create a txt file by name of user name and print the above invoice in it. Once you are done with the first customer start the process from step (b) for the second customer. Repeat these steps for all five customers and display the sales report on the screen in the following format. item Available Qty Sold Qty Revenue item1 ... ... ... 1 item2 ... ... ... ..................... Total Sale = ... Finally, print the sales report in a txt file by the name of ’SalesReport.txt’. You should define some functions such as get price(),get discount(), get stock() get sale(), etc., in a separate file to perform various required tasks. Then, connect these functions in an another file to complete the code.
- Assistant
First, let's create a file named
functions.pywhere we will define the necessary functions.# functions.py def get_price(item_qty, price_per_item, discount): total_price = item_qty * price_per_item * (1 - discount) return total_price def get_discount(item_qty, discount_rates): if item_qty > 10: discount = discount_rates else: discount = 0 return discount def get_stock(items, available_qty, price_per_item, discount_rates): stock = [] for item, qty, price, discount_rate in zip(items, available_qty, price_per_item, discount_rates): stock.append([item, qty, price, discount_rate]) return stock def get_sale(items, order_qty, stock): sold_qty = [0] * len(items) total_revenue = 0 for item, qty in zip(items, order_qty): index = items.index(item) new_qty = stock[index][1] - qty sold_qty[index] += qty total_revenue += qty * stock[index][2] * (1 - stock[index][3]) return sold_qty, total_revenue def update_stock(stock, sold_qty): for item, qty in zip(stock, sold_qty): item[1] -= qtyNow, let's create another file named
main.pyand connect the functions defined above.# main.py from functions import get_price, get_discount, get_stock, get_sale, update_stock items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6'] available_qty = [50] * len(items) price_per_item = [4] * len(items) discount_rates = [i * 0.01 for i in range(1, 7)] stock = get_stock(items, available_qty, price_per_item, discount_rates) for _ in range(5): print("Welcome to our Test Store!") buyer_name = input("Your name please: ").strip() print(f"Current stock: {'Item':<8} {'Qty':<10} {'Price/item':<15} {'Discount'}") for item_info in stock: item, qty, price, discount_rate = item_info print(f"{item:<8} {qty:<10} {price:<15} {discount_rate * 100:.0f}%") order = eval(input("Your order please (format: {'item1': 4, 'item2': 11}): ")) order_qty = [order.get(item, 0) for item in items] invoice_file = f"{buyer_name}_invoice.txt" with open(invoice_file, "w") as f: f.write(f"Dear {buyer_name}, thank you for shopping with us.\n") f.write("Please find below your invoice.\n") f.write(f"{'Item':<8} {'Qty':<10} {'Price/item':<15} {'Discount':<10} {'Price'}\n") total = 0 for item, qty, price_item, discount_rate in zip(items, order_qty, price_per_item, discount_rates): if qty > 0: discount = get_discount(qty, discount_rate) price = get_price(qty, price_item, discount) total += price f.write(f"{item:<8} {qty:<10} {price_item:<15} {discount * 100:.0f}% {price:.2f}\n") f.write(f"{'-' * 35}\n") f.write(f"{'Total':<35} {total:.2f}") sold_qty, total_revenue = get_sale(items, order_qty, stock) update_stock(stock, sold_qty) print("Sales report:") print(f"{'Item':<15} {'Available Qty':<15} {'Sold Qty':<15} {'Revenue'}") total_sale = 0 for item_info, qty_sold, revenue in zip(stock, sold_qty, total_revenue): item, available_qty, _, _ = item_info total_sale += revenue print(f"{item:<15} {available_qty:<15} {qty_sold:<15} {revenue:.2f}") print(f"{'-' * 50}") print(f"{'Total Sale':<45} {total_sale:.2f}") with open("SalesReport.txt", "w") as f: f.write("Sales report:\n") f.write(f"{'Item':<15} {'Available Qty':<15} {'Sold Qty':<15} {'Revenue'}\n") for item_info, qty_sold, revenue in zip(stock, sold_qty, total_revenue): item, available_qty, _, _ = item_info f.write(f"{item:<15} {available_qty:<15} {qty_sold:<15} {revenue:.2f}\n") f.write(f"{'-' * 50}\n") f.write(f"{'Total Sale':<45} {total_sale:.2f}")Now, run the
main.pyfile and interact with the console to buy items for 5 customers. After completing the process, you will find the invoices for each customer with their name and aSalesReport.txtfile with the sales report.