+1 (315) 557-6473 

Creating a Basic E-commerce Simulator in Python

In this guide, we will take you through the process of building a basic e-commerce simulator in Python. This comprehensive guide is your gateway to understanding and creating a user-friendly e-commerce system. It empowers customers to log in, explore products, place orders, and personalize their profiles. Additionally, administrators will find essential tools for effective product management, customer administration, and data-driven decision-making through the generation of statistical figures. Whether you're a novice developer or an entrepreneur looking to embark on the e-commerce journey, this guide will serve as your fundamental resource to construct a fully operational online shopping experience.

Building a Python E-commerce Simulator

Explore creating an e-commerce platform in Python. We offer comprehensive guidance to help with your Python assignment, ensuring you grasp the intricacies of building an effective online shopping system. Our resources empower you to master Python for e-commerce development. With hands-on guides, code examples, and expert insights, you'll be equipped to create a fully functional e-commerce platform tailored to your needs. Dive into the world of Python and unlock endless possibilities for your e-commerce projects.

Imports

```python from user_operation import UserOperation from product_operation import ProductOperation from order_operation import OrderOperation from customer_operation import CustomerOperation from admin_operation import AdminOperation from interface import Interface import os ```

These import statements bring in various custom modules and libraries necessary for the e-commerce simulator. The modules include operations related to users, products, orders, customers, admin, and an interface for user interactions.

Initialization

```python current_logged_in_customer = None product_op = ProductOperation() user_op = UserOperation() order_op = OrderOperation() customer_op = CustomerOperation() ```

Here, global variables are defined:

  • `current_logged_in_customer` stores the currently logged-in customer or admin (initialized as `None`).
  • Instances of different operation classes are created (e.g., `ProductOperation`, `UserOperation`) to perform various tasks.

`login()`

```python def login(): global current_logged_in_customer while True: username, password = Interface.get_user_input("Enter your username and password separated by a space: ", 2) current_logged_in_customer = user_op.login(username, password) if not current_logged_in_customer: Interface.print_error_message("UserOperation.login", "Cannot log in the user.") else: return ```

This function allows a user to log in. It prompts the user for a username and password, validates the credentials, and sets the `current_logged_in_customer` variable if the login is successful. Error messages are displayed when login fails.

`register()`

```python def register(): while True: username, password, user_email, user_mobile = Interface.get_user_input("Enter your username, password, email and mobile separated by a space: ", 4) result = customer_op.register_customer(username, password, user_email, user_mobile) if result == True: return else: Interface.print_error_message("CustomerOperation.register", result) ```

This function allows a customer to register by providing a username, password, email, and mobile. It calls the `register_customer` method and displays error messages if registration fails.

`show_products()`

```python def show_products(): page_number = 1 while True: product_list = product_op.get_product_list(page_number) if product_list: Interface.show_list(current_logged_in_customer.user_role, "Product", product_list) action, = Interface.get_user_input("Enter 'n' for next page, 'p' for previous page, or 'q' to quit: ", 1) if action == "n": page_number += 1 elif action == "p": page_number -= 1 if page_number < 1: page_number = 1 elif action == "q": break else: Interface.print_error_message("ProductOperation.get_product_list", "No products to show.") break ```

This function displays a list of products to the user. It retrieves product data, shows a page of products, and allows the user to navigate through the list (next page, previous page, or quit).

`add_customer()`

```python def add_customer(): while True: username, password, user_email, user_mobile = Interface.get_user_input("Enter the customer's username, password, email, and mobile separated by a space: ", 4) result = customer_op.register_customer(username, password, user_email, user_mobile) if result == True: return else: Interface.print_error_message("CustomerOperation.register", result) ```

This function allows the admin to add a new customer. It prompts for username, password, email, and mobile, and calls the `register_customer` method to add the customer. Error messages are displayed if the operation fails.

`show_customers()`

```python def show_customers(): page_number = 1 while True: customer_list = customer_op.get_customer_list(page_number) if not customer_list: Interface.print_message("No customers to show.") break Interface.show_list(current_logged_in_customer.user_role, "Customer", customer_list) action, = Interface.get_user_input("Enter 'n' for the next page, 'p' for the previous page, or 'q' to quit: ", 1) if action == "n": page_number += 1 elif action == "p": page_number -= 1 if page_number < 1: page_number = 1 elif action == "q": break ```

This function displays a list of customers for admin management. It allows the admin to navigate through the list (next page, previous page, or quit).

`show_orders()`

```python def show_orders(): page_number = 1 while True: order_list = order_op.get_order_list(current_logged_in_customer.user_id, page_number) if not order_list: Interface.print_message("No orders to show.") break Interface.show_list(current_logged_in_customer.user_role, "Order", order_list) action, = Interface.get_user_input("Enter 'n' for the next page, 'p' for the previous page, or 'q' to quit: ", 1) if action == "n": page_number += 1 elif action == "p": page_number -= 1 if page_number < 1: page_number = 1 elif action == "q": break ```

