Here's an updated version of the C program with additional features:
Shopping Mall Management System
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Define structure for products
typedef struct {
int id;
char name[50];
float price;
int quantity;
} Product;
// Define structure for users
typedef struct {
char username[50];
char password[50];
} User;
// Function to add products
void addProduct(Product *products, int *productCount) {
printf("Enter product ID: ");
scanf("%d", &products[*productCount].id);
printf("Enter product name: ");
scanf("%s", products[*productCount].name);
printf("Enter product price: ");
scanf("%f", &products[*productCount].price);
printf("Enter product quantity: ");
scanf("%d", &products[*productCount].quantity);
(*productCount)++;
}
// Function to display products
void displayProducts(Product *products, int productCount) {
printf("Product List:\n");
for (int i = 0; i < productCount; i++) {
printf("ID: %d, Name: %s, Price: %.2f, Quantity: %d\n",
products[i].id, products[i].name, products[i].price, products[i].quantity);
}
}
// Function to sell products
void sellProduct(Product *products, int productCount) {
int productId;
printf("Enter product ID to sell: ");
scanf("%d", &productId);
for (int i = 0; i < productCount; i++) {
if (products[i].id == productId) {
if (products[i].quantity > 0) {
products[i].quantity--;
printf("Product sold successfully!\n");
} else {
printf("Product out of stock!\n");
}
return;
}
}
printf("Product not found!\n");
}
// Function to authenticate users
int authenticateUser(User *users, int userCount, char *username, char *password) {
for (int i = 0; i < userCount; i++) {
if (strcmp(users[i].username, username) == 0 && strcmp(users[i].password, password) == 0) {
return 1;
}
}
return 0;
}
// Function to add users
void addUser(User *users, int *userCount) {
printf("Enter username: ");
scanf("%s", users[*userCount].username);
printf("Enter password: ");
scanf("%s", users[*userCount].password);
(*userCount)++;
}
// Function to display users
void displayUsers(User *users, int userCount) {
printf("User List:\n");
for (int i = 0; i < userCount; i++) {
printf("Username: %s\n", users[i].username);
}
}
// Function to save data to file
void saveData(Product *products, int productCount, User *users, int userCount) {
FILE *fp;
fp = fopen("data.txt", "w");
if (fp == NULL) {
printf("Error opening file!\n");
return;
}
for (int i = 0; i < productCount; i++) {
fprintf(fp, "%d,%s,%.2f,%d\n", products[i].id, products[i].name, products[i].price, products[i].quantity);
}
for (int i = 0; i < userCount; i++) {
fprintf(fp, "%s,%s\n", users[i].username, users[i].password);
}
fclose(fp);
}
// Function to load data from file
void loadData(Product *products, int *productCount, User *users, int *userCount) {
FILE *fp;
fp = fopen("data.txt", "r");
if (fp == NULL) {
printf("Error opening file!\n");
return;
}
char line[100];
while (fgets(line, sizeof(line), fp)) {
char *token = strtok(line, ",");
if (token != NULL) {
products[*productCount].id = atoi(token);
token = strtok(NULL, ",");
strcpy(products[*productCount].name, token);
token = strtok(NULL, ",");
products[*productCount].price = atof(token);
token = strtok(NULL, "\n");
products[*productCount].quantity = atoi(token);
(*productCount)++;
} else {
token = strtok(line, ",");
strcpy(users[*userCount].username, token);
token = strtok(NULL, "\n");
strcpy(users[*userCount].password, token);
(*userCount)++;
}
}
fclose
fclose(fp);
}
int main() {
Product products[100];
int productCount = 0;
User users[100];
int userCount = 0;
int choice;
char username[50];
char password[50];
// Load data from file
loadData(products, &productCount, users, &userCount);
while (1) {
printf("Shopping Mall Management System\n");
printf("1. Login\n");
printf("2. Register\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter username: ");
scanf("%s", username);
printf("Enter password: ");
scanf("%s", password);
if (authenticateUser(users, userCount, username, password)) {
printf("Login successful!\n");
while (1) {
printf("1. Add Product\n");
printf("2. Display Products\n");
printf("3. Sell Product\n");
printf("4. Display Users\n");
printf("5. Logout\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addProduct(products, &productCount);
break;
case 2:
displayProducts(products, productCount);
break;
case 3:
sellProduct(products, productCount);
break;
case 4:
displayUsers(users, userCount);
break;
case 5:
printf("Logout successful!\n");
break;
default:
printf("Invalid choice!\n");
}
}
} else {
printf("Invalid username or password!\n");
}
break;
case 2:
addUser(users, &userCount);
printf("Registration successful!\n");
break;
case 3:
printf("Exiting...\n");
saveData(products, productCount, users, userCount);
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
This program includes the following features:
1. *User authentication*: Users can log in with their username and password.
2. *User registration*: New users can register by providing a username and password.
3. *Product management*: Users can add, display, and sell products.
4. *User management*: Users can display the list of registered users.
5. *Data persistence*: Program data is saved to a file when the program exits.
Note that this program uses a simple text-based file for data storage, which is not secure for storing sensitive information like passwords. In a real-world application, you would want to use a more secure method of storing user credentials.
No comments:
Post a Comment