如何用C语言代码实现商品管理系统开发

发布时间:2022-08-03 17:54:43 作者:iii
来源:亿速云 阅读:264

如何用C语言代码实现商品管理系统开发

目录

  1. 引言
  2. 需求分析
  3. 系统设计
  4. 代码实现
  5. 系统测试
  6. 总结与展望
  7. 附录

引言

商品管理系统是现代商业活动中不可或缺的一部分,它能够帮助企业高效地管理商品信息、库存、销售等关键业务。本文将详细介绍如何使用C语言开发一个简单的商品管理系统。通过本系统,用户可以方便地进行商品信息的录入、查询、修改、删除等操作,同时还能管理库存和销售记录。

需求分析

在开发商品管理系统之前,首先需要进行详细的需求分析,明确系统需要实现的功能和用户的需求。以下是本系统的主要功能需求:

  1. 商品信息管理:包括商品的添加、删除、修改、查询等功能。
  2. 库存管理:能够实时更新商品的库存数量,支持库存预警功能。
  3. 销售管理:记录商品的销售信息,生成销售报表。
  4. 用户管理:支持多用户登录,不同用户有不同的权限。
  5. 数据持久化:系统数据能够保存到文件中,并在启动时加载。

系统设计

3.1 系统架构

本系统采用模块化设计,主要分为以下几个模块:

3.2 数据结构设计

为了高效地管理商品信息,我们需要设计合适的数据结构。以下是本系统中使用的主要数据结构:

typedef struct {
    int id;             // 商品ID
    char name[50];      // 商品名称
    float price;        // 商品价格
    int stock;          // 商品库存
} Product;

typedef struct {
    int id;             // 销售记录ID
    int product_id;     // 商品ID
    int quantity;       // 销售数量
    float total_price;  // 总价
    char date[20];      // 销售日期
} SaleRecord;

typedef struct {
    char username[20];  // 用户名
    char password[20];  // 密码
    int role;           // 用户角色(0: 管理员, 1: 普通用户)
} User;

3.3 功能模块设计

3.3.1 商品信息管理模块

3.3.2 库存管理模块

3.3.3 销售管理模块

3.3.4 用户管理模块

3.3.5 数据持久化模块

代码实现

4.1 商品信息管理模块

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_PRODUCTS 100

typedef struct {
    int id;
    char name[50];
    float price;
    int stock;
} Product;

Product products[MAX_PRODUCTS];
int product_count = 0;

void add_product() {
    if (product_count >= MAX_PRODUCTS) {
        printf("商品数量已达上限,无法添加新商品。\n");
        return;
    }

    Product new_product;
    new_product.id = product_count + 1;
    printf("请输入商品名称: ");
    scanf("%s", new_product.name);
    printf("请输入商品价格: ");
    scanf("%f", &new_product.price);
    printf("请输入商品库存: ");
    scanf("%d", &new_product.stock);

    products[product_count++] = new_product;
    printf("商品添加成功!\n");
}

void delete_product() {
    int id;
    printf("请输入要删除的商品ID: ");
    scanf("%d", &id);

    int index = -1;
    for (int i = 0; i < product_count; i++) {
        if (products[i].id == id) {
            index = i;
            break;
        }
    }

    if (index == -1) {
        printf("未找到该商品。\n");
        return;
    }

    for (int i = index; i < product_count - 1; i++) {
        products[i] = products[i + 1];
    }

    product_count--;
    printf("商品删除成功!\n");
}

void modify_product() {
    int id;
    printf("请输入要修改的商品ID: ");
    scanf("%d", &id);

    int index = -1;
    for (int i = 0; i < product_count; i++) {
        if (products[i].id == id) {
            index = i;
            break;
        }
    }

    if (index == -1) {
        printf("未找到该商品。\n");
        return;
    }

    printf("请输入新的商品名称: ");
    scanf("%s", products[index].name);
    printf("请输入新的商品价格: ");
    scanf("%f", &products[index].price);
    printf("请输入新的商品库存: ");
    scanf("%d", &products[index].stock);

    printf("商品修改成功!\n");
}

void query_product() {
    char name[50];
    printf("请输入要查询的商品名称: ");
    scanf("%s", name);

    for (int i = 0; i < product_count; i++) {
        if (strcmp(products[i].name, name) == 0) {
            printf("商品ID: %d\n", products[i].id);
            printf("商品名称: %s\n", products[i].name);
            printf("商品价格: %.2f\n", products[i].price);
            printf("商品库存: %d\n", products[i].stock);
            return;
        }
    }

    printf("未找到该商品。\n");
}

4.2 库存管理模块

