0802-0803 category/product/[order]/orderitem/[user]

本文介绍了一个网上商城的基本功能实现,包括主页的热销商品、最新商品展示,导航栏分类选择,以及购买、购物车、商品详情、下订单等操作。后台管理功能也得到了阐述。内容涉及SQL查询、分页以及使用了如servlet、jsp等技术。通过导入相关jar包并配置文件夹,实现了数据访问和业务逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网上商城
   基本功能
        主页 -- 热销商品,最新商品 ,导航栏(分类)
          热销商品 : select * from product where is_hot = 1
          最新商品:  select top 20 * from product order by pdate desc
         导航栏(分类):  select * from product where cid=?

        购买 ,购物车,商品详情(展示商品详细的信息)
        下订单
        后台管理
        分页
           

      主页  手机数码   电脑办公
           

 导入jar包(sqljdbc/jstl/standard)  图片所在文件夹products放入WebContent

商品类型表
CREATE TABLE category (
  cid varchar(32) primary key, --类型编号
  cname varchar(20) DEFAULT NULL--类型名称
)

INSERT INTO  category VALUES ('1','手机数码'),('2','电脑办公');
select * from category


CREATE TABLE product (
   pid varchar(32) primary key,--商品编号
   pname varchar(50) DEFAULT NULL,--商品名称
   market_price float DEFAULT NULL,--市场价
   shop_price float DEFAULT NULL,--商品价格
   pimage varchar(200) DEFAULT NULL,--商品图片(图片路径)
   pdate date DEFAULT NULL,--上架时间
   is_hot int DEFAULT NULL, --是否是热销商品 1:是   0:否
   pdesc varchar(255) DEFAULT NULL,--商品描述
   pflag int DEFAULT NULL,--是否下架  1:是    0:否
   cid varchar(32) DEFAULT NULL references category(cid)
   --商品类型
)

