-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_function.cpp
More file actions
230 lines (210 loc) · 6.99 KB
/
admin_function.cpp
File metadata and controls
230 lines (210 loc) · 6.99 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
//
// Created by wangh on 2023/6/22.
//
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>
#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; // 订单信息
void AdminLogin() {
system("cls");
ifstream ifs("../data/AdminAccount.txt");
if (!ifs.is_open()) {
cout << "文件打开失败" << endl;
return;
}
string username;
string passwd;
cout << "请输入管理员用户名:";
cin >> username;
cout << "请输入管理员密码:";
cin >> passwd;
string username1;
string passwd1;
ifs >> username1;
ifs >> passwd1;
if (username == username1 && passwd == passwd1) {
cout << "登录成功" << endl;
cout << "欢迎您," << username << endl;
AdminMenu();
return;
}
else
cout << "用户名或密码错误" << endl;
ifs.close();
return;
}
void ShowAdminMenu()
{
system("cls");
cout << "***** 欢迎进入管理员界面 *****" << endl;
cout << "\t1. 查看所有商品" << endl;
cout << "\t2. 搜索商品" << endl;
cout << "\t3. 查看所有订单" << endl;
cout << "\t4. 查看所有用户" << endl;
cout << "\t5. 删除用户" << endl;
cout << "\t6. 下架商品" << endl;
cout << "\t7. 退出登录" << endl;
}
void AdminMenu()
{
while(true) {
ShowAdminMenu();
int option = 0;
cout << "请输入您的选择:";
cin >> option;
if (cin.fail()) {
cout << "输入错误,请重新输入" << endl;
cin.clear();
cin.ignore(1024, '\n');
system("pause");
continue;
}
switch (option) {
case 1:
cout << "查看所有商品" << endl;
ShowAllCommodity();
break;
case 2:
cout << "搜索商品" << endl;
SearchCommodity();
break;
case 3:
cout << "查看所有订单" << endl;
ShowAllOrder();
break;
case 4:
cout << "查看所有用户" << endl;
ShowAllUser();
break;
case 5:
cout << "删除用户" << endl;
DeleteUser();
break;
case 6:
cout << "下架商品" << endl;
DeleteCommodity();
break;
case 7:
cout << "退出登录" << endl;
return;
default:
cout << "输入错误,请重新输入" << endl;
break;
}
system("pause");
}
}
void ShowAllUser() // 显示所有用户信息
{
system("cls");
cout << "==================================================================================================" << endl;
cout << "== 所有用户信息 ==" << endl;
cout << "==================================================================================================" << endl;
cout << left << setw(15) << "用户ID" << setw(15) << "用户名" << setw(15) << "密码" <<setw(20) << "用户地址" << setw(20) << "用户电话" << setw(15) << "余额" << endl;
for (auto it = v.begin(); it != v.end(); it++)
{
cout << left << setw(15) << it->id << setw(15) << it->username << setw(15) << it->password << setw(20) << it->address << setw(20) << it->phone << setw(15) << it->balance << endl;
}
cout << "==================================================================================================" << endl;
return;
}
void RemoveUserCommodity(string id) // 删除用户的商品
{
for (auto it = v1.begin(); it != v1.end(); it++)
{
if (it->seller == id)
{
it->status = "已下架";
UpdateCommodity();
return;
}
}
return;
}
void DeleteUser()
{
system("cls");
cout << "***** 删除用户 *****" << endl;
cout << "请输入要删除的用户ID:";
string del_id;
cin >> del_id;
int flag = 0;
for (auto it = v.begin(); it != v.end(); it++)
{
if (it->id == del_id)
{
flag = 1;
cout << "删除成功" << endl;
v.erase(it);
RemoveUserCommodity(del_id);
break;
}
}
if (flag == 0)
cout << "用户不存在" << endl;
UpdateUser();
return;
}
void ShowAllOrder()
{
system("cls");
cout << "=======================================================================================================" << endl;
cout << "== 所有订单 ==" << endl;
cout << "=======================================================================================================" << endl;
cout << left << setw(15) << "订单ID" << setw(15) << "商品ID" << setw(20) << "商品名称" << setw(10) << "商品价格" << setw(10) << "买家ID" << setw(10) << "卖家ID" << setw(20) << "订单时间" << endl;
for (auto it = v2.begin(); it != v2.end(); it++)
{
for (auto it1 = v1.begin(); it1 != v1.end(); it1++)
{
if (it->commodity_id == it1->id)
{
cout << left << setw(15) << it->order_id << setw(15) << it->commodity_id << setw(20) << it1->name << setw(10) << it->price << setw(10) << it->buyer << setw(10) << it->seller << setw(20) << it->order_time << endl;
}
}
}
cout << "=======================================================================================================" << endl;
return;
}
void ShowAllCommodity()
{
system("cls");
cout << "=======================================================================================================" << endl;
cout << "== 所有商品 ==" << endl;
cout << "=======================================================================================================" << endl;
cout << left << setw(20) << "商品id" << setw(20) << "商品名称" << setw(20) << "商品价格" << setw(30) << "发布时间" << setw(20) << "商品状态" << endl;
for (auto it = v1.begin(); it != v1.end(); it++)
{
cout << left << setw(20) << it->id << setw(20) << it->name << setw(20) << it->price << setw(30) << it->added_time << setw(20) << it->status << endl;
}
cout << "=======================================================================================================" << endl;
return;
}
void DeleteCommodity() // 下架商品
{
system("cls");
string id;
cout << "请输入要下架的商品id:";
cin >> id;
for (auto it = v1.begin(); it != v1.end(); it++)
{
if (it->id == id)
{
it->status = "已下架";
cout << "下架成功" << endl;
UpdateCommodity();
return;
}
}
cout << "商品不存在" << endl;
return;
}