首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >C语言之超市商品管理系统

C语言之超市商品管理系统

作者头像
LucianaiB
发布2025-01-21 12:42:45
发布2025-01-21 12:42:45
4260
举报
现代零售业务中,一个高效的商品管理系统对于提升运营效率和顾客满意度至关重要。本文将介绍一个基于C语言实现的超市商品管理系统,该系统为管理员(卖家)提供了商品管理功能,同时也为消费者(买家)提供了购买商品的功能。系统设计简洁,功能实用,适合小型超市或零售店使用。
系统功能概述

超市商品管理系统主要分为管理员功能和消费者功能:

管理员功能

  1. 进货功能:管理员可以输入商品的编号、名称、进价、售价和数量,完成进货操作。
  2. 查询商品信息:通过商品编号查询商品的详细信息。
  3. 修改商品信息:通过商品编号修改商品的名称、进价、售价和库存数量。
  4. 删除商品:通过商品编号删除商品。
  5. 查看盈利情况:系统根据商品的进价和售价计算盈利情况。
  6. 退出:退出管理员功能模块,返回主菜单。

消费者功能

  1. 购买商品:消费者可以输入商品编号和购买数量,将商品加入购物车。
  2. 修改购物车:消费者可以修改购物车中商品的数量或删除商品。
  3. 查看购物车:消费者可以查看购物车中的商品和数量。
  4. 结账:消费者完成购物并结账。
  5. 退出:退出消费者功能模块,返回主菜单。
系统设计

系统设计基于以下结构体和功能模块:

商品结构体

代码语言:javascript
复制
struct Product {
    int id;
    char name[50];
    float cost_price;
    float selling_price;
    int quantity;
};

购物车项结构体

代码语言:javascript
复制
struct CartItem {
    int product_id;
    int quantity;
};

管理员功能

  • 进货功能:通过addProduct()函数实现。
  • 查询商品信息:通过displayProduct()函数实现。
  • 修改商品信息:通过modifyProduct()函数实现。
  • 删除商品:通过deleteProduct()函数实现。
  • 查看盈利情况:通过viewProfit()函数实现。

消费者功能

  • 购买商品:通过buyProduct()函数实现。
  • 修改购物车:通过modifyCart()函数实现。
  • 查看购物车:通过viewCart()函数实现。
  • 结账:通过checkout()函数实现。
系统运行流程
  1. 启动系统:程序启动时显示主菜单,用户可以选择管理员功能或消费者功能。
  2. 管理员登录:选择管理员功能时,需要输入用户名和密码进行验证。
  3. 管理员操作:登录成功后,管理员可以选择进货、查询、修改、删除商品或查看盈利情况。
  4. 消费者操作:消费者无需登录,可以直接购买商品、修改购物车、查看购物车或结账。
  5. 退出系统:用户可以选择退出当前功能模块,返回主菜单或退出程序。
总结

本文介绍了一个基于C语言实现的超市商品管理系统,旨在为管理员和消费者提供高效的商品管理与购物体验。系统通过简洁的设计和实用的功能模块,满足了小型超市或零售店的日常运营需求。

系统的核心功能分为管理员功能和消费者功能。管理员可以通过系统进行商品的进货、信息查询、修改、删除以及查看盈利情况。消费者则可以购买商品、修改购物车、查看购物车内容并完成结账。系统通过结构体数组存储商品信息,并利用购物车项结构体管理消费者的购物车。

在设计上,系统使用了两个主要的结构体:Product用于存储商品信息,包括编号、名称、进价、售价和库存数量;CartItem用于存储购物车中的商品项。管理员功能模块包括进货、查询、修改、删除商品以及查看盈利情况,分别由addProduct()displayProduct()modifyProduct()deleteProduct()viewProfit()函数实现。消费者功能模块则包括购买商品、修改购物车、查看购物车和结账,分别由buyProduct()modifyCart()viewCart()checkout()函数实现。

系统运行流程从主菜单开始,用户可以选择进入管理员或消费者功能模块。管理员需要通过用户名和密码登录,而消费者无需登录即可直接购物。系统通过循环和条件语句实现功能选择,并在用户完成操作后返回主菜单或退出程序。

