Repository navigation
Expand file tree
/
Copy pathbookinng2.cpp
More file actions
executable file
·338 lines (312 loc) · 11.1 KB
/
Copy pathbookinng2.cpp
File metadata and controls
executable file
·338 lines (312 loc) · 11.1 KB
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
// bookinng2.cpp: îïðåäåëÿåò òî÷êó âõîäà äëÿ êîíñîëüíîãî ïðèëîæåíèÿ.
//
#include "stdafx.h"
#include "globals.h"
#include "sqlite3.h"
#include <iostream>
#include <fstream>
#include "User.h"
#include <windows.h>
#include <cstdlib>
#include "Country.h"
#include "City.h"
#include "BaseSQLiteModel.h"
#include "Reservation.h"
#include "Hotel.h"
#include "Router.h"
#include <locale>
using namespace std;
char* zErrMsg = NULL;
void show_admin_route()
{
std::cout << "Admin route";
}
void login_user()
{
if (User::global_auth_uther == NULL)
{
std::cout << "Login" << std::endl;
std::string phone, password;
std::cout << "Enter your phone: ";
std::cin >> phone;
std::cout << std::endl;
std::cout << "Enter your password: ";
std::cin >> password;
std::cout << std::endl;
User* user = User::authenticate(phone, password);
User::global_auth_uther = user;
if (user == NULL)
std::cout << "Incorrect phone or password user..." << std::endl;
else
std::cout << "Welcome, " + user->get_full_name() + "!" << std::endl;
}
else
std::cout << "Welcome, " + User::global_auth_uther->get_full_name() + "!" << std::endl;
}
void logout_user()
{
std::cout << "Logout user" << std::endl;
if (User::global_auth_uther == NULL)
return;
std::cout << "Buy, " << User::global_auth_uther->get_full_name() << "!" << std::endl;
User::global_auth_uther = NULL;
}
void registration_user()
{
if (User::global_auth_uther != NULL)
logout_user();
std::string first_name, second_name, middle_name, phone, password1 = "", password2 = "";
bool exists_phone = true;
std::cout << "Registration" << std::endl;
std::cout << "Enter your first name: ";
std::cin >> first_name;
std::cout << std::endl;
std::cout << "Enter your second name: ";
std::cin >> second_name;
std::cout << std::endl;
std::cout << "Enter your middle name: ";
std::cin >> middle_name;
std::cout << std::endl;
// check phone as login
while (exists_phone)
{
std::cout << "Enter your phone number: ";
std::cin >> phone;
std::cout << std::endl;
exists_phone = User::check_phone(phone);
if (exists_phone)
std::cout << "Phone already exists. Retype phone number..." << std::endl;
}
while ((password1 == "") || (password2 == "") || (password1 != password2))
{
std::cout << "Enter your password: ";
std::cin >> password1;
std::cout << std::endl;
std::cout << "Retype password: ";
std::cin >> password2;
std::cout << std::endl;
if (password1 != password2)
std::cout << "Error set password. Please retype yor password..." << std::endl;
}
User::global_auth_uther = User::create(first_name, second_name, middle_name, phone, password1);
std::cout << "We successfully registrated." << std::endl;
}
void admin_add_country()
{
std::string title;
std::cout << "Admin: add country" << std::endl;
std::cout << "Enter country title: ";
std::getline(std::cin, title);
std::cout << std::endl;
Country::create(title);
std::cout << std::endl;
}
void admin_add_city()
{
std::cout << "Admin: add city" << std::endl;
std::string title;
std::cout << "Enter city title: ";
std::getline(std::cin, title);
Country* country = Country::choice({ "title" });
if (country == NULL)
{
std::cout << "This city has not added" << std::endl;
return;
}
City* city = City::create(country->get_id(), title);
std::cout << std::endl;
}
void admin_add_hotel()
{
std::cout << "Admin: add hotel" << std::endl;
std::string title, phone, situated, description;
int position;
City* city;
std::cout << "Enter tiitle: ";
std::getline(std::cin, title);
std::cout << std::endl;
std::cout << "Enter phone: ";
std::getline(std::cin, phone);
std::cout << std::endl;
std::cout << "Enter situated (example 2016-10-01): ";
std::getline(std::cin, situated);
std::cout << std::endl;
std::cout << "Enter description: ";
std::getline(std::cin, description);
std::cout << std::endl;
std::cout << "Position: ";
std::cin >> position;
std::cout << std::endl;
city = City::choice({ "title" });
Hotel::create(title, phone, situated, city->get_id(), description, position);
std::cout << "Hotel successfully added" << std::endl;
}
void admin_add_room()
{
std::cout << "Admin: add room" << std::endl;
std::string title, description;
int level = 5;
Hotel* hotel;
float price;
std::cout << "Enter title: ";
std::getline(std::cin, title);
std::cout << "Enter description: ";
std::getline(std::cin, description);
std::cout << "Enter price: ";
std::cin >> price;
std::cin.ignore();
hotel = Hotel::choice({ "title", "description" });
if (hotel == NULL)
{
std::cout << "No hotels" << std::endl;
return;
}
Room::create(title, description, hotel->get_id(), level, price);
}
void add_reservations()
{
std::cout << "Admin: add reservations" << std::endl;
std::string start_date, end_date, note;
std::cout << "Enter date start reservation: ";
std::getline(std::cin, start_date);
std::cout << "Enter date end reservation: ";
std::getline(std::cin, end_date);
std::cout << "Enter note: ";
std::getline(std::cin, note);
Room* room = Room::choice({ "title", "position" });
if (room == NULL)
{
std::cout << "No room" << std::endl;
return;
}
if (User::global_auth_uther == NULL)
{
int i;
std::cout << "User not authenticated" << std::endl;
std::cout << "1- login, 2- registration;" << std::endl;
std::cin >> i;
std::cin.ignore();
if (i == 1)
login_user();
else
registration_user();
if (User::global_auth_uther == NULL)
{
std::cout << "Reservation not added" << std::endl;
return;
}
}
Reservation* reservation = Reservation::create(User::global_auth_uther->get_id(), room->get_id(), start_date, end_date, note);
std::cout << "User: " << User::global_auth_uther->get_full_name() << ". Add reservation to \"" << reservation->get_hotel()->get_title() << ". Date range: " << reservation->get_start_date() << " to " << reservation->get_end_date() << "." << std::endl;
}
void show_hotels_by_country()
{
std::cout << "Show hotels by country" << std::endl;
Country* country = Country::choice({ "title" });
if (country == NULL)
{
std::cout << "No such country" << std::endl;
return;
}
std::string fields = "`" + Hotel::get_table_name() + "`.`id`, `" + Hotel::get_table_name() + "`.`title`";
std::string sql_raw = "SELECT " + fields + " FROM `" + Hotel::get_table_name() + "` INNER JOIN `" + City::get_table_name() + "` ON (`" + Hotel::get_table_name() + "`.`city_id`=`" + City::get_table_name() + "`.`id`) WHERE `" + City::get_table_name() + "`.`country_id`=? ORDER BY `" + Hotel::get_table_name() + "`.`title`";
if (DEBUG)
std::cout << sql_raw << std::endl;
sqlite3_stmt* stmt;
if (sqlite3_prepare_v2(db_link, sql_raw.c_str(), -1, &stmt, 0) != SQLITE_OK)
{
std::cout << "SQL error: " << sqlite3_errmsg(db_link) << std::endl;
return ;
};
if (sqlite3_bind_int(stmt, 1, country->get_id()) != SQLITE_OK)
{
std::cout << "SQL error: " << sqlite3_errmsg(db_link) << std::endl;
return;
}
int i = 0;
while (sqlite3_step(stmt) == SQLITE_ROW)
{
if (i == 0)
{
printf("%-10s %-20s %-20s\n", "ID", "Title", "City");
}
i++;
Hotel* hotel = Hotel::get_by_id(sqlite3_column_int(stmt, 0));
City* city = hotel->get_city();
printf("%-10d", hotel->get_id());
std::cout << hotel->get_title() << std::string(" ", 20-hotel->get_title().length());
std::cout << city->get_title() << std::string(" ", 20-city->get_title().length()) << std::endl;
}
sqlite3_finalize(stmt);
if (i == 0)
{
std::cout << "No such hotels..." << std::endl;
return;
}
}
void show_city_by_city()
{
std::cout << "Show hotels by city" << std::endl;
}
void user_reservation()
{
std::cout << "Show user reservations" << std::endl;
}
void add_reservation(int id)
{
std::cout << "Add reservation" << std::endl;
}
void check_state(int result_code, char* query) {
if (result_code == SQLITE_OK)
return;
cout << "Error query: ``" << query << "``" << endl;
}
void initial_structure(sqlite3* db) {
char* table_countries = "CREATE TABLE Countries( id INTEGER PRIMARY KEY AUTOINCREMENT, title CHAR(256) );";
char* table_cities = "CREATE TABLE Cities( id INTEGER PRIMARY KEY AUTOINCREMENT, country_id INT, title CHAR(256), FOREIGN KEY(country_id) REFERENCES Countries(id) );";
char* table_users = "CREATE TABLE Users( id INTEGER PRIMARY KEY AUTOINCREMENT, first_name CHAR(256), second_name CHAR(256), middle_name CHAR(256), phone CHAR(32), password CHAR(1024), date_joined CHAR(32));";
char* table_hotels = "CREATE TABLE Hotels( id INTEGER PRIMARY KEY AUTOINCREMENT, title CHAR(1024) NOT NULL, phone CHAR(32), city_id INT NOT NULL, position INT NOT NULL, description VARCHAR(2048) NOT NULL, situated DATE NOT NULL, FOREIGN KEY(city_id) REFERENCES Cities(id));";
char* table_rooms = "CREATE TABLE Rooms( id INTEGER PRIMARY KEY AUTOINCREMENT, title CHAR(512), hotel_id INT NOT NULL, price FLOAT NOT NULL, position int INT NOT NULL, description VARCHAR(2048) NOT NULL, FOREIGN KEY(hotel_id) REFERENCES Hotels(id) );";
char* table_reservations = "CREATE TABLE Reservations( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INT NOT NULL, room_id INT NOT NULL, start_date DATE NOT NULL, end_date DATE NOT NULL, note VARCHAR(2048) NOT NULL, FOREIGN KEY(user_id) REFERENCES Users(id), FOREIGN KEY(room_id) REFERENCES Rooms(id) );";
int rc;
rc = sqlite3_exec(db, table_countries, NULL, 0, &zErrMsg);
check_state(rc, table_countries);
rc = sqlite3_exec(db, table_cities, NULL, 0, &zErrMsg);
check_state(rc, table_cities);
rc = sqlite3_exec(db, table_users, NULL, 0, &zErrMsg);
check_state(rc, table_users);
rc = sqlite3_exec(db, table_hotels, NULL, 0, &zErrMsg);
check_state(rc, table_hotels);
rc = sqlite3_exec(db, table_rooms, NULL, 0, &zErrMsg);
check_state(rc, table_rooms);
rc = sqlite3_exec(db, table_reservations, NULL, 0, &zErrMsg);
check_state(rc, table_reservations);
}
int _tmain(int argc, _TCHAR* argv[])
{
// setlocale(LC_ALL, "Russian");
bool need_initial = !(bool)ifstream("bookings.sqlite");
int rc = sqlite3_open("bookings.sqlite", &db_link);
if (need_initial)
initial_structure(db_link);
// admin module
Router* admin_main = new Router("Admin module");
admin_main->addRoute(new Router(admin_add_city, "Admin: Add city"));
admin_main->addRoute(new Router(admin_add_country, "Admin: Add country"));
admin_main->addRoute(new Router(admin_add_hotel, "Admin: Add hotel"));
admin_main->addRoute(new Router(admin_add_room, "Admin: Add room"));
// user module
Router* user_module = new Router("User module");
user_module->addRoute(new Router(login_user, "Login"));
user_module->addRoute(new Router(logout_user, "Logout"));
user_module->addRoute(new Router(registration_user, "Registration"));
user_module->addRoute(new Router(add_reservations, "Add reservation"));
user_module->addRoute(new Router(show_hotels_by_country, "Show hotels by country"));
// main module
Router* main = new Router("BOOKING-COM");
main->addRoute(admin_main);
main->addRoute(user_module);
main->navigate();
system("pause");
return 0;
}