void update_stock() {
    int id, quantity;
    printf("请输入要更新库存的商品ID: ");
    scanf("%d", &id);
    printf("请输入库存变化量: ");
    scanf("%d", &quantity);

    int index = -1;
    for (int i = 0; i < product_count; i++) {
        if (products[i].id == id) {
            index = i;
            break;
        }
    }

    if (index == -1) {
        printf("未找到该商品。\n");
        return;
    }

    products[index].stock += quantity;
    printf("库存更新成功!\n");
}

void stock_alert() {
    int threshold;
    printf("请输入库存预警阈值: ");
    scanf("%d", &threshold);

    for (int i = 0; i < product_count; i++) {
        if (products[i].stock < threshold) {
            printf("商品ID: %d, 商品名称: %s, 当前库存: %d\n", products[i].id, products[i].name, products[i].stock);
        }
    }
}

4.3 销售管理模块

#define MAX_SALES 100

typedef struct {
    int id;
    int product_id;
    int quantity;
    float total_price;
    char date[20];
} SaleRecord;

SaleRecord sales[MAX_SALES];
int sale_count = 0;

void add_sale_record() {
    if (sale_count >= MAX_SALES) {
        printf("销售记录数量已达上限,无法添加新记录。\n");
        return;
    }

    SaleRecord new_sale;
    new_sale.id = sale_count + 1;
    printf("请输入商品ID: ");
    scanf("%d", &new_sale.product_id);
    printf("请输入销售数量: ");
    scanf("%d", &new_sale.quantity);
    printf("请输入销售日期: ");
    scanf("%s", new_sale.date);

    int index = -1;
    for (int i = 0; i < product_count; i++) {
        if (products[i].id == new_sale.product_id) {
            index = i;
            break;
        }
    }

    if (index == -1) {
        printf("未找到该商品。\n");
        return;
    }

    if (products[index].stock < new_sale.quantity) {
        printf("库存不足,无法完成销售。\n");
        return;
    }

    new_sale.total_price = products[index].price * new_sale.quantity;
    products[index].stock -= new_sale.quantity;
    sales[sale_count++] = new_sale;
    printf("销售记录添加成功!\n");
}

void generate_sales_report() {
    printf("销售报表:\n");
    for (int i = 0; i < sale_count; i++) {
        printf("销售记录ID: %d, 商品ID: %d, 销售数量: %d, 总价: %.2f, 销售日期: %s\n",
               sales[i].id, sales[i].product_id, sales[i].quantity, sales[i].total_price, sales[i].date);
    }
}

4.4 用户管理模块

#define MAX_USERS 10

typedef struct {
    char username[20];
    char password[20];
    int role; // 0: 管理员, 1: 普通用户
} User;

User users[MAX_USERS];
int user_count = 0;

int login() {
    char username[20], password[20];
    printf("请输入用户名: ");
    scanf("%s", username);
    printf("请输入密码: ");
    scanf("%s", password);

    for (int i = 0; i < user_count; i++) {
        if (strcmp(users[i].username, username) == 0 && strcmp(users[i].password, password) == 0) {
            return users[i].role;
        }
    }

    printf("用户名或密码错误。\n");
    return -1;
}

void add_user() {
    if (user_count >= MAX_USERS) {
        printf("用户数量已达上限,无法添加新用户。\n");
        return;
    }

    User new_user;
    printf("请输入用户名: ");
    scanf("%s", new_user.username);
    printf("请输入密码: ");
    scanf("%s", new_user.password);
    printf("请输入用户角色 (0: 管理员, 1: 普通用户): ");
    scanf("%d", &new_user.role);

    users[user_count++] = new_user;
    printf("用户添加成功!\n");
}

4.5 数据持久化模块

void save_data() {
    FILE *file = fopen("products.dat", "wb");
    if (file == NULL) {
        printf("无法打开文件。\n");
        return;
    }

    fwrite(&product_count, sizeof(int), 1, file);
    fwrite(products, sizeof(Product), product_count, file);

    fwrite(&sale_count, sizeof(int), 1, file);
    fwrite(sales, sizeof(SaleRecord), sale_count, file);

    fwrite(&user_count, sizeof(int), 1, file);
    fwrite(users, sizeof(User), user_count, file);

    fclose(file);
    printf("数据保存成功!\n");
}

void load_data() {
    FILE *file = fopen("products.dat", "rb");
    if (file == NULL) {
        printf("无法打开文件。\n");
        return;
    }

    fread(&product_count, sizeof(int), 1, file);
    fread(products, sizeof(Product), product_count, file);

    fread(&sale_count, sizeof(int), 1, file);
    fread(sales, sizeof(SaleRecord), sale_count, file);

    fread(&user_count, sizeof(int), 1, file);
    fread(users, sizeof(User), user_count, file);

    fclose(file);
    printf("数据加载成功!\n");
}

