-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_function.cpp
More file actions
434 lines (409 loc) · 11.1 KB
/
user_function.cpp
File metadata and controls
434 lines (409 loc) · 11.1 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//
// Created by wangh on 2023/6/20.
//
# include <iostream>
# include <string>
# include <fstream>
# include <iomanip>
# include <algorithm>
# include "menu.h"
# include "config.h"
using namespace std;
extern int U_id; // 用户id
extern int C_id; // 商品id
extern int O_id; // 订单id
extern vector<User> v; // 用户信息
extern vector<Commodity> v1; // 商品信息
extern vector<Order> v2; // 订单信息
int check(string username) // 检查用户名是否存在
{
for (auto it = v.begin(); it != v.end(); it++)
{
if (it->username == username)
return 1;
}
return 0;
}
void Register() // 用户注册
{
system("cls");
User user;
user.id= 'U' + to_string(U_id);
user.address = "";
user.phone = "";
user.balance = 0;
cout << "请输入您的用户名:";
cin >> user.username;
while (check(user.username))
{
cout << "该用户名已存在,请重新输入:";
cin >> user.username;
}
cout << "请输入您的密码:";
cin >> user.password;
cout << "注册成功" << endl;
// 更新User.txt
v.push_back(user);
UpdateUser();
// 更新Config.txt
U_id++;
UpdateConfig();
return;
}
void Login() // 用户登录
{
system("cls");
string username;
string passwd;
cout << "请输入您的用户名:";
cin >> username;
cout << "请输入您的密码:";
cin >> passwd;
for (auto it = v.begin(); it != v.end(); it++)
{
if (it->username == username && it->password == passwd)
{
cout << "登录成功" << endl;
cout << "欢迎您," << username << endl;
UserMenu(username);
return;
}
}
cout << "用户名或密码错误" << endl;
return;
}
void UserMenu(string username) // 用户菜单
{
string user_id;
for (auto it = v.begin(); it != v.end(); it++)
{
if (it->username == username)
{
user_id = it->id;
break;
}
}
while(true) {
system("cls");
cout << "===================================================" << endl;
cout << " 1.我是买家 2.我是卖家 3.个人信息 4.退出登录" << endl;
cout << "===================================================" << endl;
int option;
cin >> option;
if (cin.fail()) {
cout << "输入错误,请重新输入" << endl;
cin.clear();
cin.ignore(1024, '\n');
system("pause");
continue;
}
switch (option) {
case 1:
cout << "我是买家" << endl;
BuyerMenu(user_id);
break;
case 2:
cout << "我是卖家" << endl;
SellerMenu(user_id);
break;
case 3:
cout << "个人信息" << endl;
PersonalInfo(user_id);
break;
case 4:
cout << "退出登录" << endl;
return;
default:
cout << "输入错误" << endl;
break;
}
system("pause");
}
}
void SellerMenu(string user_id) // 卖家菜单
{
while(true) {
system("cls");
cout << "====================================================================================" << endl;
cout << " 1.商品上架 2.查看发布商品 3.修改商品信息 4.商品下架 5.查看历史订单 6. 返回" << endl;
cout << "====================================================================================" << endl;
int option;
cin >> option;
if (cin.fail()) {
cout << "输入错误,请重新输入" << endl;
cin.clear();
cin.ignore(1024, '\n');
system("pause");
continue;
}
switch (option) {
case 1:
cout << "商品上架" << endl;
PostCommodity(user_id);
break;
case 2:
cout << "查看发布商品" << endl;
ShowMyCommodity(user_id);
break;
case 3:
cout << "修改商品信息" << endl;
ModifyCommodity(user_id);
break;
case 4:
cout << "商品下架" << endl;
RemoveCommodity(user_id);
break;
case 5:
cout << "查看历史订单" << endl;
ShowMySellOrder(user_id);
break;
case 6:
cout << "返回" << endl;
return;
default:
cout << "输入错误" << endl;
break;
}
system("pause");
}
}
void BuyerMenu(string user_id) // 买家菜单
{
while(true)
{
system("cls");
cout << "=======================================================================" << endl;
cout << " 1.查看商品列表 2.购买商品 3.搜索商品 4.查看历史订单 5.返回" << endl;
cout << "=======================================================================" << endl;
int option;
cin >> option;
if (cin.fail()) {
cout << "输入错误,请重新输入" << endl;
cin.clear();
cin.ignore(1024, '\n');
system("pause");
continue;
}
switch (option) {
case 1:
ShowBuyCommodity(user_id);
break;
case 2:
BuyCommodity(user_id);
break;
case 3:
SearchCommodity();
break;
case 4:
ShowMyBuyOrder(user_id);
break;
case 5:
cout << "返回" << endl;
return;
default:
cout << "输入错误" << endl;
break;
}
system("pause");
}
}
void PersonalInfo(string user_id) // 个人信息
{
while(true)
{
system("cls");
cout << "=========================================================" << endl;
cout << " 1.查看个人信息 2.修改个人信息 3.充值 4.返回" << endl;
cout << "=========================================================" << endl;
int option;
cin >> option;
if (cin.fail()) {
cout << "输入错误,请重新输入" << endl;
cin.clear();
cin.ignore(1024, '\n');
system("pause");
continue;
}
switch (option) {
case 1:
cout << "查看个人信息" << endl;
ShowPersonalInfo(user_id);
break;
case 2:
cout << "修改个人信息" << endl;
ModifyPersonalInfo(user_id);
break;
case 3:
cout << "充值" << endl;
Recharge(user_id);
break;
case 4:
cout << "返回" << endl;
return;
default:
cout << "输入错误" << endl;
break;
}
system("pause");
}
}
void ShowPersonalInfo(string user_id) // 查看个人信息
{
system("cls");
for (auto it = v.begin(); it != v.end(); it++)
{
if (it->id == user_id)
{
cout << endl;
cout << "***************个人信息***************" << endl;
cout << left << setw(10) << "用户ID:" << it->id << endl;
cout << left << setw(10) << "用户名:" << it->username << endl;
cout << left << setw(10) << "地址:" << it->address << endl;
cout << left << setw(10) << "电话:" << it->phone << endl;
cout << left << setw(10) << fixed << setprecision(2) << "余额:" << it->balance << endl;
cout << "**************************************" << endl;
return;
}
}
}
void ModifyPersonalInfo(string user_id) // 修改个人信息
{
while (true) {
system("cls");
ShowPersonalInfo(user_id);
int option;
cout << "1.修改用户名 2.修改密码 3.修改地址 4.修改电话 5.返回" << endl;
cin >> option;
if (cin.fail()) {
cout << "输入错误,请重新输入" << endl;
cin.clear();
cin.ignore(1024, '\n');
system("pause");
continue;
}
switch (option) {
case 1:
cout << "修改用户名" << endl;
ModifyUsername(user_id);
break;
case 2:
cout << "修改密码" << endl;
ModifyPassword(user_id);
break;
case 3:
cout << "修改地址" << endl;
ModifyAddress(user_id);
break;
case 4:
cout << "修改电话" << endl;
ModifyPhone(user_id);
break;
case 5:
cout << "修改成功" << endl;
return;
default:
cout << "输入错误" << endl;
break;
}
system("pause");
}
}
void ModifyUsername(string user_id) // 修改用户名
{
string username;
cout << "请输入新的用户名:";
cin >> username;
if (check(username))
{
cout << "用户名已存在" << endl;
return;
}
for (auto it = v.begin(); it != v.end(); it++)
{
if (it->id == user_id)
{
it->username = username;
cout << "修改成功" << endl;
UpdateUser();
return;
}
}
}
void ModifyPassword(string user_id) // 修改密码
{
string new_password,old_password;
for (auto it = v.begin(); it != v.end(); it++)
{
if (it->id == user_id)
{
cout << "请输入旧密码:";
cin >> old_password;
if (old_password != it->password)
{
cout << "密码错误" << endl;
return;
}
cout << "请输入新密码:";
cin >> new_password;
it->password = new_password;
cout << "修改成功" << endl;
UpdateUser();
return;
}
}
}
void ModifyAddress(string user_id) // 修改地址
{
string address;
cout << "请输入新的地址:";
cin >> address;
for (auto it = v.begin(); it != v.end(); it++)
{
if (it->id == user_id)
{
it->address = address;
cout << "修改成功" << endl;
UpdateUser();
return;
}
}
}
void ModifyPhone(string user_id) // 修改电话
{
string phone;
cout << "请输入新的电话:";
cin >> phone;
for (auto it = v.begin(); it != v.end(); it++)
{
if (it->id == user_id)
{
it->phone = phone;
cout << "修改成功" << endl;
UpdateUser();
return;
}
}
}
void Recharge(string user_id) // 充值
{
system("cls");
double money;
cout << "请输入充值金额:";
cin >> money;
if (cin.fail()) {
cout << "ERROR!!" << endl;
cin.clear();
cin.ignore(1024, '\n');
return;
}
for (auto it = v.begin(); it != v.end(); it++)
{
if (it->id == user_id)
{
it->balance += money;
cout << "充值成功" << endl;
UpdateUser();
return;
}
}
}