-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
404 lines (329 loc) · 13.5 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
#include <iostream>
#include <string> // Using the string header file to enable us use the string function
#include <cstdlib>
#include <ctime>
//Using MYSQL to create the database
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
using namespace sql;
class Vehicle {
protected:
string registration_number;
public:
string get_registration_number() { return registration_number; }
void renewReg(string expiry_date_str){
expiry_date_str = expiry_date_str;
cout << "Registration renewed. New expiry date: " << expiry_date << endl;
}
void display_menu() { // Menu display for the type of car
cout<< "1.Type Cruiser"<< endl;
cout<< "2.Type Sedan, Minibuses, and Cross-country Vehicles"<< endl;
cout<< "3.Type Transport,and Medium Product Vehicles"<< endl;
cout<< "4.Type Transport, Mentors, and Hefty Product Vehicles"<< endl;
cout<< "5.Type Heavy Type Machinery"<< endl;
cout<< "6.Type Trailers and Trucks"<< endl;
}
virtual void generate_number_plate() = 0; // using a virtual function since it is implemented differently with different parameters
};
class TypeA : public Vehicle { // first sub-class that uses an array of letters and random numbers to generate it's unique number plate
private:
static const char letters[];
public:
void generate_number_plate() override {
char first_letter = letters[rand() % 3];
char second_letter = letters[rand() % 3];
int number = rand() % 10000;
registration_number = string(1, first_letter) + string(1, second_letter) + to_string(number);
}
};
const char TypeA::letters[] = {'A', 'B', 'C'}; // Using an array with specific members to help generate the license plate
class TypeB : public Vehicle {
private:
static const char letters[];
public:
void generate_number_plate() override {
char first_letter = letters[rand() % 3]; //Randomly selecting a member of the array for the license plate
char second_letter = letters[rand() % 3];
int number = rand() % 10000; // Randomy selecting a number to add to the number plate
registration_number = string(1, first_letter) + string(1, second_letter) + to_string(number); //Presentation and storing of license plate
}
};
const char TypeB::letters[] = {'D', 'E', 'F'};
class TypeC : public Vehicle {
private:
static const char letters[];
public:
void generate_number_plate() override {
char first_letter = letters[rand() % 3];
char second_letter = letters[rand() % 3];
int number = rand() % 10000;
registration_number = string(1, first_letter) + string(1, second_letter) + to_string(number);
}
};
const char TypeC::letters[] = {'G', 'H', 'I'};
class TypeD : public Vehicle {
private:
static const char letters[];
public:
void generate_number_plate() override {
char first_letter = letters[rand() % 3];
char second_letter = letters[rand() % 3];
int number = rand() % 10000;
registration_number = string(1, first_letter) + string(1, second_letter) + to_string(number);
}
};
const char TypeD::letters[] = {'J', 'K', 'L'};
class TypeE : public Vehicle {
private:
static const char letters[];
public:
void generate_number_plate() override {
char first_letter = letters[rand() % 3];
char second_letter = letters[rand() % 3];
int number = rand() % 10000;
registration_number = string(1, first_letter) + string(1, second_letter) + to_string(number);
}
};
const char TypeE::letters[] = {'M', 'N', 'O'};
class TypeF : public Vehicle {
private:
static const char letters[];
public:
void generate_number_plate() override {
char first_letter = letters[rand() % 3];
char second_letter = letters[rand() % 3];
int number = rand() % 10000;
registration_number = string(1, first_letter) + string(1, second_letter) + to_string(number);
}
};
const char TypeF::letters[] = {'P', 'Q', 'R'};
void display_info(ResultSet* res) { // MYSQL database that stores and displays the information of the users input
while (res->next()) {
cout << "Name: " << res->getString("name") << endl;
cout << "Age: " << res->getInt("age") << endl;
cout << "Gender: " << res->getString("gender") << endl;
cout << "Phone Number 1: " << res->getString("phone_number_1") << endl;
cout << "Phone Number 2: " << res->getString("phone_number_2") << endl;
cout << "ID Number: " << res->getString("id_number") << endl;
cout << "Driver's License Number: " << res->getString("drivers_license_number") << endl;
cout << "Email: " << res->getString("email") << endl;
cout << "Make: " << res->getString("make") << endl;
cout << "Model: " << res->getString("model") << endl;
cout << "Year: " << res->getInt("year") << endl;
cout << "License Plate: " << res->getString("license_plate") << endl << endl;
}
}
class Driver{
private:
string license_number;
string date_issued;
string date_expiry;
string license_type;
public:
void display_menu() { // Menu display for the type of car
cout<< "1.Type Cruiser"<< endl;
cout<< "2.Type Sedan, Minibuses, and Cross-country Vehicles"<< endl;
cout<< "3.Type Transport,and Medium Product Vehicles"<< endl;
cout<< "4.Type Transport, Mentors, and Hefty Product Vehicles"<< endl;
cout<< "5.Type Heavy Type Machinery"<< endl;
cout<< "6.Type Trailers and Trucks"<< endl;
}
void d_license(){
try {
// Prompt user for information
string name, gender, phone_number_1, phone_number_2, id_number, drivers_license_number, email, license_number;
int age, type;
cout << "Enter your name: ";
cin.ignore();
getline(cin, name);
cout << "Enter your age: ";
cin >> age;
cout << "Enter your gender(M/F): ";
cin.ignore();
getline(cin, gender);
cout << "Enter your phone number 1: ";
cin.ignore();
getline(cin, phone_number_1);
cout << "Enter your phone number 2 (if any): ";
cin.ignore();
getline(cin, phone_number_2);
cout << "Enter your ID number: ";
cin.ignore();
getline(cin, id_number);
cout << "Enter your email: ";
cin.ignore();
getline(cin, email);
display_menu();
cout << "Enter the vehicle type: ";
cin >> type;
// Generate License number, date issued and expiry date
drivers_license_number = generate_license_number();
date_issued = generate_date();
date_expiry = generate_expiry_date();
// Insert values into MySQL table
sql::Driver *driver = get_driver_instance();
unique_ptr<sql::Connection> con(driver->connect("tcp://127.0.0.1:3306", "username", "password")); // Replace with your MySQL username and password
con->setSchema("mydatabase"); // Replace with your database name
unique_ptr<sql::PreparedStatement> pstmt(con->prepareStatement("INSERT INTO mytable (name, age, gender, phone_number_1, phone_number_2, id_number, email, license_number, date_issued, date_expiry, vehicle_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"));
pstmt->setString(1, name);
pstmt->setInt(2, age);
pstmt->setString(3, gender);
pstmt->setString(4, phone_number_1);
pstmt->setString(5, phone_number_2);
pstmt->setString(6, id_number);
pstmt->setString(7, email);
pstmt->setString(8, drivers_license_number);
pstmt->setString(9, date_issued);
pstmt->setString(10, date_expiry);
pstmt->setInt(11, type);
pstmt->executeUpdate();
cout << "Driver's license successfully added to the database." << endl;
} catch (const sql::SQLException &e) {
cout << "Error: " << e.what() << endl;
}
}
// Generate License number, date issued and expiry date
string generate_license_number() {
string license_num = "";
for (int i = 0; i < 7; i++) {
int random_num = rand() % 10;
license_num += to_string(random_num);
}
return license_num;
}
string generate_date() {
time_t now = time(0);
tm *date = localtime(&now);
string date_str = to_string(date->tm_mday) + "/" + to_string(1 + date->tm_mon) + "/" + to_string(1900 + date->tm_year);
return date_str;
}
string generate_expiry_date() {
time_t now = time(0);
tm *date = localtime(&now);
date->tm_year += 7; // License is valid for 7 years
mktime(date);
string expiry_date_str = to_string(date->tm_mday) + "/" + to_string(1 + date->tm_mon) + "/" + to_string(1900 + date->tm_year);
return expiry_date_str;
}
void renewLic( string expiry_date_str){
expiry_date = expiry_date_str;
cout << "Registration renewed. New expiry date: " << expiry_date << endl;
}
void display_information() {
try {
// Retrieve data from MySQL table
sql::Driver *driver = get_driver_instance();
unique_ptr<sql::Connection> con(driver->connect("tcp://127.0.0.1:3306", "username", "password")); // Replace with your MySQL username and password
con->setSchema("mydatabase"); // Replace with your database name
unique_ptr<sql::Statement> stmt(con->createStatement());
unique_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT * FROM mytable")); // Replace with your table name
// Loop through the result set and display information
while (res->next()) {
cout << "Name: " << res->getString("name") << endl;
cout << "Age: " << res->getInt("age") << endl;
cout << "Gender: " << res->getString("gender") << endl;
cout << "Phone Number 1: " << res->getString("phone_number_1") << endl;
cout << "Phone Number 2: " << res->getString("phone_number_2") << endl;
cout << "ID Number: " << res->getString("id_number") << endl;
cout << "Email: " << res->getString("email") << endl;
cout << "License Number: " << res->getString("license_number") << endl;
cout << "Date Issued: " << res->getString("date_issued") << endl;
cout << "Date Expiry: " << res->getString("date_expiry") << endl;
cout << "Vehicle Type: " << res->getInt("vehicle_type") << endl;
}
} catch (const sql::SQLException &e) {
cout << "Error: " << e.what() << endl;
}
}
};
void V_license(){
try {
// Prompt user for information
string name, gender, phone_number_1, phone_number_2, id_number, drivers_license_number, email, model,make;
int age, year,decide;
vehicle cars;
cout << "Enter your name: ";
getline(cin, name);
cout << "Enter your age: ";
cin >> age<< endl;
cout << "Enter your gender: ";
cin >> gender<<endl;
cout << "Enter your phone number 1: ";
cin >> phone_number_1<<endl;
cout << "Enter your phone number 2 (if any): ";
cin >> phone_number_2<<endl;
cout << "Enter your ID number: ";
cin >> id_number<<endl;
cout << "Enter your Driver's License number: ";
cin >> drivers_license_number<<endl;
cout << "Enter your email: ";
cin >> email<<endl;
cout << "Enter the model of your vehicle: ";
cin >> model<<endl;
cout << "Enter the make of your vehicle: ";
cin >> make<<endl;
cout << "Enter the year of your vehicle: ";
cin >> year<<endl;
cout << "Enter the type of your vehicle by selecting it's corresponding number: ";
cars.display_menu();
cin >> decide<<endl;// making a decision on the car using an integer type to generate the unique number plate
switch(decide){
case 1:
vehicle_ptr = new TypeA();
break;
case 2:
vehicle_ptr = new TypeB();
break;
case 3:
vehicle_ptr = new TypeC();
break;
case 4:
vehicle_ptr = new TypeD();
break;
case 5:
vehicle_ptr = new TypeE();
break;
case 6:
vehicle_ptr = new TypeF();
break;
default:
cerr<<"Invalid decision"<< endl;
return 1;
}
display info(); // Function call to use the MYSQL database
}
}
int main() {
Driver a;
Vehicle b;
int opt;
cout <<"Welcome to DVLA"<< endl;
cout <<"Please enter your Option"<< endl;
cout <<"1. Register Your Vehicle"<< endl;
cout <<"2.Regiter for a drivers license"<< endl;
cout <<"3.Renew your Vehicle registration"<< endl;
cout <<"4.Renew your drivers license"<< endl;
cin >> opt;
switch(opt){
case 1:
V_license() ;
break;
case 2:
a.d_license();
break;
case 3:
b.renewReg();
break;
case 4:
a.renewLic();
break;
default:
cerr<<"Invalid decision"<< endl;
return 1;
return 0;
}