Question: Task 1 Define a Python dataclass ( you must use dataclass not class ) named Product to represent product information. Each product should have the

Task 1 Define a Python dataclass (you must use dataclass not class) named Product to represent product information. Each product should have the following attributes:
- id (int): A unique identifier for the product.
- name (str): The name of the product.
- category (str): The category to which the product belongs.
- price (float): The price of the product.
- stock (int): The number of units available in stock.
Task 2 Write a Python script that performs the following actions:
1. Reads product data from a file named products.txt. Each line in the file follows this format:
id,name, category,price,stock
Example data:
101,Apple, Fruit, 0.5,100
102, Banana, Fruit, 0.2,150
103,Milk, Dairy, 1.2,50
2. Populates a dictionary where the key is the product's id, and the value is a Product object created from the file data.
Task 3 Implement the following functionalities:
- Total stock value per category: Compute and print the total stock value for each category, where the stock value is defined as price multiplied by stock. This function should take a dictionary as parameter and
- Most expensive product: Identify and display the product with the highest price.
- Category with the highest stock: Determine which category has the most items in stock (calculated as the sum of the stock for all products in the category).
All functions in Task 3 should take a single parameter - a Python dictionary. Each function in this task must required information like presented in sample output.
Task 1 Define a Python dataclass ( you must use

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!