-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommodity_function.cpp
More file actions
262 lines (241 loc) · 8.46 KB
/
commodity_function.cpp
File metadata and controls
262 lines (241 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//
// Created by wangh on 2023/6/22.
//
#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
#include <iomanip>
#include "menu.h"
#include "config.h"
using namespace std;
extern vector<User> v; // 用户信息
extern vector<Commodity> v1; // 商品信息
extern vector<Order> v2; // 订单信息
extern int U_id; // 用户id
extern int C_id; // 商品id
extern int O_id; // 订单id
string GetcurTime() // 获取当前时间
{
time_t now = time(0);
tm* ltm = localtime(&now);
string curTime = to_string(1900 + ltm->tm_year) + "-" +
to_string(1 + ltm->tm_mon) + "-" +
to_string(ltm->tm_mday) +" " +
to_string(ltm->tm_hour) + ":" +
to_string(ltm->tm_min) + ":" +
to_string(ltm->tm_sec);
return curTime;
}
void PostCommodity(string user_id) // 发布商品
{
system("cls");
Commodity commodity;
commodity.id = 'C' + to_string(C_id);
commodity.seller = user_id;
commodity.buyer = "";
commodity.status = "在售";
cout << "请输入商品名称:";
cin >> commodity.name;
cout << "请输入商品价格:";
cin >> commodity.price;
cout << "请输入商品描述:";
cin >> commodity.description;
commodity.added_time = GetcurTime();
cout << "发布成功" << endl;
C_id++;
UpdateConfig();
v1.push_back(commodity);
UpdateCommodity();
return;
}
void ShowMyCommodity(string user_id) // 查看我的商品
{
system("cls");
cout << endl;
cout << "=======================================================================================================" << endl;
cout << left << setw(10) << "商品id" << setw(10) << "商品名称" << setw(10) << "商品价格" << setw(20) << "发布时间" << setw(10) << "商品状态" << endl;
for (auto it = v1.begin(); it != v1.end(); it++)
{
if (it->seller == user_id)
{
cout << left << setw(10) << it->id << setw(10) << it->name << setw(10) << fixed << setprecision(2) << it->price << setw(20) << it->added_time << setw(10) << it->status << endl;
}
}
cout << "=======================================================================================================" << endl;
return;
}
void ModifyCommodity(string user_id) // 修改商品信息
{
system("cls");
string id;
cout << "请输入要修改的商品id:";
cin >> id;
for (auto it = v1.begin(); it != v1.end(); it++)
{
if (it->id == id && it->seller == user_id)
{
while(true)
{
system("cls");
cout << endl;
cout << "**************商品信息**************" << endl;
cout << left << setw(20) << "商品id:" << setw(20) << it->id << endl;
cout << left << setw(20) << "商品名称:" << setw(20) << it->name << endl;
cout << left << setw(20) << "商品价格:" << setw(20) << fixed << setprecision(2) << it->price << endl;
cout << left << setw(20) << "商品描述:" << setw(20) << it->description << endl;
cout << "************************************" << endl;
cout << "请输入要修改的信息:1.商品名称 2.商品价格 3.商品描述 4.退出" << endl;
int option;
cin >> option;
if (cin.fail())
{
cin.clear();
cin.ignore(1024, '\n');
cout << "输入错误,请重新输入" << endl;
continue;
}
switch (option)
{
case 1:
cout << "请输入商品名称:";
cin >> it->name;
break;
case 2:
cout << "请输入商品价格:";
cin >> it->price;
break;
case 3:
cout << "请输入商品描述:";
cin >> it->description;
break;
case 4:
cout << "修改成功" << endl;
return;
default:
cout << "输入错误,请重新输入" << endl;
break;
}
UpdateCommodity();
system("pause");
}
}
}
cout << "商品不存在或您无权修改" << endl;
return;
}
void RemoveCommodity(string user_id) // 下架商品
{
system("cls");
string id;
cout << "请输入要下架的商品id:";
cin >> id;
for (auto it = v1.begin(); it != v1.end(); it++)
{
if (it->id == id && it->seller == user_id)
{
it->status = "已下架";
cout << "下架成功" << endl;
UpdateCommodity();
return;
}
}
cout << "商品不存在或您无权操作" << endl;
return;
}
void ShowBuyCommodity(string user_id) // 查看可购买商品
{
system("cls");
cout << endl;
cout << "========================可购买商品========================" << endl;
cout << left << setw(10) << "商品id" << setw(10) << "商品名称" << setw(10) << "商品价格" << setw(20) << "发布时间" << setw(10) << "商品状态" << endl;
for (auto it = v1.begin(); it != v1.end(); it++)
{
if (it->status == "在售" && it->seller != user_id)
{
cout << left << setw(10) << it->id << setw(10) << it->name << setw(10) << fixed << setprecision(2)<< it->price << setw(20) << it->added_time << setw(10) << it->status << endl;
}
}
cout << "===========================================================" << endl;
return;
}
void BuyCommodity(string user_id) // 购买商品
{
system("cls");
string id;
cout << "请输入要购买的商品id:";
cin >> id;
for (auto it = v1.begin(); it != v1.end(); it++)
{
if (it->id == id && it->status == "在售" && it->seller != user_id)
{
// 支付
for (auto it1 = v.begin(); it1 != v.end(); it1++)
{
if (it1->id == user_id)
{
if (it1->balance < it->price)
{
cout << "余额不足,购买失败" << endl;
return;
}
else
{
for(auto it2 = v.begin(); it2 != v.end(); it2++)
{
if (it2->id == it->seller)
{
it2->balance += it->price;
it1->balance -= it->price;
break;
}
}
UpdateUser();
Order order;
order.order_id = 'O' + to_string(O_id);
order.commodity_id = it->id;
order.price = it->price;
order.seller= it->seller;
order.buyer = it1->id;
order.order_time = GetcurTime();
v2.push_back(order);
UpdateOrder();
O_id++;
UpdateConfig();
break;
}
}
}
it->buyer = user_id;
it->status = "已售出";
cout << "购买成功" << endl;
UpdateCommodity();
return;
}
}
cout << "商品不存在或已下架" << endl;
return;
}
void SearchCommodity() // 搜索商品
{
system("cls");
string name;
cout << "请输入要搜索的商品名称:";
cin >> name;
for (auto it = v1.begin(); it != v1.end(); it++)
{
if ( (it->name).find(name) != string::npos )
{
cout << endl;
cout << "**************商品信息**************" << endl;
cout << left << setw(10) <<"名称" << setw(10) << it -> name << endl;
cout << left << setw(10) <<"价格" << setw(10) << fixed << setprecision(2) <<it -> price << endl;
cout << left << setw(10) <<"描述" << setw(10) << it -> description << endl;
cout << left << setw(10) <<"发布时间" << setw(10) << it -> added_time << endl;
cout << left << setw(10) <<"状态" << setw(10) << it -> status << endl;
cout << left << setw(10) <<"卖家" << setw(10) << it -> seller << endl;
cout << "************************************" << endl;
}
}
return;
}