-
Notifications
You must be signed in to change notification settings - Fork 6
/
database.cpp
327 lines (271 loc) · 8.88 KB
/
database.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#ifndef database_cpp
#define database_cpp
#include<bits/stdc++.h>
using namespace std;
#include "customer.cpp"
#include "shopkeeper.cpp"
#include "moderator.cpp"
#include "network.cpp"
ulli number_of_shops;
Customer get_customer(string email)
{
Customer temp;
temp.name[0]='\0';
email="database/customer_data/"+email+".ooad";
fstream file(email.c_str());
if(!file)
{
cout<<"stuck here";
return temp;
}
file.read((char *)&temp,sizeof(Customer));
file.close();
return temp;
}
Shopkeeper get_shopkeeper(string email)
{
Shopkeeper temp;
temp.name[0]='\0';
email="database/shopkeeper_data/"+email+".ooad";
fstream file(email.c_str());
if(!file)
{
return temp;
}
file.read((char *)&temp,sizeof(Shopkeeper));
file.close();
return temp;
}
void register_customer(Customer temp,string email)
{
string path="database/customer_data/"+email+".ooad";
ofstream file(path.c_str());
file.write((char*)&temp,sizeof(Customer));
file.close();
fstream emails("database/customer_data/emails.txt",ios::app);
emails<<email<<"\n";
emails.close();
}
void register_shopkeeper(Shopkeeper temp,string email)
{
string path="database/shopkeeper_data/"+email+".ooad";
ofstream file(path.c_str());
file.write((char*)&temp,sizeof(Shopkeeper));
file.close();
fstream emails("database/shopkeeper_data/emails.txt",ios::app);
emails<<email<<"\n";
emails.close();
}
bool is_customer(string email)
{
string path = "database/customer_data/"+email+".ooad";
fstream file1(path.c_str());
if(file1)
{
file1.close();
return true;
}
return false;
}
bool is_shopkeeper(string email)
{
string path="database/shopkeeper_data/"+email+".ooad";
fstream file2(path.c_str());
if(file2)
{
file2.close();
return true;
}
return false;
}
bool is_moderator(string email)
{
if(strcmp(Moderator::email,email.c_str())==0)
return true;
return false;
}
void increase_shop()
{
fstream file1("database/shopkeeper_data/number_of_shops.num",ios::in);
ulli number;
file1>>number;
number++;
file1.close();
fstream file2("database/shopkeeper_data/number_of_shops.num",ios::out);
file2<<number;
file2.close();
}
ulli get_number_of_shops()
{
fstream file1("database/shopkeeper_data/number_of_shops.num",ios::in);
ulli number;
file1>>number;
file1.close();
return number;
}
int register_shop(Shop shop)
{
string id=Utilities::ulli_to_string(shop.ID);
string path="database/shop_data/"+id+".shop";
ofstream file(path.c_str());
file.write((char*)&shop,sizeof(Shop));
file.close();
string path2="database/shop_data/"+id+".num";
ofstream file2(path2.c_str());
file2<<0;
file2.close();
}
int register_user(string email)
{
cout<<"> Entered email not found in our database. Do you want to register (y/n)? ";
char answer;
cin>>answer;
if(answer=='n')
return -1;
if(answer!='y')
cout<<"> No a valid choice terminating\n";
cout<<"> To register as Customer press (c) and to register as shopkeeper press (s) ";
cin>>answer;
cout<<"> Terms and Conditions \n";
Utilities::loading(40);
switch(answer)
{
case 'c':
{
Customer customer;
fstream file("database/customer_data/tandc.txt");
string temp;
cout<<"> Terms and Conditions are as following\n";
char ch;
// printing tandc
while(file.get(ch))
cout<<ch;
file.close();
cout<<"\n> I, as a Customer of CC Basket, I hereby declare that I have gone through above terms and conditions and accept them, I also pledge to follow them. I also understand that non-fullfillment of any such issue can lead to legal action.(Yes->y/No->n) ";
cin>>ch;
if(ch=='n')
return -1;
if(ch!='y')
{
cout<<"> Invalid input\n";
return -1;
}
if(customer.get_details(email)==-1)
return -1;
// verification process before entering into database
string temp_name(customer.name);
string temp_email(customer.email);
string temp_phone(customer.contact_number);
cout<<"> Starting email verification process\n";
// verifing email
string OTP=Network::mail_otp(temp_name,temp_email);
if(OTP.length()==0)
return -1;
cout<<"> Enter OTP recieved on your email :: ";
char temp_otp[11];
cin>>temp_otp;
if(strcmp(temp_otp,OTP.c_str())!=0)
{
cout<<"> Worng OTP entered aborting process\n";
return -1;
}
else
{
cout<<"> OTP accepeted\n";
}
cout<<"> Starting mobile verification\n";
// sending OTP
OTP=Network::sms_otp(temp_name,temp_phone);
if(OTP.length()==0)
return -1;
cout<<"> Enter OTP recieved on your phone number :: ";
temp_otp[11];
cin>>temp_otp;
if(strcmp(temp_otp,OTP.c_str())!=0)
{
cout<<"> Worng OTP entered aborting process\n";
return -1;
}
else
{
cout<<"> OTP accepeted\n";
}
register_customer(customer,email);
cout<<"> Customer Registration Completed \n";
return 1;
}
break;
case 's':
{
Shopkeeper shopkeeper(get_number_of_shops());
fstream file("database/shopkeeper_data/tandc.txt");
string temp;
cout<<"> Terms and Conditions are as following\n";
char ch;
while(file.get(ch))
cout<<ch;
file.close();
cout<<"\n> I, as a Shopkeeper of a shop under fbay network, I hereby declare that I have gone through above terms and conditions and accept them, I also pledge to follow them. I also understand that non-fullfillment of any such issue can lead to legal action.(Yes->y/No->n) ";
cin>>ch;
if(ch=='n')
return -1;
if(shopkeeper.get_details(email)==-1)
return -1;
// verification process before entering into database
string temp_name(shopkeeper.name);
string temp_email(shopkeeper.email);
string temp_phone(shopkeeper.contact_number);
cout<<"> Starting email verification process\n";
// verifing email
string OTP=Network::mail_otp(temp_name,temp_email);
if(OTP.length()==0)
return -1;
cout<<"> Enter OTP recieved on your email :: ";
char temp_otp[11];
cin>>temp_otp;
if(strcmp(temp_otp,OTP.c_str())!=0)
{
cout<<"> Worng OTP entered aborting process\n";
return -1;
}
else
{
cout<<"> OTP accepeted\n";
}
cout<<"> Starting mobile verification\n";
// sending OTP
OTP=Network::sms_otp(temp_name,temp_phone);
if(OTP.length()==0)
return -1;
cout<<"> Enter OTP recieved on your phone number :: ";
temp_otp[11];
cin>>temp_otp;
if(strcmp(temp_otp,OTP.c_str())!=0)
{
cout<<"> Worng OTP entered aborting process\n";
return -1;
}
else
{
cout<<"> OTP accepeted\n";
}
Shop shop(get_number_of_shops());
if(shop.get_details(shopkeeper.email,shopkeeper.name,shopkeeper.shop_category)==-1)
return -1;
register_shopkeeper(shopkeeper,email);
register_shop(shop);
increase_shop();
cout<<"> Shopkeeper Registration Completed \n";
cout<<"> You need to login to proceed!\n";
return 2;
}
break;
default:
{
cout<<"> Not a valid input\n";
return 3;
}
break;
}
}
#endif