-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUsersManager.cpp
More file actions
167 lines (157 loc) · 4.69 KB
/
Copy pathUsersManager.cpp
File metadata and controls
167 lines (157 loc) · 4.69 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
#include "pch.h"
#include "User.h"
#include <fstream>
#include <vector>
using std::vector;
using std::ios;
using std::fstream;
#include "UsersManager.h"
UsersManager::UsersManager() {
fstream f;
f.open("UserDataBase.txt", ios::in | ios::binary);
unsigned total = 0;
f.read((char*)&total, sizeof(unsigned));
for (unsigned i = 0; i < total; ++i) {
char Name[20], Password[20], Tel[20], ID[20];
f.read(Name, 20 * sizeof(char));
f.read(Tel, 20 * sizeof(char));
f.read(Password, 20 * sizeof(char));
unsigned type, money;
f.read((char*)&type, sizeof(unsigned));
f.read(ID, 20 * sizeof(char));
f.read((char*)&money, sizeof(money));
unsigned n = 0;
f.read((char*)&n, sizeof(unsigned));
User NewU(Name, Password, Tel, type, money);
NewU.SetID(ID);
if (n)
for (int j = 0; j < n; ++j) {
char No[10], Date[20], Dest[20], Ori[20], ST[10], AT[20];
f.read(No, 10 * sizeof(char));
f.read(Date, 20 * sizeof(char));
f.read(Ori, 20 * sizeof(char));
f.read(Dest, 20 * sizeof(char));
f.read(ST, 10 * sizeof(char));
f.read(AT, 10 * sizeof(char));
int Fc, Fp, Sc, Sp;
f.read((char*)&Fc, sizeof(int));
f.read((char*)&Fp, sizeof(int));
f.read((char*)&Sc, sizeof(int));
f.read((char*)&Sp, sizeof(int));
FlightInfo New(No, Date);
New.SetOrigin(Ori);
New.SetDestination(Dest);
New.SetTime(ST, AT);
New.SetFirstInfo(Fc, Fp);
New.SetSecondInfo(Sc, Sp);
NewU.BookedList.push_back(New);
}
UserList.push_back(NewU);
}
f.close();
}
UsersManager::~UsersManager() {
fstream f;
f.open("UserDataBase.txt", ios::out | ios::binary);
unsigned size = UserList.size();
f.write((char*)&size, sizeof(unsigned));
for (unsigned i = 0; i < size; ++i) {
User tmp = UserList[i];
f.write(tmp.GetName(), 20 * sizeof(char));
f.write(tmp.GetTel(), 20 * sizeof(char));
f.write(tmp.GetPassword(), 20 * sizeof(char));
unsigned type = tmp.GetUserType(), money = tmp.GetMoney();
f.write((char*)&type, sizeof(unsigned));
f.write(tmp.GetID(), 20 * sizeof(char));
f.write((char*)&money, sizeof(unsigned));
unsigned total = tmp.BookedList.size();
f.write((char*)&total, sizeof(unsigned));
if (total) {
for (unsigned j = 0; j < total; ++j) {
FlightInfo t = tmp.BookedList[j];
char* No = t.GetNum();
char* Date = t.GetDate();
f.write(No, 10 * sizeof(char));
f.write(Date, 20 * sizeof(char));
f.write(t.GetOrigin(), 20 * sizeof(char));
f.write(t.GetDestination(), 20 * sizeof(char));
f.write(t.GetStartTime(), 10 * sizeof(char));
f.write(t.GetArriveTime(), 10 * sizeof(char));
Seats t1 = t.GetFirst();
int t11 = t1.cnt, t12 = t1.price;
f.write((char*)&t11, sizeof(int));
f.write((char*)&t12, sizeof(int));
Seats t3 = t.GetSecond();
int t31 = t3.cnt, t32 = t3.price;
f.write((char*)&t31, sizeof(int));
f.write((char*)&t32, sizeof(int));
}
}
}
f.close();
}
bool UsersManager::AddUser(char Name[], char Password[], char Tel[], char *ID, unsigned type) {
unsigned size = UserList.size();
for (unsigned i = 0; i < size; ++i) {
User tmp = UserList[i];
if (strcmp(Tel, tmp.GetTel()) == 0)
return false;
}
User New(Name, Password, Tel, type);
New.SetID(ID);
UserList.push_back(New);
return true;
}
bool UsersManager::DelUser(char Tel[]) {
vector <User>::iterator it;
for (it = UserList.begin(); it != UserList.end(); ++it)
if (strcmp(Tel, it->GetTel()) == 0) {
UserList.erase(it);
return true;
}
return false;
}
bool UsersManager::ResetPassword(char Tel[], char pw[]) {
vector<User>::iterator it;
for (it = UserList.begin(); it != UserList.end(); ++it)
if (strcmp(Tel, it->GetTel()) == 0) {
it->SetPassword(pw);
return true;
}
return false; //Êä³öδÕÒµ½Óû§ÐÅÏ¢
}
bool UsersManager::ValidateUser(char Tel[], char Password[], unsigned type) {
unsigned size = UserList.size();
for (unsigned i = 0; i < size; ++i) {
User tmp = UserList[i];
if ((strcmp(Tel, tmp.GetTel()) == 0) && (strcmp(Password, tmp.GetPassword()) == 0) && (type == tmp.GetUserType()))
return true;
}
return false;
}
bool UsersManager::FindUserByTel(char* Tel, User& Dest) {
unsigned size = UserList.size();
for (unsigned i = 0; i < size; ++i) {
User tmp = UserList[i];
if ((strcmp(Tel, tmp.GetTel()) == 0)) {
Dest = tmp;
Dest.BookedList = tmp.BookedList;
return true;
}
}
return false;
}
void UsersManager::EditUserFlight(User& t, FlightInfo & r) {
vector<User>::iterator it;
for (it = UserList.begin(); it != UserList.end(); ++it) {
if (strcmp(t.GetTel(), it->GetTel()) == 0)
it->BookedList.push_back(r);
}
}
void UsersManager::EditUser(User& t) {
vector<User>::iterator it;
for (it = UserList.begin(); it != UserList.end(); ++it) {
if (strcmp(t.GetTel(), it->GetTel()) == 0)
*it = t;
}
}