该系统通过简单的C语言结构和函数实现了商品管理与购物的核心功能,具有较高的可扩展性和可维护性。虽然系统功能较为基础,但已足够满足小型零售业务的日常需求。未来可以进一步扩展功能,例如增加用户界面、支持文件存储或网络功能,以提升系统的实用性和用户体验。

代码实现
代码语言:javascript
复制
#include <stdio.h>
#include <string.h>

// 商品结构体
struct Product {
    int id;
    char name[50];
    float cost_price;
    float selling_price;
    int quantity;
};

// 商品数据库,用结构体数组表示
struct Product products[100];
int num_products = 0;

// 购物车项结构体
struct CartItem {
    int product_id;
    int quantity;
};

// 购物车数组和商品数量
struct CartItem cart[100];
int cart_size = 0;

// 管理员账号和密码
const char admin_username[] = "test1";
const char admin_password[] = "test1";

// 函数声明
void adminMenu();
void customerMenu();
void addProduct();
void displayProduct(int id);
void modifyProduct(int id);
void deleteProduct(int id);
void viewProfit();
void buyProduct();
void modifyCart();
void viewCart();
void checkout();
int cartContainsProduct(int product_id);
void addToCart(int product_id, int quantity);
void removeFromCart(int product_id);
void modifyCartQuantity(int product_id, int new_quantity);

int main() {
    // 主菜单
    int choice;
    do {
        printf("-------------------\n");
        printf("|    Main Menu    |\n");
        printf("|    1. Admin     |\n");
        printf("|    2. Customer  |\n");
        printf("|    3. Exit      |\n");
        printf("-------------------\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                adminMenu();
                break;
            case 2:
                customerMenu();
                break;
            case 3:
                printf("Exiting...\n");
                break;
            default:
                printf("Invalid choice. Please try again.\n");
        }
    } while (choice != 3);

    return 0;
}

void adminMenu() {
    // 管理员登录验证
    char username[50];
    char password[50];
    printf("-------------------\n");
    printf(" |  Admin Sign in  |\n");
    printf(" ------------------\n");
    printf("Username: ");
    scanf("%s", username);
    printf("Password: ");
    scanf("%s", password);

    // 验证用户名和密码
    if (strcmp(username, admin_username) == 0 && strcmp(password, admin_password) == 0) {
        printf("Sign in Successful!!\n");

        // 在登录成功后显示管理员功能菜单
        int choice;
        do {
            printf("---------------------------\n");
            printf("|      Admin Menu         |\n");
            printf("|    1. Add Product       |\n");
            printf("|    2. Display Product   |\n");
            printf("|    3. Modify Product    |\n");
            printf("|    4. Delete Product    |\n");
            printf("|    5. View Profit       |\n");
            printf("|    6. Exit              |\n");
            printf("---------------------------\n");
            printf("Enter your choice: ");
            scanf("%d", &choice);

            switch (choice) {
                case 1:
                    addProduct(); // Add a new product
                    break;
                case 2:
                    printf("Enter the product ID to display: ");
                    int display_id;
                    scanf("%d", &display_id);
                    displayProduct(display_id); // Display product details
                    break;
                case 3:
                    printf("Enter the product ID to modify: ");
                    int modify_id;
                    scanf("%d", &modify_id);
                    modifyProduct(modify_id); // Modify product information
                    break;
                case 4:
                    printf("Enter the product ID to delete: ");
                    int delete_id;
                    scanf("%d", &delete_id);
                    deleteProduct(delete_id); // Delete a product
                    break;
                case 5:
                    viewProfit(); // View profit information
                    break;
                case 6:
                    printf("Exiting the admin menu...\n");
                    break;
                default:
                    printf("Invalid choice, please try again.\n");
            }
        } while (choice != 6);
    } else {
        printf("Login failed. Incorrect username or password.\n");
    }
}

