-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
184 lines (126 loc) · 3.81 KB
/
Main.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
#include "main.h"
#include "Initial.h"
int main(int argc, char *argv[])
{
string filename = "users.txt";
QApplication a(argc, argv);
load_data(userHash, filename);
loggedInUserID = "";
handlingDoses();
//print values in hash for debugging
for (auto & key : hashKeysOrdered) {
qDebug() << "ID: " << key << ", Password: " << userHash[key].password << ", Name: " << userHash[key].name << ", Age: " << userHash[key].age << ", Gender: " << userHash[key].gender << ", Governorate: " << userHash[key].governorate << ", Dose: " << userHash[key].dose << "\n";
}
Initial inital;
inital.show();
//Qt Application main event loop
int exitCode = a.exec();
//PRINT VALUES AFTER PROGRAM ENDS FOR DEBUGGING
//display linked list for debugging
vaccinated.display();
//display queue for debugging
queue<string> copy_queue = not_vaccinated; // Make a copy of the queue
qDebug() << "Contents of the queue: ";
while (!copy_queue.empty()) {
qDebug() << copy_queue.front() << " ";
copy_queue.pop();
}
qDebug() << "\n";
//print values in hash for debugging
for (auto& key : hashKeysOrdered) {
qDebug() << "ID: " << key << ", Password: " << userHash[key].password << ", Name: " << userHash[key].name << ", Age: " << userHash[key].age << ", Gender: " << userHash[key].gender << ", Governorate: " << userHash[key].governorate << ", Dose: " << userHash[key].dose << "\n";
}
save_data(userHash, filename);
return exitCode;
}
void save_data(unordered_map<string, User>& userHash, string filename) {
ofstream outfile(filename);
for (const auto& key : hashKeysOrdered) {
outfile << key << "\n";
outfile << userHash[key].password << "\n";
outfile << userHash[key].name << "\n";
outfile << userHash[key].age << "\n";
outfile << userHash[key].gender << "\n";
outfile << userHash[key].governorate << "\n";
outfile << userHash[key].dose << "\n";
}
}
void load_data(unordered_map<string, User>& userHash, string filename) {
ifstream infile(filename);
string line;
string id, password, name, ageStr, gender, governorate, doseStr;
while (getline(infile, line)) {
id = trim(line);
getline(infile, line);
password = trim(line);
getline(infile, line);
name = trim(line);
getline(infile, line);
ageStr = trim(line);
int age = stoi(ageStr);
getline(infile, line);
gender = trim(line);
getline(infile, line);
governorate = trim(line);
getline(infile, line);
doseStr = trim(line);
int dose = stoi(doseStr);
//create user and store him into hash
User user(password, name, age, gender, governorate, dose);
hashKeysOrdered.push_back(id);
userHash[id] = user;
}
}
//gets put into queue waiting list if unvaccinated and in a sorted linked list if vaccinated
void handlingDoses()
{
for (const auto& key : hashKeysOrdered) {
if(userHash[key].dose == 0)
not_vaccinated.push(key);
else
vaccinated.insert(key, userHash);
}
}
void deleteUser(string natID)
{
if (userHash.count(natID) == 0) return;
if (userHash[natID].dose == 0) {
queue <string> copyqueue;
while (!not_vaccinated.empty())
{
if (natID != not_vaccinated.front())
copyqueue.push(not_vaccinated.front());
not_vaccinated.pop();
}
while (!copyqueue.empty())
{
not_vaccinated.push(copyqueue.front());
copyqueue.pop();
}
}
else {
vaccinated.deleteAt(natID);
}
hashKeysOrdered.erase(remove(hashKeysOrdered.begin(), hashKeysOrdered.end(), natID), hashKeysOrdered.end());
userHash.erase(natID);
}
bool isDigitString(const string& str) {
if (str.length() == 0) return false;
for (char c : str) {
if (!isdigit(c)) {
return false;
}
}
return true;
}
string trim(const std::string& s) {
auto start = s.begin();
while (start != s.end() && isspace(*start)) {
++start;
}
auto end = s.end();
while (end != start && isspace(*(end - 1))) {
--end;
}
return string(start, end);
}