-
Notifications
You must be signed in to change notification settings - Fork 0
/
signup.cpp
264 lines (196 loc) · 6.83 KB
/
signup.cpp
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
#include "signup.h"
#include "main.h"
#include "Initial.h"
using namespace std;
Signup::Signup(QWidget* parent)
: QMainWindow(parent), ui(new Ui::SignupClass)
{
ui->setupUi(this);
//set validations to false (default)
Signup::id_valid = false;
Signup::name_valid = false;
Signup::password_valid = false;
Signup::age_valid = false;
Signup::governorate_valid = false;
Signup::gender_valid = false;
Signup::vaccine_valid = false;
//make the warning initially invisible
ui->name_warn->setVisible(false);
ui->password_warn->setVisible(false);
ui->age_warn->setVisible(false);
ui->ID_warn->setVisible(false);
ui->gov_warn->setVisible(false);
ui->gender_warn->setVisible(false);
ui->dose_warn->setVisible(false);
//successful note
ui->successfully->setVisible(false);
//make it initially enabled
ui->signup->setEnabled(true);
//ommiting password
ui->password->setEchoMode(QLineEdit::Password);
//buttons
connect(ui->signup, &QPushButton::clicked, this, &Signup::signup_clicked);
connect(ui->back, &QPushButton::clicked, this, &Signup::back_clicked);
//text loses focus
connect(ui->name, &QLineEdit::editingFinished, this, &Signup::name_validate);
connect(ui->name, &QLineEdit::returnPressed, this, &Signup::signup_clicked);
connect(ui->natID, &QLineEdit::editingFinished, this, &Signup::id_validate);
connect(ui->natID, &QLineEdit::returnPressed, this, &Signup::signup_clicked);
connect(ui->password, &QLineEdit::editingFinished, this, &Signup::pass_validate);
connect(ui->password, &QLineEdit::returnPressed, this, &Signup::signup_clicked);
connect(ui->age, &QLineEdit::editingFinished, this, &Signup::age_validate);
connect(ui->age, &QLineEdit::returnPressed, this, &Signup::signup_clicked);
}
Signup::~Signup()
{
delete ui;
}
//Running does not save in files
void Signup::signup_clicked() {
//read from text boxes, validate inputs, then proceed or give an error message if wrong
//validate inputs and length of nat ID
QString q_name = ui->name->text();
string name = q_name.toStdString();
QString q_natID = ui->natID->text();
string natID = q_natID.toStdString();
QString q_password = ui->password->text();
string password = q_password.toStdString();
string q_age = ui->age->text().toStdString();
QString q_governorate = ui->governorate->currentText();
string governorate = q_governorate.toStdString();
//make sure a box got checked before proceeding to not read from NULL
string gender;
if (ui->genderButtonGroup->checkedButton() != nullptr) {
ui->gender_warn->setVisible(false);
QString q_gender = ui->genderButtonGroup->checkedButton()->objectName();
if (q_gender.toStdString() == "male")
gender = "male";
else {
gender = "female";
}
gender_valid = true;
}
else {
ui->gender_warn->setVisible(true);
gender_valid = false;
qDebug() << "Gender Required Field!";
}
//returns unvaccinated or vaccinated_1 or vaccinated_2
int vaccine = 0;
if (ui->vaccineButtonGroup->checkedButton() != nullptr) { //make sure not NULL
ui->dose_warn->setVisible(false);
QString q_vaccine = ui->vaccineButtonGroup->checkedButton()->objectName();
if (q_vaccine.toStdString() == "unvaccinated")
vaccine = 0;
else if (q_vaccine.toStdString() == "vaccinated_1")
vaccine = 1;
else {
vaccine = 2;
}
vaccine_valid = true;
}
else {
ui->dose_warn->setVisible(true);
vaccine_valid = false;
qDebug() << "Vaccine is Required Field!";
}
//validate, in case of pressing sign up without clicking (the edit finished won't be triggered so we manually vlaidate)
name_validate();
id_validate();
age_validate();
pass_validate();
gov_validate();
//if everything is valid then create user
if (id_valid && name_valid && age_valid && password_valid && gender_valid && vaccine_valid && governorate_valid) {
ui->signup->setEnabled(false);
ui->successfully->setVisible(true);
qDebug() << "User Created";
User user(password, name, stoi(q_age), gender, governorate, vaccine);
hashKeysOrdered.push_back(natID);
userHash[natID] = user;
//insert him into either queue waiting list or linked list, depending on his dose
if (vaccine == 0)
not_vaccinated.push(natID);
else
vaccinated.insert(natID, userHash);
//next page
}
}
void Signup::back_clicked() {
Initial* initial = new Initial();
initial->show();
this->close();
}
//validation functions
void Signup::name_validate() {
string name = ui->name->text().toStdString();
if (name == "") {
ui->name_warn->setVisible(true);
name_valid = false;
qDebug() << "Name Required Field!";
}
else {
name_valid = true;
ui->name_warn->setVisible(false);
}
}
void Signup::id_validate() {
string natID = ui->natID->text().toStdString();
//validate the data, insert the data into a new object of User in the hash table
if (userHash.count(natID) || natID.length() != 14 || !isDigitString(natID)) {
id_valid = false;
ui->ID_warn->setVisible(true);
qDebug() << "Invalid ID";
}
else {
id_valid = true;
ui->ID_warn->setVisible(false);
}
}
void Signup::pass_validate() {
string password = ui->password->text().toStdString();
if (password.length() < 8) {
password_valid = false;
ui->password_warn->setVisible(true);
qDebug() << "Password must be at least 8 characters";
}
else {
password_valid = true;
ui->password_warn->setVisible(false);
}
}
void Signup::age_validate() {
string q_age = ui->age->text().toStdString();
int age;
//validate the data, insert the data into a new object of User in the hash table
if (isDigitString(q_age)) {
age = stoi(q_age);
if (age <= 0) {
age_valid = false;
ui->age_warn->setVisible(true);
qDebug() << "Age must be more than 0";
}
//valid age
else {
age_valid = true;
ui->age_warn->setVisible(false);
}
}
else {
age_valid = false;
ui->age_warn->setVisible(true);
qDebug() << "Age must be a number!";
}
}
void Signup::gov_validate() {
string governorate = ui->governorate->currentText().toStdString();;
if (governorate == "-") {
governorate_valid = false;
ui->gov_warn->setVisible(true);
qDebug() << "Governorate Required Field!";
}
else {
governorate_valid = true;
ui->gov_warn->setVisible(false);
}
}