系统测试

在完成代码实现后,我们需要对系统进行全面的测试,以确保各个功能模块能够正常工作。以下是测试的主要步骤:

  1. 商品信息管理测试:测试商品的添加、删除、修改、查询功能。
  2. 库存管理测试:测试库存的更新和预警功能。
  3. 销售管理测试:测试销售记录的添加和报表生成功能。
  4. 用户管理测试:测试用户的登录和权限管理功能。
  5. 数据持久化测试:测试数据的保存和加载功能。

总结与展望

通过本文的介绍,我们详细讲解了如何使用C语言开发一个简单的商品管理系统。该系统涵盖了商品信息管理、库存管理、销售管理、用户管理和数据持久化等核心功能。尽管该系统功能较为基础,但它为后续的功能扩展和优化提供了良好的基础。

未来,我们可以考虑以下改进方向:

  1. 图形用户界面(GUI):为系统添加图形用户界面,提升用户体验。
  2. 数据库支持:使用数据库替代文件存储,提高数据管理的效率和安全性。
  3. 多用户并发支持:支持多用户并发操作,提升系统的并发处理能力。
  4. 报表生成优化:提供更丰富的报表生成功能,支持数据分析和决策支持。

附录

7.1 完整代码

”`c #include #include #include

#define MAX_PRODUCTS 100 #define MAX_SALES 100 #define MAX_USERS 10

typedef struct { int id; char name[50]; float price; int stock; } Product;

typedef struct { int id; int product_id; int quantity; float total_price; char date[20]; } SaleRecord;

typedef struct { char username[20]; char password[20]; int role; // 0: 管理员, 1: 普通用户 } User;

Product products[MAX_PRODUCTS]; int product_count = 0;

SaleRecord sales[MAX_SALES]; int sale_count = 0;

User users[MAX_USERS]; int user_count = 0;

void add_product() { if (product_count >= MAX_PRODUCTS) { printf(“商品数量已达上限,无法添加新商品。\n”); return; }

Product new_product;
new_product.id = product_count + 1;
printf("请输入商品名称: ");
scanf("%s", new_product.name);
printf("请输入商品价格: ");
scanf("%f", &new_product.price);
printf("请输入商品库存: ");
scanf("%d", &new_product.stock);

products[product_count++] = new_product;
printf("商品添加成功!\n");

}

void delete_product() { int id; printf(“请输入要删除的商品ID: “); scanf(”%d”, &id);

int index = -1;
for (int i = 0; i < product_count; i++) {
    if (products[i].id == id) {
        index = i;
        break;
    }
}

if (index == -1) {
    printf("未找到该商品。\n");
    return;
}

for (int i = index; i < product_count - 1; i++) {
    products[i] = products[i + 1];
}

product_count--;
printf("商品删除成功!\n");

}

void modify_product() { int id; printf(“请输入要修改的商品ID: “); scanf(”%d”, &id);

int index = -1;
for (int i = 0; i < product_count; i++) {
    if (products[i].id == id) {
        index = i;
        break;
    }
}

if (index == -1) {
    printf("未找到该商品。\n");
    return;
}

printf("请输入新的商品名称: ");
scanf("%s", products[index].name);
printf("请输入新的商品价格: ");
scanf("%f", &products[index].price);
printf("请输入新的商品库存: ");
scanf("%d", &products[index].stock);

printf("商品修改成功!\n");

}

void query_product() { char name[50]; printf(“请输入要查询的商品名称: “); scanf(”%s”, name);

for (int i = 0; i < product_count; i++) {
    if (strcmp(products[i].name, name) == 0) {
        printf("商品ID: %d\n", products[i].id);
        printf("商品名称: %s\n", products[i].name);
        printf("商品价格: %.2f\n", products[i].price);
        printf("商品库存: %d\n", products[i].stock);
        return;
    }
}

printf("未找到该商品。\n");

}

void update_stock() { int id, quantity; printf(“请输入要更新库存的商品ID: “); scanf(”%d”, &id); printf(“请输入库存变化量: “); scanf(”%d”, &quantity);

int index = -1;
for (int i = 0; i < product_count; i++) {
    if (products[i].id == id) {
推荐阅读:
  1. 如何用C语言代码实现扫雷游戏
  2. 如何用Java代码实现图书管理系统

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c语言

上一篇:如何利用vue+elementUI设置省市县三级联动下拉列表框

下一篇:Python执行时间计算方法及优化实例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》