void customerMenu() {
    // 消费者功能菜单
    int choice;
    do {
        printf("---------------------------\n");
        printf("|       Customer Menu      |\n");
        printf("|      1. Buy Product      |\n");
        printf("|      2. Modify Cart      |\n");
        printf("|      3. View Cart        |\n");
        printf("|      4. Checkout         |\n");
        printf("|      5. Exit             |\n");
        printf("---------------------------\n");
        printf("Enter your choice: ");

        // 清空输入缓冲区
        fflush(stdin);

        if (scanf("%d", &choice) != 1) {
            printf("Invalid input. Please enter a number.\n");
            // 清空输入缓冲区
            while (getchar() != '\n');
            continue;
        }

        if (choice < 1 || choice > 5) {
            printf("Invalid choice. Please enter a number between 1 and 5.\n");
            continue;
        }

        switch (choice) {
            case 1:
                buyProduct();
                break;
            case 2:
                modifyCart();
                break;
            case 3:
                viewCart();
                break;
            case 4:
                checkout();
                break;
            case 5:
                printf("Exiting customer menu...\n");
                break;
            default:
                printf("Invalid selection, please try again.\n");
        }
    } while (choice != 5);
}

void addProduct() {
    // 检查是否达到最大商品数量限制
    if (num_products >= 100) {
        printf("The maximum number of products has been reached. Unable to add more products.\n");
        return;
    }

    // 输入新商品信息 商品编号 商品名称 商品进价 商品售价 商品库存数量
    struct Product new_product;
    printf("Enter information for the new product:\n");
    printf("Product ID: ");
    scanf("%d", &new_product.id);
    printf("Product Name: ");
    scanf("%s", new_product.name);
    printf("Cost Price: ");
    scanf("%f", &new_product.cost_price);
    printf("Selling Price: ");
    scanf("%f", &new_product.selling_price);
    printf("Quantity in Stock: ");
    scanf("%d", &new_product.quantity);

    // 将新商品添加到商品数组中
    products[num_products] = new_product;
    num_products++;

    printf("Product added successfully!!\n");
}

void displayProduct(int id) {
    // 根据商品编号显示特定商品的信息
    for (int i = 0; i < num_products; i++) {
        if (products[i].id == id) {
            printf("Product ID: %d\n", products[i].id);
            printf("Product Name: %s\n", products[i].name);
            printf("Cost Price: %.2f\n", products[i].cost_price);
            printf("Selling Price: %.2f\n", products[i].selling_price);
            printf("Quantity in Stock: %d\n", products[i].quantity);
            return;
        }
    }
    printf("Product not found.\n");
}

void modifyProduct(int id) {
    // 根据商品编号修改商品信息
    for (int i = 0; i < num_products; i++) {
        if (products[i].id == id) {
            printf("Enter the new product information:\n");
            printf("Product Name: ");
            scanf("%s", products[i].name);
            printf("Cost Price: ");
            scanf("%f", &products[i].cost_price);
            printf("Selling Price: ");
            scanf("%f", &products[i].selling_price);
            printf("Quantity in Stock: ");
            scanf("%d", &products[i].quantity);
            printf("Product information modified successfully!\n");
            return;
        }
    }
    printf("Product not found.\n");
}

void deleteProduct(int id) {
    // 根据商品编号删除商品
    for (int i = 0; i < num_products; i++) {
        if (products[i].id == id) {
            for (int j = i; j < num_products - 1; j++) {
                products[j] = products[j + 1];
            }
            num_products--;
            printf("Product deleted successfully!\n");
            return;
        }
    }
    printf("Product not found.\n");
}

void viewProfit() {
    // 查看盈利情况
    float total_profit = 0;
    for (int i = 0; i < num_products; i++) {
        total_profit += (products[i].selling_price - products[i].cost_price) * products[i].quantity;
    }
    printf("Total profit: %.2f\n", total_profit);
}

void buyProduct() {
    int product_id, quantity;
    printf("Enter the product ID you want to buy: ");
    scanf("%d", &product_id);

    // 查找商品是否存在
    int product_index = -1;
    for (int i = 0; i < num_products; i++) {
        if (products[i].id == product_id) {
            product_index = i;
            break;
        }
    }

    if (product_index == -1) {
        printf("Product not found.\n");
        return;
    }

    printf("Enter the quantity to purchase: ");
    scanf("%d", &quantity);

    // 检查库存是否足够
    if (products[product_index].quantity >= quantity) {
        // 更新库存数量
        products[product_index].quantity -= quantity;

        // 添加到购物车
        if (cart_size < 100) { // 确保购物车不会超过最大容量
            cart[cart_size].product_id = product_id;
            cart[cart_size].quantity = quantity;
            cart_size++;
        }

        printf("Purchase successful!\n");
    } else {
        printf("Insufficient stock, purchase failed.\n");
    }
}

