-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
748 lines (727 loc) · 19.8 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdexcept>
// MySQL Libraries
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
using namespace std;
// Helper files
#include "basic.cpp"
#include "models.cpp"
// Config files
#include "config.cpp"
class DatabaseConn
{
private:
sql::Driver *driver;
public:
sql::Connection *con;
DatabaseConn(string url, string user, string pass, string database)
{
driver = get_driver_instance();
con = driver->connect(url, user, pass);
con->setSchema(database);
}
~DatabaseConn()
{
delete con;
}
};
class UserTable
{
private:
sql::Connection *conn;
public:
UserTable(DatabaseConn *db)
{
this->conn = db->con;
}
vector<User> getAllUsers()
{
vector<User> u;
try
{
sql::PreparedStatement *pstmt = conn->prepareStatement("SELECT * FROM users");
sql::ResultSet *rs = pstmt->executeQuery();
while (rs->next())
{
string username = rs->getString("username");
string password = rs->getString("password");
string record = rs->getString("record");
int id = rs->getInt("id");
string role = rs->getString("role");
if(role=="manager") continue;
int fine = rs->getInt("fine");
User u1(username, password, role);
if (password == pass)
u1.authorise();
u1.setId(id);
u1.setFine(fine);
u1.setRecord(record);
u.push_back(u1);
}
}
catch (const sql::SQLException &e)
{
cerr << "SQL error: " << e.what() << '\n';
}
return u;
}
User getUserByUsername(string username, string password)
{
try
{
sql::PreparedStatement *pstmt = conn->prepareStatement("SELECT * FROM users WHERE username = ?");
pstmt->setString(1, username);
sql::ResultSet *rs = pstmt->executeQuery();
if (rs->next())
{
string pass = rs->getString("password");
int id = rs->getInt("id");
string role = rs->getString("role");
string record = rs->getString("record");
int fine = rs->getInt("fine");
User u1(username, password, role);
if (password == pass)
u1.authorise();
u1.setId(id);
u1.setFine(fine);
u1.setRecord(record);
return u1;
}
}
catch (const sql::SQLException &e)
{
cerr << "SQL error: " << e.what() << '\n';
}
User defaultUser("", "", "");
return defaultUser;
}
bool saveUser(User u)
{
try
{
sql::PreparedStatement *pstmt = conn->prepareStatement("UPDATE users SET fine = ?,record=? WHERE username = ?");
pstmt->setInt(1, u.fine);
pstmt->setString(2,u.record);
pstmt->setString(3, u.username);
pstmt->execute();
delete pstmt;
return true;
}
catch (const sql::SQLException &e)
{
cerr << "SQL error: " << e.what() << '\n';
return false;
}
}
bool changePass(string username, string password)
{
try
{
sql::PreparedStatement *pstmt = conn->prepareStatement("UPDATE users SET password = ? WHERE username = ?");
pstmt->setString(1, password);
pstmt->setString(2, username);
pstmt->execute();
delete pstmt;
return true;
}
catch (const sql::SQLException &e)
{
cerr << "SQL Error: " << e.what() << '\n';
return false;
}
}
bool addUser(string username, string password, string role)
{
try
{
sql::PreparedStatement *pstmt = conn->prepareStatement("INSERT INTO users(username,password,role) VALUES (?,?,?);");
pstmt->setString(1, username);
pstmt->setString(2, password);
pstmt->setString(3, role);
pstmt->execute();
delete pstmt;
return true;
}
catch (const sql::SQLException &e)
{
cerr << "SQL Error: " << e.what() << '\n';
return false;
}
}
bool deleteUser(string username)
{
try
{
sql::PreparedStatement *pstmt = conn->prepareStatement("DELETE FROM users where username = ?;");
pstmt->setString(1, username);
pstmt->execute();
delete pstmt;
return true;
}
catch (const sql::SQLException &e)
{
std::cerr << "SQL Error: " << e.what() << '\n';
return false;
}
}
};
class CarTable
{
private:
sql::Connection *conn;
public:
CarTable(DatabaseConn *db)
{
this->conn = db->con;
}
bool addCar(string model, string health, int price)
{
try
{
sql::PreparedStatement *pstmt = conn->prepareStatement("INSERT INTO cars(model,health,price) VALUES (?,?,?);");
pstmt->setString(1, model);
pstmt->setString(2, health);
pstmt->setUInt(3, price);
pstmt->execute();
delete pstmt;
return true;
}
catch (const sql::SQLException &e)
{
std::cerr << "SQL Error: " << e.what() << '\n';
return false;
}
}
vector<Car> showCars()
{
vector<Car> result;
try
{
sql::PreparedStatement *pt = conn->prepareStatement("SELECT * FROM cars;");
sql::ResultSet *r = pt->executeQuery();
while (r->next())
{
int id = r->getInt("id");
string model = r->getString("model");
string health = r->getString("health");
string availability = r->getString("availability");
int price = r->getInt("price");
string due_date = r->getString("due_date");
Car temp(model, health, price);
temp.setAvailability(availability);
temp.setDate(due_date);
temp.setId(id);
result.push_back(temp);
}
delete r;
delete pt;
}
catch (const sql::SQLException &e)
{
std::cerr << e.what() << '\n';
}
return result;
}
bool deleteCar(int id)
{
try
{
sql::PreparedStatement *pt = conn->prepareStatement("DELETE FROM cars WHERE id = ?;");
pt->setInt(1, id);
pt->execute();
delete pt;
return true;
}
catch (const sql::SQLException &e)
{
std::cerr << "SQL Error: " << e.what() << '\n';
return false;
}
}
Car getCar(int id)
{
try
{
sql::PreparedStatement *pt = conn->prepareStatement("SELECT * FROM cars WHERE id = ?;");
pt->setInt(1, id);
sql::ResultSet *rs = pt->executeQuery();
if (rs->next())
{
string model, health, availability, due_date;
int id, price;
model = rs->getString("model");
health = rs->getString("health");
availability = rs->getString("availability");
id = rs->getInt("id");
price = rs->getInt("price");
due_date = rs->getString("due_date");
Car currCar(model, health, price);
currCar.setId(id);
currCar.setAvailability(availability);
currCar.setDate(due_date);
return currCar;
}
delete pt;
}
catch (const sql::SQLException &e)
{
std::cerr << "SQL Error: " << e.what() << '\n';
}
Car cr("", "", 0);
return cr;
}
bool updateCarDetails(Car cr)
{
try
{
sql::PreparedStatement *pt = conn->prepareStatement("UPDATE cars SET model = ?, health = ?, price = ?,availability = ? , due_date = ? WHERE id = ?;");
pt->setString(1, cr.model);
pt->setString(2, cr.health);
pt->setInt(3, cr.price);
pt->setString(4, cr.availability);
if (cr.due_date == "")
{
pt->setNull(5, sql::DataType::DATE);
}
else
{
pt->setString(5, cr.due_date);
}
pt->setInt(6, cr.id);
pt->execute();
delete pt;
return true;
}
catch (const sql::SQLException &e)
{
std::cerr << "SQL Error: " << e.what() << '\n';
return false;
}
}
bool rentCar(int id, string username, string due_date)
{
try
{
sql::PreparedStatement *pt = conn->prepareStatement("UPDATE cars SET availability = ?, due_date = ? WHERE id = ?;");
pt->setString(1, username);
pt->setString(2, due_date);
pt->setInt(3, id);
pt->execute();
delete pt;
return true;
}
catch (const sql::SQLException &e)
{
std::cerr << "SQL Error: " << e.what() << '\n';
return false;
}
}
};
class Manager
{
private:
UserTable *tb;
CarTable *cartb;
public:
Manager(UserTable *tb, CarTable *car)
{
this->tb = tb;
this->cartb = car;
}
void UserRegister()
{
string role = SelectRole();
string username, password;
cout << "Enter Username: ";
cin >> username;
cout << "Enter Password: ";
cin >> password;
tb->addUser(username, password, role);
}
void DeleteUser()
{
string username;
cout << "Enter Username to delete: ";
cin >> username;
cout << "\n";
if ((tb->deleteUser(username)))
{
cout << "User " << username << " successfully deleted!!\n";
}
else
{
cout << "Error deleting user\n";
}
}
void AddCar()
{
string model, health;
int price;
cout << "Enter Model Name: ";
cin >> model;
cout << "\nEnter Health(good,moderate,bad): ";
cin >> health;
cout << "\nEnter Price: ";
cin >> price;
cout << "\n";
cartb->addCar(model, health, price);
}
void SeeCars()
{
vector<Car> v1 = cartb->showCars();
for (auto i : v1)
{
printDashes(20);
PrintCarDetails(i);
}
printDashes(20);
}
void DeleteCarById()
{
int id;
cout << "Enter id: ";
cin >> id;
cout << "\n";
if ((cartb->deleteCar(id)))
{
cout << "Deleted Successfully!\n";
}
else
{
cout << "Error occured\n";
}
}
void UpdateCar()
{
cout << "Enter Id of the car: ";
int id;
cin >> id;
cout << "\n";
Car currCar = cartb->getCar(id);
printDashes(20);
PrintCarDetails(currCar);
printDashes(20);
int n = CarUpdate();
switch (n)
{
case 1:
cout << "Enter Health(good,moderate,bad): ";
cin >> currCar.health;
break;
case 2:
cout << "Enter Price: ";
cin >> currCar.price;
break;
default:
break;
}
cartb->updateCarDetails(currCar);
cout << "\nUpdated!!!\n\n";
}
void RentCar()
{
cout << "Enter Id of the car: ";
int id;
cin >> id;
Car currCar = cartb->getCar(id);
if (currCar.availability != "admin")
{
cout << "==> Car Not Available!! Rented By: " << currCar.availability << "\n";
return;
}
cout << "\nEnter Username: ";
string username;
cin >> username;
User u = tb->getUserByUsername(username, "");
if (u.username == "")
{
cout << "\n\nUsername doesn't exist!!\n\n";
return;
}
cout << "Enter Due-Date(YYYY-MM-DD): ";
cin >> currCar.due_date;
currCar.availability = username;
cartb->updateCarDetails(currCar);
cout << "\nRented Successfully!!\n\n";
}
void returnCar()
{
cout << "Enter Id of the car: ";
int id;
cin >> id;
Car currCar = cartb->getCar(id);
if (currCar.availability == "admin")
{
cout << "==> Car is Already in store\n";
return;
}
currCar.availability = "admin";
currCar.due_date = "";
cartb->updateCarDetails(currCar);
cout << "Car Returned Successfully!!\n\n";
}
void SeeAllUsers(){
vector<User> uu = tb->getAllUsers();
if(uu.size()==0) return;
for(auto i: uu){
printDashes(20);
PrintUsers(i);
}
printDashes(20);
}
void UpdateUser(){
cout<<"Enter Username: "; string username;cin>>username;
auto u = tb->getUserByUsername(username,"");
if(u.username == "") {
cout<<"No user found\n\n";
return;
}
string s;
cout<<"\nUser Details: ";PrintUsers(u);printDashes(20);cout<<endl;
int i; cout<<"1. Update Fine: \n2. Update Record:\n Choose One: ";cin>>i;
switch (i)
{
case 1:
cout<<"Enter fine: ";cin>>i; u.setFine(i);
break;
case 2:
cout<<"Enter Record(good,moderate,bad): ";cin>>s;u.setRecord(s);
break;
default:
break;
}
tb->saveUser(u);
cout<<"User updated !!!"<<endl;
}
};
class EmployeeCustomer
{
private:
UserTable *utb;
CarTable *cartb;
User *user;
float priceFactor = 1;
public:
EmployeeCustomer(UserTable *tb, CarTable *car, User *u)
{
this->utb = tb;
this->cartb = car;
this->user = u;
if(u->role=="employee"){
priceFactor = employeedis;
}
}
void changePass()
{
string password;
cout << "Enter Password: ";
cin >> password;
cout << "\n";
if (utb->changePass(this->user->username, password))
{
cout << "Password Changed Successfully\n\n";
}
else
{
cout << "Error Changing pass\n\n";
}
}
void seeRentedCars()
{
cout << "Your Rented cars:\n\n";
vector<Car> v1 = cartb->showCars();
int t = 0;
for (auto i : v1)
{
i.price = priceFactor * i.price;
if (i.availability == user->username)
{
printDashes(20);
PrintCarDetails(i);
t++;
}
}
printDashes(20);
if (t == 0)
{
cout << "0 Rented Cars\n";
}
printDashes(20);
}
void seeAvailableCars()
{
vector<Car> v1 = cartb->showCars();
int t = 0;
for (auto i : v1)
{
if (i.availability == "admin")
{
i.price = priceFactor * i.price;
printDashes(20);
PrintCarDetails(i);
t++;
}
}
printDashes(20);
if (t == 0)
{
cout << "0 Available cars\n";
}
printDashes(20);
}
void rentCar()
{
if (user->record == "bad")
{
cout << "You record is bad sorry!!\n\n";
return;
}
cout << "List of available cars:\n";
this->seeAvailableCars();
int id;
cout << "Enter id of Car you want to Rent: ";
cin >> id;
Car c = cartb->getCar(id);
if (c.availability == "admin")
{
c.availability = this->user->username;
string date;
cout << "Enter due date(YYYY-MM-DD): ";
cin >> date;
c.due_date = date;
cartb->updateCarDetails(c);
cout << "Car rented successfully\n";
}
else
{
cout << "Car not available\n"
<< endl;
}
}
void returnCar()
{
this->seeRentedCars();
cout << "Enter id of car to return: ";
int id;
cin >> id;
auto c = cartb->getCar(id);
if (c.availability == this->user->username)
{
int fine = dateDiffInDays(c.due_date) * finePerDay;
this->user->fine += fine;
utb->saveUser(*(this->user));
c.due_date = "";
c.availability = "admin";
cartb->updateCarDetails(c);
cout << "Car returned successfully\n";
this->seeFine();
}
else
{
cout << "This car is not with you\n";
}
}
void seeFine()
{
auto u = utb->getUserByUsername(user->username, user->password);
cout << "==> Your fine is " << u.fine << "\n\n";
}
};
int main(int argc, const char **argv)
{
today = getCurrentDate();
cout << "Today is " << today << endl;
DatabaseConn RentalDB(host, user, pass, database);
UserTable users(&RentalDB);
CarTable cars(&RentalDB);
auto creds = handleSignIn();
User currUser = users.getUserByUsername(creds.first, creds.second);
if (!currUser.isAuthorised)
{
cout << "Error Signing You!! \n\n";
return 0;
}
cout << "Signed in Successfully!!\n\n"
<< "Your Role: " << currUser.role << "\n\n";
while (true)
{
if (currUser.role == "manager")
{
Manager mang(&users, &cars);
int opt = ManagerOptions();
switch (opt)
{
case 1:
mang.UserRegister();
break;
case 2:
mang.DeleteUser();
break;
case 3:
mang.UpdateUser();
break;
case 4:
mang.AddCar();
break;
case 5:
mang.UpdateCar();
break;
case 6:
mang.DeleteCarById();
break;
case 7:
mang.SeeCars();
break;
case 8:
mang.RentCar();
break;
case 9:
mang.returnCar();
break;
case 10:
mang.SeeAllUsers();
break;
default:
return 0;
break;
}
}
else if (currUser.role == "employee" || currUser.role == "customer")
{
EmployeeCustomer employee(&users, &cars, &currUser);
int opt = Options();
switch (opt)
{
case 1:
employee.changePass();
break;
case 2:
employee.seeRentedCars();
break;
case 3:
employee.seeAvailableCars();
break;
case 4:
employee.rentCar();
break;
case 5:
employee.returnCar();
break;
case 6:
employee.seeFine();
break;
default:
return 0;
break;
}
}
}
return 0;
}