Web-commerce Using c
Web-commerce Using c
This code is a header file (ecommerce.h) for a simple e-commerce system that provides
functionality to manage users, products, and their interactions within the system.
It includes data structures and function prototypes for managing and tracking user-product
interactions like browsing and purchasing.
1. Macros
2. Data Structures
User
This code is a header file (ecommerce.h) for a simple e-commerce system that provides
functionality to manage users, products, and their interactions within the system. It includes
data structures and function prototypes for managing and tracking user-product interactions
like browsing and purchasing.
Key Components
1. Macros
2. Data Structures
User
} User;
Product
} Product;
Interaction
} Interaction;
This structure uses a linked list to store multiple interactions, enabling dynamic growth.
ECommerceContext
} ECommerceContext;
This structure is the central context of the system, storing all users, products, and
interactions.
3. Function Prototypes
These are declarations of functions that will implement various functionalities of the
e-commerce system:
● Initialization
○ void initECommerceContext(ECommerceContext* context);
Initializes an ECommerceContext instance, setting up tables and the
interaction graph.
● User Management
○ void addUser(ECommerceContext* context, int id, char*
name, char* email); Adds a user to the userTable with the given
details.
○ User* getUser(ECommerceContext* context, int userId);
Retrieves a user by their ID.
○ void displayUser(User* user); Displays details of a specific user.
● Product Management
○ void addProduct(ECommerceContext* context, int id, char*
name, float price); Adds a product to the productTable with the
given details.
○ Product* getProduct(ECommerceContext* context, int
productId); Retrieves a product by its ID.
○ void displayProduct(Product* product); Displays details of a
specific product.
● Interaction Tracking
○ void trackInteraction(ECommerceContext* context, int
userId, int productId, int interactionType); Records an
interaction (browse or purchase) between a user and a product.
● Reports and Analysis
○ void generateRecommendations(ECommerceContext* context,
int userId); Generates product recommendations for a user based on
their interactions.
○ void displayBrowsingHistory(ECommerceContext* context,
int userId); Displays the browsing history of a user.
○ void displayUserActivity(ECommerceContext* context, int
userId); Displays all activity (browsing and purchasing) of a user.
This header file defines the foundational components of the e-commerce system. It focuses
on:
1. Managing users and products: Adding, retrieving, and displaying them.
2. Tracking interactions: Logging user activities like browsing or purchasing.
3. Analysis: Generating user-specific recommendations and activity reports.
The implementation details for these functions will be provided in the corresponding .c file.
Function 3
Tracking User Interactions
To track user interactions, I used a linked list (for its dynamic nature) and inserting
interaction between users and products.
1. The first thing I did was created a node newInteraction to represent the interaction.
2. This line creates space in memory for a new interaction node.
3. It uses malloc to ensure the size is appropriate for the Interaction structure,
making it ready to hold the interaction data.
newInteraction->userId = userId;
Stores the userId (the ID of the user performing the interaction).
newInteraction->productId = productId;
Stores the productId (the product involved in the interaction).
newInteraction->interactionType = interactionType;
Stores the interactionType, where 0 means the user browsed the product and 1 means
they purchased it.
newInteraction->next = context->interactionGraph;
Links the new interaction to the current head of the list. This ensures the new interaction
"points" to the previous head.
context->interactionGraph = newInteraction;
Updates the head of the list (context->interactionGraph) to the new interaction. This
makes the new interaction the first item in the list.
I chose a singly linked list because interactions only needed to be stored and retrieved
sequentially.
● I ensured the function could handle multiple types of interactions (browsing and
purchasing) by adding the interactionType field, making the system versatile
for future use.
Purpose:
The getUser function retrieves user information from the system based on the user's
unique ID.
How it works:
2. Hashing:
return id % tableSize;
● Fast Lookup: Hashing allows near-constant time complexity for accessing a user's
data.
● Simple Implementation: The array-based hash table provides a straightforward and
efficient way to manage fixed-size datasets.
Code Walkthrough:
}
1. Linked List
● Definition: A sequence of nodes where each node contains data and a pointer to the
next node.
● Why Use It?: It allows dynamic growth without worrying about predefined sizes.
Perfect for cases like the interaction log, where the number of interactions is
unpredictable.
2. Hash Table
● Definition: A data structure that maps keys (like user IDs) to values (like user
information) using a hash function.
● Why Use It?: It enables fast and direct access to stored data, making it ideal for
retrieving users or products based on their unique IDs.