|
1 | 1 | # Exercise 61 - Inventory Management System |
2 | | -# Write a CSV file containing inventory data and generates a report on stock levels and restock alerts. |
3 | | -# The CSV file contains columns: item_name, item_id, quantity, and price. |
4 | | -# Read the CSV file and calculate the total value of the inventory. |
| 2 | +# Given a text file containing inventory data and generates a report on stock levels and restock alerts. |
| 3 | +# This file contains columns: item_name, quantity, and price. |
| 4 | +# Read the file and calculate the total value of the inventory. |
5 | 5 | # Identify items with stock levels below the restock threshold and store them in a list. |
6 | 6 | # Return a dictionary containing the total inventory value and the list of items that need restocking. |
7 | 7 |
|
8 | | -# Example data.csv: |
9 | | -# item_name,item_id,quantity,price |
| 8 | +# Example data.txt: |
| 9 | +# item_name,quantity,price |
10 | 10 | # Apple,1,100,0.5 |
11 | 11 | # Banana,2,200,0.3 |
12 | 12 | # Orange,3,150,0.4 |
13 | 13 |
|
14 | | -# The function should return: |
15 | | - |
| 14 | +# With restock threshold of 200. The function should return: |
16 | 15 | # { |
17 | | -# "total_inventory_value": 140.0, |
18 | | -# "restock_alerts": [ |
19 | | -# {"item_name": "Apple", "item_id": 1, "quantity": 100, "price": 0.5}, |
20 | | -# {"item_name": "Orange", "item_id": 3, "quantity": 150, "price": 0.4}, |
21 | | -# ], |
| 16 | +# "total_inventory_value": 170.0, |
| 17 | +# "restock_alerts": ["Apple", "Orange"] |
22 | 18 | # } |
23 | 19 |
|
24 | | -# The restock threshold is 200 items. |
25 | 20 | # The total inventory value is calculated as the sum of quantity * price for each item. |
26 | 21 | # The restock alerts are items with a quantity below the restock threshold. |
27 | 22 |
|
28 | 23 |
|
29 | 24 | def inventory_management( |
30 | 25 | file_name: str, restock_threshold: int |
31 | 26 | ) -> dict[str, list[dict[str, str]]]: |
32 | | - # Your code should go here. |
| 27 | + total_inventory_value = 0.0 |
| 28 | + restock_alerts = [] |
| 29 | + |
| 30 | + with open(file_name, "r") as file: |
| 31 | + # Read the header line |
| 32 | + header = file.readline().strip().split(",") |
| 33 | + |
| 34 | + # Check if header is correct |
| 35 | + if header != ["item_name", "quantity", "price"]: |
| 36 | + raise ValueError("Incorrect CSV format") |
| 37 | + |
| 38 | + for line in file: |
| 39 | + # Split each line into columns |
| 40 | + columns = line.strip().split(",") |
| 41 | + |
| 42 | + # Extract data from columns |
| 43 | + item_name = columns[0] |
| 44 | + quantity = int(columns[1]) |
| 45 | + price = float(columns[2]) |
| 46 | + |
| 47 | + # Calculate the total inventory value |
| 48 | + total_inventory_value += quantity * price |
| 49 | + |
| 50 | + # Check if restock is needed |
| 51 | + if quantity < restock_threshold: |
| 52 | + restock_alerts.append(item_name) |
33 | 53 |
|
34 | | - ... |
| 54 | + return { |
| 55 | + "total_inventory_value": total_inventory_value, |
| 56 | + "restock_alerts": restock_alerts, |
| 57 | + } |
0 commit comments