0% found this document useful (0 votes)
6 views4 pages

User Authentication and Inventory System

python

Uploaded by

Humera Raza
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

User Authentication and Inventory System

python

Uploaded by

Humera Raza
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

class User:

def __init__(self, username, password, role):

[Link] = username

[Link] = password

[Link] = role

class AuthSystem:

def __init__(self):

[Link] = {

'admin': User('admin', 'adminpass', 'Admin'),

'user': User('user', 'userpass', 'User')

def login(self, username, password):

user = [Link](username)

if user and [Link] == password:

return user

return None

class Product:

def __init__(self, product_id, name, category, price, stock_quantity):

self.product_id = product_id

[Link] = name

[Link] = category

[Link] = price

self.stock_quantity = stock_quantity

class Inventory:

def __init__(self):

[Link] = {}

def add_product(self, product):

[Link][product.product_id] = product
def edit_product(self, product_id, **kwargs):

if product_id in [Link]:

for key, value in [Link]():

setattr([Link][product_id], key, value)

def delete_product(self, product_id):

if product_id in [Link]:

del [Link][product_id]

def view_all_products(self):

for product in [Link]():

print(product.__dict__)

class InventoryManager:

def __init__(self):

[Link] = Inventory()

def check_stock_levels(self, threshold):

for product in [Link]():

if product.stock_quantity < threshold:

print(f"Stock of {[Link]} is low. Consider restocking.")

def search_by_name(self, name):

for product in [Link]():

if [Link] == name:

print(product.__dict__)

def filter_by_category(self, category):

for product in [Link]():

if [Link] == category:

print(product.__dict__)

def find_product(self, product_id):

try:
product = [Link](product_id)

if not product:

raise InventoryError("Product not found.")

return product

except InventoryError as e:

print(e)

class InventoryError(Exception):

pass

def main():

auth_system = AuthSystem()

inventory_manager = InventoryManager()

username = input("Enter username: ")

password = input("Enter password: ")

user = auth_system.login(username, password)

if not user:

print("Invalid login credentials.")

return

while True:

if [Link] == 'Admin':

print("1. Add product")

print("2. Edit product")

print("3. Delete product")

print("4. View all products")

print("5. Check stock levels")

option = int(input("Choose an option: "))

if option == 1:

product_id = input("Enter product ID: ")

name = input("Enter product name: ")


category = input("Enter product category: ")

price = float(input("Enter product price: "))

stock_quantity = int(input("Enter product stock quantity: "))

product = Product(product_id, name, category, price, stock_quantity)

inventory_manager.inventory.add_product(product)

elif option == 2:

product_id = input("Enter product ID to edit: ")

updates = {}

updates['name'] = input("Enter new product name (or press enter to skip): ")

updates['category'] = input("Enter new product category (or press enter to skip): ")

updates['price'] = float(input("Enter new product price (or press enter to skip): "))

updates['stock_quantity'] = int(input("Enter new stock quantity (or press enter to skip): "))

inventory_manager.inventory.edit_product(product_id, **{k: v for k, v in [Link]() if v})

elif option == 3:

product_id = input("Enter product ID to delete: ")

inventory_manager.inventory.delete_product(product_id)

elif option == 4:

inventory_manager.inventory.view_all_products()

elif option == 5:

threshold = int(input("Enter stock threshold: "))

inventory_manager.check_stock_levels(threshold)

elif [Link] == 'User':

inventory_manager.inventory.view_all_products()

if input("Do you want to logout? (yes/no): ").lower() == 'yes':

break

if __name__ == "__main__":

main()

Common questions

Powered by AI

Yes, an admin can update selective attributes of a product by using the edit_product method of the Inventory class. This method allows updating specific product attributes by passing a dictionary of new values, ignoring `None` values or skipped inputs . This approach ensures partial updates can be conducted without altering unchanged data, maintaining the accuracy of the inventory.

The system requires a valid username and password for a user to log in successfully. The AuthSystem verifies these credentials against stored values in its user dictionary . This requirement is necessary to prevent unauthorized access, ensuring that only authenticated users can perform role-specific operations, thereby maintaining system security integrity.

The system facilitates product searches by name or category through the InventoryManager methods search_by_name and filter_by_category. These methods iterate over the inventory, printing products that match the search criteria . Such functionality enhances efficiency in managing large inventories, enabling quick access to specific products for restocking, adjustments, or customer inquiries, thereby optimizing operational workflows.

An admin might choose to view all products to oversee inventory status comprehensively or perform audits. This is implemented in the InventoryManager class via the view_all_products method of the Inventory class, which iterates through all products and outputs their details . It provides a transparent overview, aiding in informed decision-making and strategic planning.

The InventoryError class serves as a custom exception type to handle specific inventory-related errors, such as product not found scenarios . Its importance lies in offering a centralized and meaningful way to manage error conditions, enhancing code readability and allowing for precise error handling and user feedback, thus contributing to robust system operation.

The AuthSystem class distinguishes roles by associating each user with a specific role ('Admin' or 'User') via the User class, which stores username, password, and role information . This differentiation allows the system to grant permissions based on the user's role. Admins can add, edit, delete, and view products or check stock levels, whereas users can only view products . This role-based access is critical for maintaining security and operational integrity within the inventory management system.

The InventoryManager class monitors stock levels using the check_stock_levels method, which compares each product's stock quantity against a specified threshold and alerts when stock is low . This functionality is vital for businesses to maintain optimal inventory levels, preventing stockouts that could lead to lost sales and customer dissatisfaction, while also avoiding overstocking that could increase storage costs.

When a product is not found during a search operation, the system raises an InventoryError with the message "Product not found." This exception handling is encapsulated within a try-except block when attempting to retrieve a product, allowing the system to gracefully manage search errors without crashing . This mechanism ensures users are informed about the issue and can take corrective actions.

To add a product to the inventory, an admin must input the product ID, name, category, price, and stock quantity. This information is then used to instantiate a Product object, which is added to the inventory using the add_product method of the Inventory class . These steps ensure all products are consistently cataloged, facilitating accurate inventory tracking and preventing discrepancies.

A user is prompted to logout during inventory viewing in the main method, specifically when a user with the role 'User' chooses to end their session. The system asks, "Do you want to logout? (yes/no)" and logs out upon a 'yes' response . This illustrates a design focus on user session management, providing clear exit options to ensure secure access termination.

You might also like