This function displays a list of orders for the admin, with options to navigate through the list or quit.

`generate_test_data()`

```python def generate_test_data(): product_op.extract_products_from_files() order_op.generate_test_order_data() Interface.print_message("Test data generated successfully.") ```

This function generates test data for products and orders. It calls methods to extract products from files and generate test order data.

`generate_all_statistical_figures()`

```python def generate_all_statistical_figures(): product_op.generate_category_figure() product_op.generate_discount_figure() product_op.generate_likes_count_figure() product_op.generate_discount_likes_count_figure() order_op.generate_all_customers_consumption_figure() order_op.generate_all_top_10_best_sellers_figure() Interface.print_message("Statistical figures generated successfully.") ```

This function generates various statistical figures for products and orders, including category, discounts, likes count, and more.

`delete_all_data()`

```python def delete_all_data(): customer_op.delete_all_customers() product_op.delete_all_products() order_op.delete_all_orders() Interface.print_message("All data deleted successfully.") ```

This function deletes all data, including customers, products, and orders.

`show_profile()`

```python def show_profile(): global current_logged_in_customer Interface.print_message(f"\nuser_name: {current_logged_in_customer.user_name}") Interface.print_message(f"user_registration_time: {current_logged_in_customer.user_register_time}") Interface.print_message(f"user_role: {current_logged_in_customer.user_role}") Interface.print_message(f"user_email: {current_logged_in_customer.user_email}") Interface.print_message(f"user_mobile: {current_logged_in_customer.user_mobile}") Interface.print_message("\n") ```

This function displays the profile of the logged-in customer, showing details like username, registration time, role, email, and mobile.

`update_profile()`

```python def update_profile(): global current_logged_in_customer attribute_name, = Interface.get_user_input("What do you want to update (email or mobile): ", 1) value, = Interface.get_user_input("Enter the updated value: ", 1) if not customer_op.update_profile(attribute_name, value, current_logged_in_customer): Interface.print_message("Cannot update the profile.") else: Interface.print_message("Profile updated successfully.") ```

This function allows the customer to update their profile by providing an attribute name (email or mobile) and the updated value.

`main()`

```python def main(): global current_logged_in_customer if not os.path.exists("data"): # Create the folder if it doesn't exist os.makedirs("data") if not os.path.exists("data/figure"): # Create the folder if it doesn't exist os.makedirs("data/figure") admin_op = AdminOperation() admin_op.register_admin() while True: Interface.main_menu() choice, = Interface.get_user_input("Enter your choice: ", 1) if choice == "1": login() if current_logged_in_customer.user_role == "admin": while True: Interface.admin_menu() admin_choice, = Interface.get_user_input("Enter your choice: ", 1) if admin_choice == "1": show_products() elif admin_choice == "2": add_customer() elif admin_choice == "3": show_customers() elif admin_choice == "4": show_orders() elif admin_choice == "5": generate_test_data() elif admin_choice == "6": generate_all_statistical_figures() elif admin_choice == "7": delete_all_data() elif admin_choice == "8": current_logged_in_customer = None break else: Interface.print_error_message("Main", "Invalid choice.") else: while True: Interface.customer_menu() customer_choice, keyword = Interface.get_user_input("Enter your choice and an optional search keyword separated by a space: ", 2) if customer_choice == "1": show_profile() elif customer_choice == "2": update_profile() elif customer_choice == "3": if keyword: show_products(keyword) else: show_products() elif customer_choice == "4": show_orders() elif customer_choice == "5": order_op.generate_single_customer_consumption_figure(current_logged_in_customer.user_id) elif customer_choice == "6": current_logged_in_customer = None break else: Interface.print_error_message("Main", "Invalid choice.") elif choice == "2": register() elif choice == "3": break else: Interface.print_error_message("Main", "Invalid choice.") if __name__ == "__main__": main() ```

The `main()` function is the entry point of the program. It checks if data folders exist, registers an admin, and then presents menus and options for users (customers or admin) to interact with the e-commerce system.

Conclusion

By following this guide, you'll gain valuable insights into building a simple e-commerce simulator in Python. The code structure and functions provide a robust foundation for creating an e-commerce system that accommodates the needs of both customers and administrators. This guide demonstrates how to effectively organize and implement various e-commerce components, fostering your ability to develop more advanced and customized solutions for online businesses. With these newfound skills, you're well on your way to building dynamic e-commerce platforms, exploring endless possibilities for innovation and entrepreneurship in the digital marketplace.