INSERT INTO product VALUES ('1','小米 4c 标准版',1399,1299,'products/1/c_0001.jpg','2015-11-02',1,'小米 4c 标准版 全网通 白色 移动联通电信4G手机 双卡双待',0,'1'),
('10','华为 Ascend Mate7',2699,2599,'products/1/c_0010.jpg','2015-11-02',1,'华为 Ascend Mate7 月光银 移动4G手机 双卡双待双通6英寸高清大屏,纤薄机身,智能超八核,按压式指纹识别!!选择下方“移动老用户4G飞享合约”,无需换号,还有话费每月返还!',0,'1'),
('11','vivo X5Pro',2399,2298,'products/1/c_0014.jpg','2015-11-02',1,'移动联通双4G手机 3G运存版 极光白【购机送蓝牙耳机+蓝牙自拍杆】新升级3G运行内存·双2.5D弧面玻璃·眼球识别技术',0,'1'),
('12','努比亚(nubia)My 布拉格',1899,1799,'products/1/c_0013.jpg','2015-11-02',0,'努比亚(nubia)My 布拉格 银白 移动联通4G手机 双卡双待【嗨11,下单立减100】金属机身,快速充电!布拉格相机全新体验!',0,'1'),
('13','华为 麦芒4',2599,2499,'products/1/c_0012.jpg','2015-11-02',1,'华为 麦芒4 晨曦金 全网通版4G手机 双卡双待金属机身 2.5D弧面屏 指纹解锁 光学防抖',0,'1'),
('14','vivo X5M',1899,1799,'products/1/c_0011.jpg','2015-11-02',0,'vivo X5M 移动4G手机 双卡双待 香槟金【购机送蓝牙耳机+蓝牙自拍杆】5.0英寸大屏显示·八核双卡双待·Hi-Fi移动KTV',0,'1'),
('15','Apple iPhone 6 (A1586)',4399,4288,'products/1/c_0015.jpg','2015-11-02',1,'Apple iPhone 6 (A1586) 16GB 金色 移动联通电信4G手机长期省才是真的省!点击购机送费版,月月送话费,月月享优惠,畅享4G网络,就在联通4G!',0,'1'),
('16','华为 HUAWEI Mate S 臻享版',4200,4087,'products/1/c_0016.jpg','2015-11-03',0,'华为 HUAWEI Mate S 臻享版 手机 极昼金 移动联通双4G(高配)满星评价即返30元话费啦;买就送电源+清水套+创意手机支架;优雅弧屏,mate7升级版',0,'1'),
('17','索尼(SONY) E6533 Z3+',4099,3999,'products/1/c_0017.jpg','2015-11-02',0,'索尼(SONY) E6533 Z3+ 双卡双4G手机 防水防尘 涧湖绿索尼z3专业防水 2070万像素 移动联通双4G',0,'1'),
('18','HTC One M9+',3599,3499,'products/1/c_0018.jpg','2015-11-02',0,'HTC One M9+(M9pw) 金银汇 移动联通双4G手机5.2英寸,8核CPU,指纹识别,UltraPixel超像素前置相机+2000万/200万后置双镜头相机!降价特卖,惊喜不断!',0,'1'),
('19','HTC Desire 826d 32G 臻珠白',1599,1469,'products/1/c_0020.jpg','2015-11-02',1,'后置1300万+UltraPixel超像素前置摄像头+【双】前置扬声器+5.5英寸【1080p】大屏!',0,'1'),
('2','中兴 AXON',2899,2699,'products/1/c_0002.jpg','2015-11-05',1,'中兴 AXON 天机 mini 压力屏版 B2015 华尔金 移动联通电信4G 双卡双待',0,'1'),
('20','小米 红米2A 增强版 白色',649,549,'products/1/c_0019.jpg','2015-11-02',0,'新增至2GB 内存+16GB容量!4G双卡双待,联芯 4 核 1.5GHz 处理器!',0,'1'),
('21','魅族 魅蓝note2 16GB 白色',1099,999,'products/1/c_0021.jpg','2015-11-02',0,'现货速抢,抢完即止!5.5英寸1080P分辨率屏幕,64位八核1.3GHz处理器,1300万像素摄像头,双色温双闪光灯!',0,'1'),
('22','三星 Galaxy S5 (G9008W) 闪耀白',2099,1999,'products/1/c_0022.jpg','2015-11-02',1,'5.1英寸全高清炫丽屏,2.5GHz四核处理器,1600万像素',0,'1'),
('23','sonim XP7700 4G手机',1799,1699,'products/1/c_0023.jpg','2015-11-09',1,'三防智能手机 移动/联通双4G 安全 黑黄色 双4G美国军工IP69 30天长待机 3米防水防摔 北斗',0,'1'),
('24','努比亚(nubia)Z9精英版 金色',3988,3888,'products/1/c_0024.jpg','2015-11-02',1,'移动联通电信4G手机 双卡双待真正的无边框!金色尊贵版!4GB+64GB大内存!',0,'1'),
('25','Apple iPhone 6 Plus (A1524) 16GB 金色',5188,4988,'products/1/c_0025.jpg','2015-11-02',0,'Apple iPhone 6 Plus (A1524) 16GB 金色 移动联通电信4G手机 硬货 硬实力',0,'1'),
('26','Apple iPhone 6s (A1700) 64G 玫瑰金色',6388,6088,'products/1/c_0026.jpg','2015-11-02',0,'Apple iPhone 6 Plus (A1524) 16GB 金色 移动联通电信4G手机 硬货 硬实力',0,'1'),
('27','三星 Galaxy Note5(N9200)32G版',5588,5388,'products/1/c_0027.jpg','2015-11-02',0,'旗舰机型!5.7英寸大屏,4+32G内存!不一样的SPen更优化的浮窗指令!赠无线充电板!',0,'1'),
('28','三星 Galaxy S6 Edge+(G9280)32G版 铂光金',5999,5888,'products/1/c_0028.jpg','2015-11-02',0,'赠移动电源+自拍杆+三星OTG金属U盘+无线充电器+透明保护壳',0,'1'),
('29','LG G4(H818)陶瓷白 国际版',3018,2978,'products/1/c_0029.jpg','2015-11-02',0,'李敏镐代言,F1.8大光圈1600万后置摄像头,5.5英寸2K屏,3G+32G内存,LG年度旗舰机!',0,'1'),
('3','华为荣耀6',1599,1499,'products/1/c_0003.jpg','2015-11-02',0,'荣耀 6 (H60-L01) 3GB内存标准版 黑色 移动4G手机',0,'1'),
('30','微软(Microsoft) Lumia 640 LTE DS (RM-1113)',1099,999,'products/1/c_0030.jpg','2015-11-02',0,'微软首款双网双卡双4G手机,5.0英寸高清大屏,双网双卡双4G!',0,'1'),
('31','宏碁(acer)ATC705-N50 台式电脑',2399,2299,'products/1/c_0031.jpg','2015-11-02',0,'爆款直降,满千减百,品质宏碁,特惠来袭,何必苦等11.11,早买早便宜!',0,'2'),
('32','Apple MacBook Air MJVE2CH/A 13.3英寸',6788,6688,'products/1/c_0032.jpg','2015-11-02',0,'宽屏笔记本电脑 128GB 闪存',0,'2'),
'33','联想(ThinkPad) 轻薄系列E450C(20EH0001CD)',4399,4199,'products/1/c_0033.jpg','2015-11-02',0,'联想(ThinkPad) 轻薄系列E450C(20EH0001CD)14英寸笔记本电脑(i5-4210U 4G 500G 2G独显 Win8.1)',0,'2'),
('34','联想(Lenovo)小新V3000经典版',4599,4499,'products/1/c_0034.jpg','2015-11-02',0,'14英寸超薄笔记本电脑(i7-5500U 4G 500G+8G SSHD 2G独显 全高清屏)黑色满1000減100,狂减!火力全开,横扫3天!',0,'2'),
('35','华硕(ASUS)经典系列R557LI',3799,3699,'products/1/c_0035.jpg','2015-11-02',0,'15.6英寸笔记本电脑(i5-5200U 4G 7200转500G 2G独显 D刻 蓝牙 Win8.1 黑色)',0,'2'),
('36','华硕(ASUS)X450J',4599,4399,'products/1/c_0036.jpg','2015-11-02',0,'14英寸笔记本电脑 (i5-4200H 4G 1TB GT940M 2G独显 蓝牙4.0 D刻 Win8.1 黑色)',0,'2'),
('37','戴尔(DELL)灵越 飞匣3000系列',3399,3299,'products/1/c_0037.jpg','2015-11-03',0,' Ins14C-4528B 14英寸笔记本(i5-5200U 4G 500G GT820M 2G独显 Win8)黑',0,'2'),
('38','惠普(HP)WASD 暗影精灵',5699,5499,'products/1/c_0038.jpg','2015-11-02',0,'15.6英寸游戏笔记本电脑(i5-6300HQ 4G 1TB+128G SSD GTX950M 4G独显 Win10)',0,'2'),
('39','Apple 配备 Retina 显示屏的 MacBook',11299,10288,'products/1/c_0039.jpg','2015-11-02',0,'Pro MF840CH/A 13.3英寸宽屏笔记本电脑 256GB 闪存',0,'2'),
('4','联想 P1',2199,1999,'products/1/c_0004.jpg','2015-11-02',0,'联想 P1 16G 伯爵金 移动联通4G手机充电5分钟,通话3小时!科技源于超越!品质源于沉淀!5000mAh大电池!高端商务佳配!',0,'1'),
('40','机械革命(MECHREVO)MR X6S-M',6799,6599,'products/1/c_0040.jpg','2015-11-02',0,'15.6英寸游戏本(I7-4710MQ 8G 64GSSD+1T GTX960M 2G独显 IPS屏 WIN7)黑色',0,'2'),
('41','神舟(HASEE) 战神K660D-i7D2',5699,5499,'products/1/c_0041.jpg','2015-11-02',0,'15.6英寸游戏本(i7-4710MQ 8G 1TB GTX960M 2G独显 1080P)黑色',0,'2'),
('42','微星(MSI)GE62 2QC-264XCN',6199,5999,'products/1/c_0042.jpg','2015-11-02',0,'15.6英寸游戏笔记本电脑(i5-4210H 8G 1T GTX960MG DDR5 2G 背光键盘)黑色',0,'2'),
('43','雷神(ThundeRobot)G150S',5699,5499,'products/1/c_0043.jpg','2015-11-02',0,'15.6英寸游戏本 ( i7-4710MQ 4G 500G GTX950M 2G独显 包无亮点全高清屏) 金',0,'2'),
('44','惠普(HP)轻薄系列 HP',3199,3099,'products/1/c_0044.jpg','2015-11-02',0,'15-r239TX 15.6英寸笔记本电脑(i5-5200U 4G 500G GT820M 2G独显 win8.1)金属灰',0,'2'),
('45','未来人类(Terrans Force)T5',10999,9899,'products/1/c_0045.jpg','2015-11-02',0,'15.6英寸游戏本(i7-5700HQ 16G 120G固态+1TB GTX970M 3G GDDR5独显)黑',0,'2'),
('46','戴尔(DELL)Vostro 3800-R6308 台式电脑',3299,3199,'products/1/c_0046.jpg','2015-11-02',0,'(i3-4170 4G 500G DVD 三年上门服务 Win7)黑',0,'2'),(
'47','联想(Lenovo)H3050 台式电脑',5099,4899,'products/1/c_0047.jpg','2015-11-11',0,'(i5-4460 4G 500G GT720 1G独显 DVD 千兆网卡 Win10)23英寸',0,'2'),
('48','Apple iPad mini 2 ME279CH/A',2088,1888,'products/1/c_0048.jpg','2015-11-02',0,'(配备 Retina 显示屏 7.9英寸 16G WLAN 机型 银色)',0,'2'),
('49','小米(MI)7.9英寸平板',1399,1299,'products/1/c_0049.jpg','2015-11-02',0,'WIFI 64GB(NVIDIA Tegra K1 2.2GHz 2G 64G 2048*1536视网膜屏 800W)白色',0,'2'),
('5','摩托罗拉 moto x(x+1)',1799,1699,'products/1/c_0005.jpg','2015-11-01',0,'摩托罗拉 moto x(x+1)(XT1085) 32GB 天然竹 全网通4G手机11月11天!MOTO X震撼特惠来袭!1699元!带你玩转黑科技!天然材质,原生流畅系统!',0,'1'),
('50','Apple iPad Air 2 MGLW2CH/A',2399,2299,'products/1/c_0050.jpg','2015-11-12',0,'(9.7英寸 16G WLAN 机型 银色)',0,'2'),
('6','魅族 MX5 16GB 银黑色',1899,1799,'products/1/c_0006.jpg','2015-11-02',0,'魅族 MX5 16GB 银黑色 移动联通双4G手机 双卡双待送原厂钢化膜+保护壳+耳机!5.5英寸大屏幕,3G运行内存,2070万+500万像素摄像头!长期省才是真的省!',0,'1'),
('7','三星 Galaxy On7',1499,1398,'products/1/c_0007.jpg','2015-11-14',0,'三星 Galaxy On7(G6000)昂小七 金色 全网通4G手机 双卡双待新品火爆抢购中!京东尊享千元良机!5.5英寸高清大屏!1300+500W像素!评价赢30元话费券!',0,'1'),
('8','NUU NU5',1288,1190,'products/1/c_0008.jpg','2015-11-02',0,'NUU NU5 16GB 移动联通双4G智能手机 双卡双待 晒单有礼 晨光金香港品牌 2.5D弧度前后钢化玻璃 随机附赠手机套+钢化贴膜 晒单送移动电源+蓝牙耳机',0,'1'),
('9','乐视(Letv)乐1pro(X800)',2399,2299,'products/1/c_0009.jpg','2015-11-02',0,'乐视(Letv)乐1pro(X800)64GB 金色 移动联通4G手机 双卡双待乐视生态UI+5.5英寸2K屏+高通8核处理器+4GB运行内存+64GB存储+1300万摄像头!',0,'1');
/*!40000 ALTER TABLE `product` ENABLE KEYS */;
select * from product