void modifyCart() {
    int choice, item_id, quantity;
    do {
        printf("---------------------------\n");
        printf("|   Modify Cart Menu      |\n");
        printf("|  1. Add to Cart          |\n");
        printf("|  2. Remove from Cart     |\n");
        printf("|  3. Modify Quantity      |\n");
        printf("|  4. Return               |\n");
        printf("---------------------------\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter the item ID and quantity to add: ");
                scanf("%d %d", &item_id, &quantity);
                // 添加商品到购物车
                addToCart(item_id, quantity);
                break;
            case 2:
                printf("Enter the item ID to remove: ");
                scanf("%d", &item_id);
                // 从购物车移除商品
                removeFromCart(item_id);
                break;
            case 3:
                printf("Enter the item ID and new quantity to modify: ");
                scanf("%d %d", &item_id, &quantity);
                // 修改购物车中商品数量
                modifyCartQuantity(item_id, quantity);
                break;
            case 4:
                printf("Returning to the main menu\n");
                break;
            default:
                printf("Invalid selection, please try again\n");
        }
    } while (choice != 4);
}

void viewCart() {
    if (cart_size == 0) {
        printf("Shopping cart is empty.\n");
    } else {
        printf("Items in the shopping cart:\n");
        for (int i = 0; i < cart_size; i++) {
            for (int j = 0; j < num_products; j++) {
                if (cart[i].product_id == products[j].id) {
                    printf("Product ID: %d, Name: %s, Quantity: %d\n",
                           products[j].id, products[j].name, cart[i].quantity);
                    break;
                }
            }
        }
    }
}

void checkout() {
    float total_cost = 0;
    printf("Your order:\n");
    for (int i = 0; i < cart_size; i++) {
        for (int j = 0; j < num_products; j++) {
            if (cart[i].product_id == products[j].id) {
                float cost = products[j].selling_price * cart[i].quantity;
                total_cost += cost;
                printf("Product ID: %d, Name: %s, Quantity: %d, Cost: %.2f\n",
                       products[j].id, products[j].name, cart[i].quantity, cost);
                // 减少库存
                products[j].quantity -= cart[i].quantity;
                break;
            }
        }
    }
    printf("Total cost: %.2f\n", total_cost);
    printf("Thank you for shopping with us!\n");

    // 清空购物车
    cart_size = 0;
}

// 检查购物车中是否已有商品项
int cartContainsProduct(int product_id) {
    for (int i = 0; i < cart_size; i++) {
        if (cart[i].product_id == product_id) {
            return i; // 返回商品在购物车中的索引
        }
    }
    return -1; // 如果没有找到商品,返回-1
}

// 添加商品到购物车
void addToCart(int product_id, int quantity) {
    int index = cartContainsProduct(product_id);
    if (index != -1) {
        // 如果购物车中已有该商品,增加其数量
        cart[index].quantity += quantity;
    } else {
        // 如果购物车中没有该商品,添加新项
        if (cart_size < 100) { // 确保购物车不会超过最大容量
            cart[cart_size].product_id = product_id;
            cart[cart_size].quantity = quantity;
            cart_size++;
        }
    }
}

// 从购物车移除商品
void removeFromCart(int product_id) {
    int index = cartContainsProduct(product_id);
    if (index != -1) {
        // 如果找到商品,从购物车中移除
        for (int i = index; i < cart_size - 1; i++) {
            cart[i] = cart[i + 1];
        }
        cart_size--;
    }
}

// 修改购物车中商品的数量
void modifyCartQuantity(int product_id, int new_quantity) {
    int index = cartContainsProduct(product_id);
    if (index != -1) {
        // 如果找到商品,修改其数量
        cart[index].quantity = new_quantity;
    }
}

嗨,我是[LucianaiB](https://lucianaib.blog.csdn.net/ “LucianaiB”)。如果你觉得我的分享有价值,不妨通过以下方式表达你的支持:👍 点赞来表达你的喜爱,📁 关注以获取我的最新消息,💬 评论与我交流你的见解。我会继续努力,为你带来更多精彩和实用的内容。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-01-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 系统功能概述
  • 系统设计
  • 系统运行流程
  • 总结
    • 代码实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档