-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook.cpp
More file actions
279 lines (230 loc) · 7.49 KB
/
Book.cpp
File metadata and controls
279 lines (230 loc) · 7.49 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
#include <iostream>
#include <ctime>
using namespace std;
#include "Book.h"
wstring PrintType( BookType type)
{
switch(type)
{
case BookType::Fiction : return L"Художественная литература";
case BookType::Journal : return L"Журнал";
case BookType::Textbook : return L"Учебная литература";
}
}
wstring PrintGenre( Genre genre )
{
switch(genre)
{
case Genre::Drama : return L"Пьеса";
case Genre::Novel : return L"Роман";
case Genre::Poem : return L"Поэма";
case Genre::Tale : return L"Повесть";
default: return L"Неизвестно";
}
}
wstring PrintDiscipline( Discipline discipline)
{
switch(discipline)
{
case Discipline::Economics : return L"Экономика";
case Discipline::Philosophy : return L"Философия";
case Discipline::Physics : return L"Физика";
case Discipline::Programming : return L"Программирование";
default: return L"Неизвестно";
}
}
wstring PrintTheme( Theme theme)
{
switch(theme)
{
case Theme::Auto : return L"Автомобили";
case Theme::Sport : return L"Спорт";
case Theme::Science : return L"Наука";
default: return L"Неизвестно";
}
}
Book::Book()
{
publicationYear = (rand() % 20) + 1980;
condition = rand() % 100;
numberOfPage = (rand() % 150) + 150;
}
BookContainer1::BookContainer1(int maxQuantity)
{
Bookcase = new PtrBook[maxQuantity];
for(int i = 0; i<maxQuantity ; i++)
{
Bookcase[i] = NULL;
}
MaxQuantity = maxQuantity;
BookQuantity = 0;
}
BookContainer1::~BookContainer1()
{
for(int i=0; i< MaxQuantity; i++)
{
if(Bookcase[i] != NULL)
{
delete Bookcase[i];
Bookcase[i] = NULL;
}
}
delete[] Bookcase;
}
void BookContainer1::AddBook(PtrBook book)
{
if(BookQuantity != MaxQuantity)
{
Bookcase[BookQuantity++] = book;
}
else
{
wcout<< L"Контейнер полон!!!" << endl;
}
}
BookContainer2::~BookContainer2()
{
for(int i=0; i< Bookcase.size(); i++)
{
if(Bookcase[i] != NULL)
{
delete Bookcase[i];
Bookcase[i] = NULL;
}
}
}
Book* Book::createBook(BookType type)
{
switch(type)
{
case BookType::Fiction: return new Fiction;
case BookType::Journal: return new Journal;
case BookType::Textbook: return new Textbook;
default: return nullptr;
}
}
BookType RandomBookType()
{
return BookType(rand()%3);
}
BookContainer3::BookContainer3()
{
int openResult = sqlite3_open("C:\\Users\\danch\\Documents\\Embarcadero\\Studio\\Projects\\Library\\Library.db", &Database);
if(openResult != SQLITE_OK)//если не смогли открыть базу данных
{
wcout << L"Ошибка открытия базы данных!" << endl;
}
id = 1;
}
BookContainer3::~BookContainer3()
{
string request = "DELETE FROM books";
char *errmsg;
int execResult = sqlite3_exec(Database, request.c_str(), NULL, 0, &errmsg);
if(execResult != SQLITE_OK)
{
wcout << L"Ошибка выполнения запроса!" << endl;
cout << errmsg << endl;
}
int close_result = sqlite3_close(Database);
if(close_result == SQLITE_OK)
{
wcout << L"Файл(База данных) закрылся" << endl;
}
}
void BookContainer3::AddBook(PtrBook book)
{
int year = book->GetYear();
int condition = book->GetCondition();
int pages = book->GetNumberOfPage();
int specific;
int type = (int)book->GetType();
switch(type)
{
case 0:
specific = (int)(((Fiction *)book)->GetGenre());
break;
case 1:
specific = (int)(((Textbook *)book)->GetDisc());
break;
case 2:
specific = (int)(((Journal *)book)->GetTheme());
break;
}
string request = "INSERT INTO books (Id, Year, Pages, Condition, Specific, Type) VALUES (" + to_string(id) + ", "
+ to_string(year) + ", "
+ to_string(pages) + ", "
+ to_string(condition) + ", "
+ to_string(specific) + ", "
+ to_string(type) + ")";
id++;
cout << request << endl;
char *errmsg;
int execResult = sqlite3_exec(Database, request.c_str(), NULL, 0, &errmsg);
if(execResult != SQLITE_OK)
{
wcout << L"Ошибка выполнения запроса!" << endl;
cout << errmsg << endl;
}
}
int BookContainer3::GetQuantity() const
{
string request = "SELECT count(*) FROM books";
sqlite3_stmt *pStatement;
int execResult2 = sqlite3_prepare_v2(Database,
request.c_str(),
-1, //здесь можно написать длину второго параметра
&pStatement, NULL);
if(execResult2 != SQLITE_OK)
{
wcout << "Ошибка подготовки запроса!" << endl;
const char *errmsg2 = sqlite3_errmsg(Database);
cout << errmsg2 << endl;
}
//выполнение запроса
int execResult3 = sqlite3_step(pStatement);
int n;
if(execResult3 == SQLITE_ROW)
{
n = sqlite3_column_int(pStatement, 0 /* номер столбца*/);
}
sqlite3_finalize(pStatement); //очистка памяти
return n;
}
PtrBook BookContainer3::GetByIndex(int i) const
{
string request = "SELECT * FROM books WHERE Id=" + to_string(i);
sqlite3_stmt *pStatement;
int execResult2 = sqlite3_prepare_v2(Database,
request.c_str(),
-1, //здесь можно написать длину второго параметра
&pStatement, NULL);
if(execResult2 != SQLITE_OK)
{
wcout << "Ошибка подготовки запроса!" << endl;
const char *errmsg2 = sqlite3_errmsg(Database);
cout << errmsg2 << endl;
}
//выполнение запроса
int execResult3 = sqlite3_step(pStatement);
int year;
int pages;
int cond;
int spec;
int type;
if(execResult3 == SQLITE_ROW)
{
year = sqlite3_column_int(pStatement, 1);
pages = sqlite3_column_int(pStatement, 2);
cond = sqlite3_column_int(pStatement, 3);
spec = sqlite3_column_int(pStatement, 4);
type = sqlite3_column_int(pStatement, 5);
}
switch((BookType)type)
{
case BookType::Fiction: sqlite3_finalize(pStatement); return new Fiction(year, pages, cond, spec);
case BookType::Journal: sqlite3_finalize(pStatement); return new Journal(year, pages, cond, spec);
case BookType::Textbook: sqlite3_finalize(pStatement); return new Textbook(year, pages, cond, spec);
default: sqlite3_finalize(pStatement); return nullptr;
}
}