订单表
CREATE TABLE [order](
  oid int primary key identity,--订单编号
  ordertime datetime DEFAULT NULL,--下单时间
  total float,--订单总价格
  state int DEFAULT NULL, -- 0 下单  1 closed
  uid int references [user](uid) --外键引用用户编号
)

select * from [user]

订单明细 订单项
CREATE TABLE orderitem (
   itemid  int primary key identity,--编号
   pid varchar(32)  references product(pid), --外键,商品编号
   num int , --商品数量
   subtotal float, --小计
   oid  int  references [order](oid) --外键,订单编号
) 

create table [user](
       uid int primary key  identity NOT NULL,
	  username varchar(20) DEFAULT NULL,
	  password varchar(20) DEFAULT NULL,
	  name varchar(20) DEFAULT NULL,
	  email varchar(30) DEFAULT NULL,
	  telephone varchar(20) DEFAULT NULL,
	  birthday date DEFAULT NULL,
	  sex varchar(10) DEFAULT NULL
)

insert into [User] values('zs','123','张芳','zff123@163.com','18274834196','1992-10-10','女'),
('ls','456','李天星','lss456@163.com','18274834197','1988-10-10','男')
select * from [user]
select * from [order]




web

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="h
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #define MAX_PRODUCTS 1000 #define MAX_USERS 500 #define MAX_ORDERS 5000 #define MAX_NAME_LENGTH 50 #define MAX_ADDRESS_LENGTH 100 #define MAX_CATEGORY_LENGTH 30 #define RESTOCK_THRESHOLD 10 #define CLEANUP_DAYS 30 // 商品信息结构体 typedef struct { int id; // 商品ID char name[MAX_NAME_LENGTH]; // 商品名称 char category[MAX_CATEGORY_LENGTH]; // 商品类别 float price; // 商品价格 int stock; // 库存数量 int sales; // 销售数量统计 } Product; // 用户信息结构体 typedef struct { int id; // 用户ID char name[MAX_NAME_LENGTH]; // 用户姓名 char address[MAX_ADDRESS_LENGTH]; // 用户地址 char contact[MAX_NAME_LENGTH]; // 联系方式 } User; // 订单状态枚举 typedef enum { PENDING_PAYMENT, // 待付款 PAID, // 已付款 SHIPPED, // 已发货 COMPLETED // 已完成 } OrderStatus; // 订单信息结构体 typedef struct { int id; // 订单ID int user_id; // 用户ID int product_id; // 商品ID int quantity; // 商品数量 time_t order_time; // 下单时间 OrderStatus status; // 订单状态 } Order; // 补货任务结构体 typedef struct { int product_id; // 商品ID int priority; // 优先级(1-高优先级, 2-中优先级, 3-低优先级) } RestockTask; // 商品类别树节点 typedef struct CategoryNode { char name[MAX_CATEGORY_LENGTH]; // 类别名称 struct CategoryNode *parent; // 父类别 struct CategoryNode *children[MAX_PRODUCTS]; // 子类别 int child_count; // 子类别数量 int product_count; // 该类别下商品数量 float sales_total; // 该类别销售总额 } CategoryNode; // 全局数据存储 Product products[MAX_PRODUCTS]; // 商品数组 int product_count = 0; // 商品数量 User users[MAX_USERS]; // 用户数组 int user_count = 0; // 用户数量 Order orders[MAX_ORDERS]; // 订单数组 int order_count = 0; // 订单数量 RestockTask restock_queue[MAX_PRODUCTS]; // 补货任务队列 int restock_count = 0; // 补货任务数量 // 栈结构用于订单状态变更历史 typedef struct { int order_id; // 订单ID OrderStatus old_status; // 原状态 time_t timestamp; // 变更时间 } OrderHistoryItem; OrderHistoryItem order_history[MAX_ORDERS]; // 订单历史记录 int history_count = 0; // 历史记录数量 // 哈希表结构用于用户信息快速查找 typedef struct HashNode { int key; // 键(用户ID) User *user; // 用户指针 struct HashNode *next; // 链表指针 } HashNode; HashNode *user_hash_table[100]; // 用户哈希表 // 文件路径定义 const char *PRODUCTS_FILE = "D:\\c\\text\\products.csv"; const char *USERS_FILE = "D:\\c\\text\\users.csv"; const char *ORDERS_FILE = "D:\\c\\text\\orders.csv"; // 函数声明 void initialize_system(); // 初始化系统 void initialize_hash_table(); // 初始化哈希表 int add_product(int id, const char *name, const char *category, float price, int stock); // 添加商品 int find_product_by_id(int id); // 根据ID查找商品 int find_product_by_name(const char *name); // 根据名称查找商品 void sort_products_by_name(); // 按名称排序商品 void print_product(int index); // 打印商品信息 int add_user(int id, const char *name, const char *address, const char *contact); // 添加用户 User* find_user_by_id(int id); // 根据ID查找用户 int find_user_by_name(const char *name); // 根据名称查找用户 void print_user(int index); // 打印用户信息 int create_order(int user_id, int product_id, int quantity); // 创建订单 int process_payment(int order_id); // 处理付款 int process_shipment(int order_id); // 处理发货 int confirm_receipt(int order_id); // 确认收货 Order* find_order_by_id(int id); // 根据ID查找订单 void sort_orders_by_time(); // 按时间排序订单 void print_order(int index); // 打印订单信息 int update_stock(int product_id, int quantity); // 更新库存 void check_low_stock(); // 检查低库存 void process_restock_tasks(); // 处理补货任务 void push_order_history(int order_id, OrderStatus old_status); // 记录订单历史 OrderHistoryItem pop_order_history(); // 获取订单历史 int has_order_history(int order_id); // 检查订单是否有历史记录 void enqueue_restock_task(int product_id, int priority); // 添加补货任务 RestockTask dequeue_restock_task(); // 获取补货任务 int is_restock_queue_empty(); // 检查补货队列是否为空 CategoryNode* create_category_node(const char *name); // 创建类别节点 void add_child_category(CategoryNode *parent, CategoryNode *child); // 添加子类别 void traverse_category_tree(CategoryNode *node, int depth); // 遍历类别树 void update_category_stats(CategoryNode *node, Product *product, float sales_amount); // 更新类别统计 unsigned int hash_function(int key); // 哈希函数 void hash_table_insert(int key, User *user); // 哈希表插入 User* hash_table_lookup(int key); // 哈希表查找 void print_system_status(); // 打印系统状态 void cleanup_old_orders(); // 清理旧订单 void show_menu(); // 显示菜单 void input_product(); // 输入商品信息 void list_products(); // 列出所有商品 void input_user(); // 输入用户信息 void list_users(); // 列出所有用户 void input_order(); // 输入订单信息 void list_orders(); // 列出所有订单 void search_product(); // 搜索商品 void search_user(); // 搜索用户 void search_order(); // 搜索订单 void wait_for_user(); // 等待用户输入 // 从文件加载商品数据 int load_products_from_file() { FILE *file = fopen(PRODUCTS_FILE, "r"); if (file == NULL) { printf("警告: 商品文件不存在,将使用空数据初始化\n"); return 0; } char line[512]; int count = 0; // 跳过标题行 if (fgets(line, sizeof(line), file) == NULL) { fclose(file); return 0; } while (fgets(line, sizeof(line), file) != NULL && product_count < MAX_PRODUCTS) { Product p; char *token; // 解析ID token = strtok(line, ","); if (token == NULL) continue; p.id = atoi(token); // 解析名称 token = strtok(NULL, ","); if (token == NULL) continue; strncpy(p.name, token, MAX_NAME_LENGTH-1); p.name[MAX_NAME_LENGTH-1] = '\0'; // 解析类别 token = strtok(NULL, ","); if (token == NULL) continue; strncpy(p.category, token, MAX_CATEGORY_LENGTH-1); p.category[MAX_CATEGORY_LENGTH-1] = '\0'; // 解析价格 token = strtok(NULL, ","); if (token == NULL) continue; p.price = atof(token); // 解析库存 token = strtok(NULL, ","); if (token == NULL) continue; p.stock = atoi(token); // 解析销量 token = strtok(NULL, ",\n"); if (token != NULL) { p.sales = atoi(token); } else { p.sales = 0; } // 添加商品 if (add_product(p.id, p.name, p.category, p.price, p.stock) != -1) { // 更新销量 products[product_count-1].sales = p.sales; count++; } } fclose(file); printf("从文件加载了 %d 个商品\n", count); return count; } // 从文件加载用户数据 int load_users_from_file() { FILE *file = fopen(USERS_FILE, "r"); if (file == NULL) { printf("警告: 用户文件不存在,将使用空数据初始化\n"); return 0; } char line[512]; int count = 0; // 跳过标题行 if (fgets(line, sizeof(line), file) == NULL) { fclose(file); return 0; } while (fgets(line, sizeof(line), file) != NULL && user_count < MAX_USERS) { User u; char *token; // 解析ID token = strtok(line, ","); if (token == NULL) continue; u.id = atoi(token); // 解析名称 token = strtok(NULL, ","); if (token == NULL) continue; strncpy(u.name, token, MAX_NAME_LENGTH-1); u.name[MAX_NAME_LENGTH-1] = '\0'; // 解析地址 token = strtok(NULL, ","); if (token == NULL) continue; strncpy(u.address, token, MAX_ADDRESS_LENGTH-1); u.address[MAX_ADDRESS_LENGTH-1] = '\0'; // 解析联系方式 token = strtok(NULL, ",\n"); if (token != NULL) { strncpy(u.contact, token, MAX_NAME_LENGTH-1); u.contact[MAX_NAME_LENGTH-1] = '\0'; } else { u.contact[0] = '\0'; } // 添加用户 if (add_user(u.id, u.name, u.address, u.contact) != -1) { count++; } } fclose(file); printf("从文件加载了 %d 个用户\n", count); return count; } // 从文件加载订单数据 int load_orders_from_file() { FILE *file = fopen(ORDERS_FILE, "r"); if (file == NULL) { printf("警告: 订单文件不存在,将使用空数据初始化\n"); return 0; } char line[512]; int count = 0; // 跳过标题行 if (fgets(line, sizeof(line), file) == NULL) { fclose(file); return 0; } while (fgets(line, sizeof(line), file) != NULL && order_count < MAX_ORDERS) { Order o; char *token; // 解析ID token = strtok(line, ","); if (token == NULL) continue; o.id = atoi(token); // 解析用户ID token = strtok(NULL, ","); if (token == NULL) continue; o.user_id = atoi(token); // 解析商品ID token = strtok(NULL, ","); if (token == NULL) continue; o.product_id = atoi(token); // 解析数量 token = strtok(NULL, ","); if (token == NULL) continue; o.quantity = atoi(token); // 解析下单时间 token = strtok(NULL, ","); if (token == NULL) continue; o.order_time = (time_t)atol(token); // 解析状态 token = strtok(NULL, ",\n"); if (token == NULL) continue; int status = atoi(token); if (status >= PENDING_PAYMENT && status <= COMPLETED) { o.status = (OrderStatus)status; } else { o.status = PENDING_PAYMENT; } // 检查用户和商品是否存在 User *user = find_user_by_id(o.user_id); if (user == NULL) { printf("警告: 订单 %d 的用户ID %d 不存在,跳过此订单\n", o.id, o.user_id); continue; } if (find_product_by_id(o.product_id) == -1) { printf("警告: 订单 %d 的商品ID %d 不存在,跳过此订单\n", o.id, o.product_id); continue; } // 添加订单(绕过库存检查,直接添加) orders[order_count] = o; order_count++; count++; } fclose(file); printf("从文件加载了 %d 个订单\n", count); return count; } // 添加商品 int add_product(int id, const char *name, const char *category, float price, int stock) { // 检查商品ID是否已存在 if (find_product_by_id(id) != -1) { printf("错误: 商品ID %d 已存在\n", id); return -1; } // 检查商品数量是否超过最大值 if (product_count >= MAX_PRODUCTS) { printf("错误: 商品数量已达到最大值\n"); return -1; } // 添加商品 products[product_count].id = id; strncpy(products[product_count].name, name, MAX_NAME_LENGTH-1); strncpy(products[product_count].category, category, MAX_CATEGORY_LENGTH-1); products[product_count].price = price; products[product_count].stock = stock; products[product_count].sales = 0; product_count++; return 0; } // 根据ID查找商品 int find_product_by_id(int id) { for (int i = 0; i < product_count; i++) { if (products[i].id == id) { return i; } } return -1; // 未找到 } // 根据名称查找商品 int find_product_by_name(const char *name) { for (int i = 0; i < product_count; i++) { if (strcmp(products[i].name, name) == 0) { return i; } } return -1; // 未找到 } // 按名称排序商品 void sort_products_by_name() { for (int i = 0; i < product_count - 1; i++) { for (int j = i + 1; j < product_count; j++) { if (strcmp(products[i].name, products[j].name) > 0) { // 交换商品 Product temp = products[i]; products[i] = products[j]; products[j] = temp; } } } } // 打印商品信息 void print_product(int index) { if (index < 0 || index >= product_count) { printf("错误: 无效的商品索引\n"); return; } printf("商品ID: %d\n", products[index].id); printf("商品名称: %s\n", products[index].name); printf("商品类别: %s\n", products[index].category); printf("商品价格: %.2f\n", products[index].price); printf("库存数量: %d\n", products[index].stock); printf("销售数量: %d\n", products[index].sales); printf("------------------------\n"); } // 添加用户 int add_user(int id, const char *name, const char *address, const char *contact) { // 检查用户ID是否已存在 if (find_user_by_id(id) != NULL) { printf("错误: 用户ID %d 已存在\n", id); return -1; } // 检查用户数量是否超过最大值 if (user_count >= MAX_USERS) { printf("错误: 用户数量已达到最大值\n"); return -1; } // 添加用户 users[user_count].id = id; strncpy(users[user_count].name, name, MAX_NAME_LENGTH-1); strncpy(users[user_count].address, address, MAX_ADDRESS_LENGTH-1); strncpy(users[user_count].contact, contact, MAX_NAME_LENGTH-1); // 添加到哈希表 hash_table_insert(id, &users[user_count]); user_count++; return 0; } // 根据ID查找用户 User* find_user_by_id(int id) { return hash_table_lookup(id); } // 根据名称查找用户 int find_user_by_name(const char *name) { for (int i = 0; i < user_count; i++) { if (strcmp(users[i].name, name) == 0) { return i; } } return -1; // 未找到 } // 打印用户信息 void print_user(int index) { if (index < 0 || index >= user_count) { printf("错误: 无效的用户索引\n"); return; } printf("用户ID: %d\n", users[index].id); printf("用户姓名: %s\n", users[index].name); printf("用户地址: %s\n", users[index].address); printf("联系方式: %s\n", users[index].contact); printf("------------------------\n"); } // 创建订单 int create_order(int user_id, int product_id, int quantity) { // 检查用户是否存在 if (find_user_by_id(user_id) == NULL) { printf("错误: 用户ID %d 不存在\n", user_id); return -1; } // 检查商品是否存在 int product_index = find_product_by_id(product_id); if (product_index == -1) { printf("错误: 商品ID %d 不存在\n", product_id); return -1; } // 检查库存是否充足 if (products[product_index].stock < quantity) { printf("错误: 商品库存不足,现有库存: %d\n", products[product_index].stock); return -1; } // 检查订单数量是否超过最大值 if (order_count >= MAX_ORDERS) { printf("错误: 订单数量已达到最大值\n"); return -1; } // 创建订单 orders[order_count].id = order_count + 1; // 订单ID从1开始 orders[order_count].user_id = user_id; orders[order_count].product_id = product_id; orders[order_count].quantity = quantity; orders[order_count].order_time = time(NULL); // 当前时间 orders[order_count].status = PENDING_PAYMENT; // 初始状态为待付款 // 更新库存 update_stock(product_id, -quantity); order_count++; printf("订单创建成功,订单ID: %d\n", orders[order_count-1].id); return orders[order_count-1].id; } // 处理付款 int process_payment(int order_id) { Order *order = find_order_by_id(order_id); if (order == NULL) { printf("错误: 订单ID %d 不存在\n", order_id); return 0; } if (order->status != PENDING_PAYMENT) { printf("错误: 订单状态不是未付款状态,当前状态: "); switch (order->status) { case PAID: printf("已付款\n"); break; case SHIPPED: printf("已发货\n"); break; case COMPLETED: printf("已完成\n"); break; default: printf("未知状态\n"); } return 0; } // 记录历史 push_order_history(order_id, order->status); // 更新状态 order->status = PAID; printf("订单 %d 付款处理成功,当前状态: 已付款\n", order_id); return 1; } // 处理发货 int process_shipment(int order_id) { Order *order = find_order_by_id(order_id); if (order == NULL) { printf("错误: 订单ID %d 不存在\n", order_id); return 0; } if (order->status != PAID) { printf("错误: 订单状态不是已付款状态,当前状态: "); switch (order->status) { case PENDING_PAYMENT: printf("未付款\n"); break; case SHIPPED: printf("已发货\n"); break; case COMPLETED: printf("已完成\n"); break; default: printf("未知状态\n"); } return 0; } // 记录历史 push_order_history(order_id, order->status); // 更新状态 order->status = SHIPPED; printf("订单 %d 发货处理成功,当前状态: 已发货\n", order_id); return 1; } // 确认收货 int confirm_receipt(int order_id) { Order *order = find_order_by_id(order_id); if (order == NULL) { printf("错误: 订单ID %d 不存在\n", order_id); return 0; } if (order->status != SHIPPED) { printf("错误: 订单状态不是已发货状态,当前状态: "); switch (order->status) { case PENDING_PAYMENT: printf("未付款\n"); break; case PAID: printf("已付款\n"); break; case COMPLETED: printf("已完成\n"); break; default: printf("未知状态\n"); } return 0; } // 记录历史 push_order_history(order_id, order->status); // 更新状态 order->status = COMPLETED; // 更新商品销量 int product_index = find_product_by_id(order->product_id); if (product_index != -1) { products[product_index].sales += order->quantity; } printf("订单 %d 收货确认成功,当前状态: 已完成\n", order_id); return 1; } // 根据ID查找订单 Order* find_order_by_id(int id) { for (int i = 0; i < order_count; i++) { if (orders[i].id == id) { return &orders[i]; } } return NULL; // 未找到 } // 按时间排序订单 void sort_orders_by_time() { for (int i = 0; i < order_count - 1; i++) { for (int j = i + 1; j < order_count; j++) { if (orders[i].order_time > orders[j].order_time) { // 交换订单 Order temp = orders[i]; orders[i] = orders[j]; orders[j] = temp; } } } } // 打印订单信息 void print_order(int index) { if (index < 0 || index >= order_count) { printf("错误: 无效的订单索引\n"); return; } char status_str[20]; switch (orders[index].status) { case PENDING_PAYMENT: strcpy(status_str, "待付款"); break; case PAID: strcpy(status_str, "已付款"); break; case SHIPPED: strcpy(status_str, "已发货"); break; case COMPLETED: strcpy(status_str, "已完成"); break; default: strcpy(status_str, "未知状态"); } User *user = find_user_by_id(orders[index].user_id); int product_index = find_product_by_id(orders[index].product_id); printf("订单ID: %d\n", orders[index].id); printf("用户信息: %s (ID: %d)\n", user ? user->name : "未知用户", orders[index].user_id); printf("商品信息: %s (ID: %d)\n", product_index != -1 ? products[product_index].name : "未知商品", orders[index].product_id); printf("数量: %d\n", orders[index].quantity); if (product_index != -1) { printf("总价: %.2f\n", products[product_index].price * orders[index].quantity); } printf("下单时间: %s", ctime(&orders[index].order_time)); printf("订单状态: %s\n", status_str); printf("------------------------\n"); } // 更新库存 int update_stock(int product_id, int quantity) { int index = find_product_by_id(product_id); if (index == -1) { printf("错误: 商品ID %d 不存在\n", product_id); return -1; } // 检查库存是否充足(仅当减少库存时) if (quantity < 0 && products[index].stock < (-quantity)) { printf("错误: 商品库存不足,现有库存: %d\n", products[index].stock); return -1; } products[index].stock += quantity; return 0; } // 检查低库存 void check_low_stock() { printf("低库存商品列表:\n"); int low_stock_count = 0; for (int i = 0; i < product_count; i++) { if (products[i].stock <= RESTOCK_THRESHOLD) { printf("商品ID: %d, 商品名称: %s, 库存: %d\n", products[i].id, products[i].name, products[i].stock); // 添加补货任务 int priority = (products[i].stock == 0) ? 1 : 2; // 缺货为高优先级 enqueue_restock_task(products[i].id, priority); low_stock_count++; } } if (low_stock_count == 0) { printf("所有商品库存充足\n"); } } // 处理补货任务 void process_restock_tasks() { if (is_restock_queue_empty()) { printf("没有需要处理的补货任务\n"); return; } printf("处理补货任务:\n"); while (!is_restock_queue_empty()) { RestockTask task = dequeue_restock_task(); int product_index = find_product_by_id(task.product_id); if (product_index != -1) { // 增加库存(模拟补货100个) update_stock(task.product_id, 100); printf("已补货: 商品ID %d, 商品名称: %s, 优先级: %d\n", task.product_id, products[product_index].name, task.priority); } else { printf("错误: 商品ID %d 不存在,无法补货\n", task.product_id); } } } // 记录订单历史 void push_order_history(int order_id, OrderStatus old_status) { if (history_count >= MAX_ORDERS) { printf("错误: 订单历史记录已满\n"); return; } order_history[history_count].order_id = order_id; order_history[history_count].old_status = old_status; order_history[history_count].timestamp = time(NULL); history_count++; } // 获取订单历史 OrderHistoryItem pop_order_history() { OrderHistoryItem item; item.order_id = -1; item.old_status = PENDING_PAYMENT; item.timestamp = 0; if (history_count <= 0) { printf("错误: 订单历史记录为空\n"); return item; } history_count--; return order_history[history_count]; } // 检查订单是否有历史记录 int has_order_history(int order_id) { for (int i = 0; i < history_count; i++) { if (order_history[i].order_id == order_id) { return 1; } } return 0; } // 添加补货任务 void enqueue_restock_task(int product_id, int priority) { if (restock_count >= MAX_PRODUCTS) { printf("错误: 补货任务队列已满\n"); return; } restock_queue[restock_count].product_id = product_id; restock_queue[restock_count].priority = priority; restock_count++; // 按优先级排序 for (int i = 0; i < restock_count - 1; i++) { for (int j = i + 1; j < restock_count; j++) { if (restock_queue[i].priority > restock_queue[j].priority) { // 交换任务 RestockTask temp = restock_queue[i]; restock_queue[i] = restock_queue[j]; restock_queue[j] = temp; } } } } // 获取补货任务 RestockTask dequeue_restock_task() { RestockTask task; task.product_id = -1; task.priority = 0; if (restock_count <= 0) { printf("错误: 补货任务队列为空\n"); return task; } // 获取优先级最高的任务(队列头部) task = restock_queue[0]; // 将后面的任务前移 for (int i = 0; i < restock_count - 1; i++) { restock_queue[i] = restock_queue[i + 1]; } restock_count--; return task; } // 检查补货队列是否为空 int is_restock_queue_empty() { return restock_count == 0; } // 创建类别节点 CategoryNode* create_category_node(const char *name) { CategoryNode *node = (CategoryNode*)malloc(sizeof(CategoryNode)); if (node == NULL) { printf("错误: 内存分配失败\n"); return NULL; } strncpy(node->name, name, MAX_CATEGORY_LENGTH-1); node->parent = NULL; node->child_count = 0; node->product_count = 0; node->sales_total = 0.0; for (int i = 0; i < MAX_PRODUCTS; i++) { node->children[i] = NULL; } return node; } // 添加子类别 void add_child_category(CategoryNode *parent, CategoryNode *child) { if (parent == NULL || child == NULL) { return; } if (parent->child_count >= MAX_PRODUCTS) { printf("错误: 类别子节点数量已达到最大值\n"); return; } child->parent = parent; parent->children[parent->child_count] = child; parent->child_count++; } // 遍历类别树 void traverse_category_tree(CategoryNode *node, int depth) { if (node == NULL) { return; } // 缩进显示层级 for (int i = 0; i < depth; i++) { printf(" "); } printf("%s (商品数量: %d, 销售总额: %.2f)\n", node->name, node->product_count, node->sales_total); // 递归遍历子类别 for (int i = 0; i < node->child_count; i++) { traverse_category_tree(node->children[i], depth + 1); } } // 更新类别统计 void update_category_stats(CategoryNode *node, Product *product, float sales_amount) { if (node == NULL || product == NULL) { return; } // 更新当前类别统计 node->product_count++; node->sales_total += sales_amount; // 递归更新父类别统计 if (node->parent != NULL) { update_category_stats(node->parent, product, sales_amount); } } // 哈希函数 unsigned int hash_function(int key) { return key % 100; } // 哈希表插入 void hash_table_insert(int key, User *user) { unsigned int index = hash_function(key); HashNode *new_node = (HashNode*)malloc(sizeof(HashNode)); if (new_node == NULL) { printf("错误: 内存分配失败\n"); return; } new_node->key = key; new_node->user = user; new_node->next = user_hash_table[index]; user_hash_table[index] = new_node; } // 哈希表查找 User* hash_table_lookup(int key) { unsigned int index = hash_function(key); HashNode *current = user_hash_table[index]; while (current != NULL) { if (current->key == key) { return current->user; } current = current->next; } return NULL; // 未找到 } // 打印系统状态 void print_system_status() { printf("===== 系统状态报告 =====\n"); printf("商品总数: %d\n", product_count); printf("用户总数: %d\n", user_count); printf("订单总数: %d\n", order_count); int pending_count = 0, paid_count = 0, shipped_count = 0, completed_count = 0; for (int i = 0; i < order_count; i++) { switch (orders[i].status) { case PENDING_PAYMENT: pending_count++; break; case PAID: paid_count++; break; case SHIPPED: shipped_count++; break; case COMPLETED: completed_count++; break; } } printf("订单状态统计:\n"); printf(" 待付款: %d\n", pending_count); printf(" 已付款: %d\n", paid_count); printf(" 已发货: %d\n", shipped_count); printf(" 已完成: %d\n", completed_count); printf("补货任务数量: %d\n", restock_count); printf("订单历史记录数量: %d\n", history_count); printf("=======================\n"); } // 清理旧订单 void cleanup_old_orders() { time_t current_time = time(NULL); time_t cutoff_time = current_time - (CLEANUP_DAYS * 24 * 60 * 60); // N天前 int cleaned_count = 0; int moved_count = 0; // 创建临时数组存储需要保留的订单 Order temp_orders[MAX_ORDERS]; int temp_count = 0; for (int i = 0; i < order_count; i++) { // 只清理已完成的订单 if (orders[i].status == COMPLETED && orders[i].order_time < cutoff_time) { cleaned_count++; } else { // 保留的订单移动到临时数组 temp_orders[temp_count] = orders[i]; temp_count++; } } // 将临时数组复制回原数组 if (temp_count != order_count) { memcpy(orders, temp_orders, temp_count * sizeof(Order)); order_count = temp_count; moved_count = cleaned_count; } printf("清理完成: 共清理 %d 个旧订单\n", moved_count); } // 初始化系统 void initialize_system() { product_count = 0; user_count = 0; order_count = 0; restock_count = 0; history_count = 0; initialize_hash_table(); // 从文件加载数据 load_products_from_file(); load_users_from_file(); load_orders_from_file(); printf("系统初始化完成\n"); // 如果文件加载失败或为空,添加一些测试数据 if (product_count == 0) { add_product(101, "智能手机", "电子产品", 4999.0, 50); add_product(102, "笔记本电脑", "电子产品", 7999.0, 30); add_product(103, "T恤", "服装", 99.0, 100); } if (user_count == 0) { add_user(1, "张三", "北京市朝阳区", "13800138000"); add_user(2, "李四", "上海市浦东新区", "13900139000"); } } // 初始化哈希表 void initialize_hash_table() { for (int i = 0; i < 100; i++) { user_hash_table[i] = NULL; } } // 显示菜单 void show_menu() { printf("\n===== 电商平台管理系统 =====\n"); printf("1. 添加商品\n"); printf("2. 查看所有商品\n"); printf("3. 搜索商品\n"); printf("4. 添加用户\n"); printf("5. 查看所有用户\n"); printf("6. 搜索用户\n"); printf("7. 创建订单\n"); printf("8. 查看所有订单\n"); printf("9. 搜索订单\n"); printf("10. 处理付款\n"); printf("11. 处理发货\n"); printf("12. 确认收货\n"); printf("13. 检查低库存商品\n"); printf("14. 处理补货任务\n"); printf("15. 查看系统状态\n"); printf("16. 清理旧订单\n"); printf("0. 退出系统\n"); printf("===========================\n"); } // 等待用户输入,按任意键返回菜单 void wait_for_user() { printf("\n按Enter键返回菜单..."); while (getchar() != '\n'); // 清除输入缓冲区 getchar(); // 等待用户按下Enter键 } // 输入商品信息 void input_product() { int id; char name[MAX_NAME_LENGTH]; char category[MAX_CATEGORY_LENGTH]; float price; int stock; printf("请输入商品ID: "); scanf("%d", &id); getchar(); // 消耗换行符 printf("请输入商品名称: "); fgets(name, MAX_NAME_LENGTH, stdin); name[strcspn(name, "\n")] = 0; // 去除换行符 printf("请输入商品类别: "); fgets(category, MAX_CATEGORY_LENGTH, stdin); category[strcspn(category, "\n")] = 0; // 去除换行符 printf("请输入商品价格: "); scanf("%f", &price); getchar(); // 消耗换行符 printf("请输入商品库存: "); scanf("%d", &stock); getchar(); // 消耗换行符 if (add_product(id, name, category, price, stock) == 0) { printf("商品添加成功\n"); } } // 列出所有商品 void list_products() { if (product_count == 0) { printf("没有商品记录\n"); return; } sort_products_by_name(); // 按名称排序 printf("商品列表:\n"); for (int i = 0; i < product_count; i++) { printf("%d. %s (ID: %d, 价格: %.2f, 库存: %d)\n", i + 1, products[i].name, products[i].id, products[i].price, products[i].stock); } } // 输入用户信息 void input_user() { int id; char name[MAX_NAME_LENGTH]; char address[MAX_ADDRESS_LENGTH]; char contact[MAX_NAME_LENGTH]; printf("请输入用户ID: "); scanf("%d", &id); getchar(); // 消耗换行符 printf("请输入用户姓名: "); fgets(name, MAX_NAME_LENGTH, stdin); name[strcspn(name, "\n")] = 0; // 去除换行符 printf("请输入用户地址: "); fgets(address, MAX_ADDRESS_LENGTH, stdin); address[strcspn(address, "\n")] = 0; // 去除换行符 printf("请输入联系方式: "); fgets(contact, MAX_NAME_LENGTH, stdin); contact[strcspn(contact, "\n")] = 0; // 去除换行符 if (add_user(id, name, address, contact) == 0) { printf("用户添加成功\n"); } } // 列出所有用户 void list_users() { if (user_count == 0) { printf("没有用户记录\n"); return; } printf("用户列表:\n"); for (int i = 0; i < user_count; i++) { printf("%d. %s (ID: %d, 联系方式: %s)\n", i + 1, users[i].name, users[i].id, users[i].contact); } } // 输入订单信息 void input_order() { int user_id, product_id, quantity; printf("请输入用户ID: "); scanf("%d", &user_id); getchar(); // 消耗换行符 printf("请输入商品ID: "); scanf("%d", &product_id); getchar(); // 消耗换行符 printf("请输入商品数量: "); scanf("%d", &quantity); getchar(); // 消耗换行符 create_order(user_id, product_id, quantity); } // 列出所有订单 void list_orders() { if (order_count == 0) { printf("没有订单记录\n"); return; } sort_orders_by_time(); // 按时间排序 printf("订单列表:\n"); for (int i = 0; i < order_count; i++) { printf("%d. 订单ID: %d, 用户ID: %d, 商品ID: %d, 数量: %d, 状态: ", i + 1, orders[i].id, orders[i].user_id, orders[i].product_id, orders[i].quantity); switch (orders[i].status) { case PENDING_PAYMENT: printf("待付款\n"); break; case PAID: printf("已付款\n"); break; case SHIPPED: printf("已发货\n"); break; case COMPLETED: printf("已完成\n"); break; default: printf("未知状态\n"); } } } // 搜索商品 void search_product() { int choice; printf("搜索方式:\n"); printf("1. 按ID搜索\n"); printf("2. 按名称搜索\n"); printf("请选择: "); scanf("%d", &choice); getchar(); // 消耗换行符 if (choice == 1) { int id; printf("请输入商品ID: "); scanf("%d", &id); getchar(); // 消耗换行符 int index = find_product_by_id(id); if (index != -1) { print_product(index); } else { printf("未找到商品ID为 %d 的记录\n", id); } } else if (choice == 2) { char name[MAX_NAME_LENGTH]; printf("请输入商品名称: "); fgets(name, MAX_NAME_LENGTH, stdin); name[strcspn(name, "\n")] = 0; // 去除换行符 int index = find_product_by_name(name); if (index != -1) { print_product(index); } else { printf("未找到商品名称为 %s 的记录\n", name); } } else { printf("无效的选择\n"); } } // 搜索用户 void search_user() { int choice; printf("搜索方式:\n"); printf("1. 按ID搜索\n"); printf("2. 按名称搜索\n"); printf("请选择: "); scanf("%d", &choice); getchar(); // 消耗换行符 if (choice == 1) { int id; printf("请输入用户ID: "); scanf("%d", &id); getchar(); // 消耗换行符 User *user = find_user_by_id(id); if (user != NULL) { // 查找用户在数组中的索引 int index = -1; for (int i = 0; i < user_count; i++) { if (users[i].id == id) { index = i; break; } } if (index != -1) { print_user(index); } } else { printf("未找到用户ID为 %d 的记录\n", id); } } else if (choice == 2) { char name[MAX_NAME_LENGTH]; printf("请输入用户姓名: "); fgets(name, MAX_NAME_LENGTH, stdin); name[strcspn(name, "\n")] = 0; // 去除换行符 int index = find_user_by_name(name); if (index != -1) { print_user(index); } else { printf("未找到用户姓名为 %s 的记录\n", name); } } else { printf("无效的选择\n"); } } // 搜索订单 void search_order() { int id; printf("请输入订单ID: "); scanf("%d", &id); getchar(); // 消耗换行符 Order *order = find_order_by_id(id); if (order != NULL) { // 查找订单在数组中的索引 int index = -1; for (int i = 0; i < order_count; i++) { if (orders[i].id == id) { index = i; break; } } if (index != -1) { print_order(index); } } else { printf("未找到订单ID为 %d 的记录\n", id); } } // 主函数 int main() { initialize_system(); int choice; do { show_menu(); printf("请输入您的选择: "); if (scanf("%d", &choice) != 1) { printf("输入错误,请输入一个数字\n"); while (getchar() != '\n'); // 清除错误输入 continue; } getchar(); // 消耗换行符 switch (choice) { case 1: input_product(); wait_for_user(); break; case 2: list_products(); wait_for_user(); break; case 3: search_product(); wait_for_user(); break; case 4: input_user(); wait_for_user(); break; case 5: list_users(); wait_for_user(); break; case 6: search_user(); wait_for_user(); break; case 7: input_order(); wait_for_user(); break; case 8: list_orders(); wait_for_user(); break; case 9: search_order(); wait_for_user(); break; case 10: printf("请输入要处理付款的订单ID: "); int order_id; scanf("%d", &order_id); getchar(); // 消耗换行符 process_payment(order_id); wait_for_user(); break; case 11: printf("请输入要处理发货的订单ID: "); scanf("%d", &order_id); getchar(); // 消耗换行符 process_shipment(order_id); wait_for_user(); break; case 12: printf("请输入要确认收货的订单ID: "); scanf("%d", &order_id); getchar(); // 消耗换行符 confirm_receipt(order_id); wait_for_user(); break; case 13: check_low_stock(); wait_for_user(); break; case 14: process_restock_tasks(); wait_for_user(); break; case 15: print_system_status(); wait_for_user(); break; case 16: cleanup_old_orders(); wait_for_user(); break; case 0: printf("退出系统...\n"); break; default: printf("无效的选择,请重新输入\n"); } } while (choice != 0); return 0; }
06-18
资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值