-
Notifications
You must be signed in to change notification settings - Fork 0
/
vehicleFile.hpp
241 lines (229 loc) · 6.82 KB
/
vehicleFile.hpp
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
#pragma once
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include"userVehicle.hpp"
class VehicleOp :public Vehicle {
public:
VehicleOp() {};
VehicleOp(Vehicle vehicle);
int vehicleStore(); //车辆信息保存
void getVehicle(); //绑定车辆信息
void update(string platenum, string model, string color, string year); //更新车辆信息
void getList(); //获取全部车辆信息
void keyInformationQueries(string platenum, string model, string color, string year); //关键信息查询
void addVehicle(string platenum, string model, string color, string year); //添加车辆
void deleteVehicle(string platenum); //删除车辆
void modifyVehicle(string modifyPlatenum, string platenum,
string model, string color, string year); //根据车牌号修改车辆信息
};
VehicleOp::VehicleOp(Vehicle vehicle) {
this->setAccount(vehicle.getAccount());
this->setName(vehicle.getName());
this->setPassword(vehicle.getPassword());
this->setPermission(vehicle.getPermission());
this->setPlateNum(vehicle.getPlateNum());
this->setModel(vehicle.getModel());
this->setColor(vehicle.getColor());
this->setYear(vehicle.getYear());
}
int VehicleOp::vehicleStore() {
ifstream fin("Vehicle.txt", ios_base::in);
string curacc, curnum, line;
fin.seekg(0, ios_base::beg);
while (!fin.eof()) {
fin >> curacc >> curnum;
getline(fin, line);
if (this->getAccount() == curacc &&
this->getPlateNum() != "未录入") {
cout << "车辆信息已被录入!" << endl;
return -1;
}
}
fin.close();
ofstream fout("Vehicle.txt", ios_base::app);
fout << endl;
fout << this->getAccount() << " "
<< this->getPlateNum() << " "
<< this->getModel() << " "
<< this->getColor() << " "
<< this->getYear() << " " << endl;
if (this->getPlateNum() != "未录入")
cout << "车辆信息录入成功!" << endl;
fout.close();
return 1;
}
void VehicleOp::getVehicle() {
ifstream fin("Vehicle.txt", ios_base::in);
string curacc, line;
fin.seekg(0, ios_base::beg);
while (!fin.eof()) {
fin >> curacc;
if (this->getAccount() == curacc) {
string curnum, curmodel, curcolor, curyear;
fin >> curnum >> curmodel >> curcolor >> curyear;
this->setAccount(curacc);
this->setPlateNum(curnum);
this->setModel(curmodel);
this->setColor(curcolor);
this->setYear(curyear);
fin.close();
break;
}
else {
getline(fin, line);
this->setPlateNum("未录入");
this->setModel("未录入");
this->setColor("未录入");
this->setYear("未录入");
}
}
}
void VehicleOp::update(string platenum, string model, string color, string year) {
ifstream fin("Vehicle.txt", ios::in);
fin.seekg(0, ios_base::beg);
string curacc, line;
string fileContents = "";
string oriContents[4];
string curContents[4] = { platenum,model,color,year };
string flag = "\\";
while (!fin.eof()) {
fin >> curacc;
if (this->getAccount() != curacc) {
getline(fin, line);
fileContents += curacc;
fileContents += line;
fileContents += "\n";
}
else if (this->getAccount() == curacc) {
for (int i = 0; i < 4; ++i)
fin >> oriContents[i];
getline(fin, line);
}
}
ofstream fout("Vehicle.txt", ios_base::out);
fout.flush();
fout << fileContents;
fout << this->getAccount() << " ";
for (int i = 0; i < 4; ++i) {
if (curContents[i] != flag)
fout << curContents[i] << ' ';
else
fout << oriContents[i] << ' ';
}
fout.close();
}
void VehicleOp::getList() {
string line;
ifstream fin("Vehicle.txt", ios::in | ios::ate);
fin.seekg(0, ios_base::beg);
string curacc, curnum, curmodel, curcolor, curyear;
cout << left << setw(15) << "车牌号";
cout << left << setw(15) << "车型";
cout << left << setw(15) << "颜色";
cout << left << setw(15) << "年份" << endl;
fin >> curacc >> curnum >> curmodel >> curcolor >> curyear;
while (!fin.eof()) {
cout << left << setw(15) << curnum;
cout << left << setw(15) << curmodel;
cout << left << setw(15) << curcolor;
cout << left << setw(15) << curyear << endl;
fin >> curacc >> curnum >> curmodel >> curcolor >> curyear;
}
fin.close();
}
void VehicleOp::keyInformationQueries(string platenum, string model, string color, string year) {
string line;
ifstream fin("Vehicle.txt", ios::in | ios::ate);
fin.seekg(0, ios_base::beg);
string curacc, curnum, curmodel, curcolor, curyear;
cout << "----------------------------查询结果---------------------------" << endl;
this->getPermission() ?
cout << left << setw(15) << "车辆所属用户" : cout << "";
cout << left << setw(15) << "车牌号";
cout << left << setw(15) << "车型";
cout << left << setw(15) << "颜色";
cout << left << setw(15) << "年份" << endl;
fin >> curacc >> curnum >> curmodel >> curcolor >> curyear;
while (!fin.eof()) {
if (platenum == curnum || model == curmodel || color == curcolor || year == curyear) {
this->getPermission() ?
cout << left << setw(15) << curacc : cout << "";
cout << left << setw(15) << curnum;
cout << left << setw(15) << curmodel;
cout << left << setw(15) << curcolor;
cout << left << setw(15) << curyear << endl;
}
fin >> curacc >> curnum >> curmodel >> curcolor >> curyear;
}
cout << "\n\n\n" << endl;
cout << "---------------------------------------------------------------" << endl;
fin.close();
}
void VehicleOp::addVehicle(string platenum, string model, string color, string year) {
ofstream fout("Vehicle.txt", ios_base::app);
fout << endl;
fout << "无" << " " << platenum << " " << model << " " << color << " " << year << " " << endl;
cout << "车辆信息录入成功!" << endl;
fout.close();
}
void VehicleOp::deleteVehicle(string platenum) {
ifstream fin("Vehicle.txt", ios::in);
fin.seekg(0, ios_base::beg);
string curacc, curnum, line;
string fileContents = "";
fin >> curacc >> curnum;
while (!fin.eof()) {
if (curnum != platenum) {
getline(fin, line);
fileContents += curacc;
fileContents += " ";
fileContents += curnum;
fileContents += line;
fileContents += "\n";
}
else if (curnum == platenum)
getline(fin, line);
fin >> curacc >> curnum;
}
ofstream fout("Vehicle.txt", ios_base::out);
fout.flush();
fout << fileContents;
cout << "删除成功!\n" << endl;
fout.close();
}
void VehicleOp::modifyVehicle(string modifyPlatenum, string platenum,
string model, string color, string year) {
ifstream fin("Vehicle.txt", ios::in);
fin.seekg(0, ios_base::beg);
string curacc, curnum, line;
string modifyAccount;
string fileContents = "";
fin >> curacc >> curnum;
while (!fin.eof()) {
if (curnum != modifyPlatenum) {
getline(fin, line);
fileContents += curacc;
fileContents += " ";
fileContents += curnum;
fileContents += line;
fileContents += "\n";
}
else if (curnum == modifyPlatenum) {
getline(fin, line);
modifyAccount = curacc;
}
fin >> curacc >> curnum;
}
ofstream fout("Vehicle.txt", ios_base::out);
fout.flush();
fout << modifyAccount << ' '
<< platenum << ' '
<< model << ' '
<< color << ' '
<< year << endl;
fout << fileContents;
cout << "修改成功!\n" << endl;
fout.close();
}