forked from CodeWithKartik/BookMyShow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserDatabase.cpp
152 lines (148 loc) · 5.24 KB
/
UserDatabase.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
#include "UserDatabase.h"
#include "Member.h"
#include <cstring>
#include <math.h>
UserDatabase::UserDatabase() {
//Maybe make this a singleton object
database = new BMSUsers<User>();
user_count = 0;
}
UserDatabase::UserDatabase(const UserDatabase &cpy) {
database = new BMSUsers<User>(*(cpy.GetDatabase()));
}
UserDatabase::~UserDatabase() {
delete database;
}
void UserDatabase::RemoveUser(const char *username) {
BMSLink<User> *t = database->GetStart();
while (t != NULL) {
if (strcmp((t->GetLink())->GetName(), username) == 0) {
database->SetStart(database->DeleteLink(t));
user_count--;
break;
}
t = t->GetNext();
}
}
void UserDatabase::ModifyUser(const char *username, char *new_psswd) {
BMSLink<User> *t = database->GetStart();
while (t != NULL) {
if (strcmp((t->GetLink())->GetName(), username) == 0) {
(t->GetLink())->SetPsswd(new_psswd);
break;
}
t = t->GetNext();
}
}
User *UserDatabase::FindUser(User *usr) {
BMSLink<User> *t = database->GetStart();
while (t != NULL) {
if (strcmp((t->GetLink())->GetName(), usr->GetName()) == 0)
return t->GetLink();
t = t->GetNext();
}
return NULL;
}
User *UserDatabase::AddUser(User *usr) {
User *tmp = FindUser(usr);
if (tmp == NULL) {
BMSLink<User> *t = new BMSLink<User>(usr);
t->GetLink()->SetUserID(user_count);
database->SetStart(database->InsertLink(t));
tmp = FindUser(usr);
user_count++;
}else {
if (strcmp(tmp->GetPsswd(), usr->GetPsswd()) != 0) {
std::cout<<"Sorry: The password is incorrect. Please try again!\n";
return NULL;
}
}
return tmp;
}
static unsigned long int char_to_count_converter(std::vector<char> char_num) {
unsigned long int ct = 0;
int ind = char_num.size() - 1;
const int char_to_int = 48;
while (ind >= 0) {
ct += (char_num[ind] - char_to_int)*pow(10, char_num.size() - ind - 1);
ind--;
}
return ct;
}
void UserDatabase::PopulateDatabase(std::vector<char> file_array) {
if (file_array.size() != 0) {
int user_counts_ind = 73;
std::vector<char> t_ct;
register int kct = 0;
while (file_array[user_counts_ind+kct] != '\n') {
t_ct.push_back(file_array[user_counts_ind+kct]);
kct++;
}
register unsigned long int ct = char_to_count_converter(t_ct);
int str_ind = user_counts_ind + kct + 2;
int utype_ind = 42;
int uind_ind = 57;
int details_ind = 70;
int title_shift = 33;
int reward_points_offset = 16;
int a = str_ind;
register int uid = 0;
char uname[100], psswd[100];
for (register int i = 0; i < ct; i++) {
if (file_array[a+utype_ind] == 'M') {
register int kt = 0, kp = 0, ku = 0, krw = 0;
while (file_array[a+uind_ind+ku] != '\n')
ku++;
ku--;
while (file_array[a+details_ind+ku+kt] != '\n') {
uname[kt] = file_array[a+details_ind+ku+kt];
kt++;
}
uname[kt] = '\0';
while (file_array[a+details_ind+ku+kt+11+kp] != '\n') {
psswd[kp] = file_array[a+details_ind+ku+kt+11+kp];
kp++;
}
psswd[kp] = '\0';
std::vector<char> rd_pts;
while (file_array[a+details_ind+ku+kt+11+kp+reward_points_offset+krw] != '\n') {
rd_pts.push_back(file_array[a+details_ind+ku+kt+11+kp+reward_points_offset+krw]);
krw++;
}
int reward_pts = char_to_count_converter(rd_pts);
a = a+details_ind+ku+kt+11+kp+reward_points_offset+krw+title_shift;
Member *new_member = new Member(uname, psswd);
new_member->SetRewardPoints(reward_pts);
AddUser(new_member);
} else {
register int kt = 0, kp = 0, ku = 0;
while (file_array[a+uind_ind+ku] != '\n')
ku++;
ku--;
while (file_array[a+details_ind+ku+kt-1] != '\n') {
uname[kt] = file_array[a+details_ind+ku+kt-1];
kt++;
}
uname[kt] = '\0';
while (file_array[a+details_ind+ku+kt+11+kp-1] != '\n') {
psswd[kp] = file_array[a+details_ind+ku+kt+11+kp-1];
kp++;
}
psswd[kp] = '\0';
a = a+details_ind-1+ku+kt+11+kp+title_shift;
Admin *admin = new Admin(psswd);
AddUser(admin);
}
}
}
}
void Admin::UpdateUserDatabase(const char *username, char *new_psswd, UserDatabase &obj) {
obj.ModifyUser(username, new_psswd);
}
std::ostream& operator<<(std::ostream& os, const UserDatabase &udb) {
os<<"--------------------USER DATABASE STARTS--------------------\n";
os<<"User count: "<<udb.GetUserCount()<<"\n\n";
os<<udb.GetDatabase()<<"\n";
os<<"--------------------USER DATABASE ENDS----------------------\n";
return os;
}